All these components are derived from the abstract class javax. Figure 1 shows a simple Java application with a JFrame. If you click the second button, the table is added using createGraphics the lower PDF, using the standard Type 1 font Helvetica. Notice the subtle differences between the fonts used for both variants. Figure 1. Click on thumbnail to view full-sized image.
|Published (Last):||19 November 2007|
|PDF File Size:||11.27 Mb|
|ePub File Size:||4.22 Mb|
|Price:||Free* [*Free Regsitration Required]|
Concepts: Editors and Renderers Before you go on to the next few tasks, you need to understand how tables draw their cells. You might expect each cell in a table to be a component. However, for performance reasons, Swing tables are implemented differently.
Instead, a single cell renderer is generally used to draw all of the cells that contain the same type of data. You can think of the renderer as a configurable ink stamp that the table uses to stamp appropriately formatted data onto each cell. If the user begins editing one of the cells, the default cell editor uses a right-aligned JTextField to control the cell editing.
To choose the renderer that displays the cells in a column, a table first determines whether you specified a renderer for that particular column. This list is initialized by the table, but you can add to it or change it. Cell editors are chosen using a similar algorithm. Remember that if you let a table create its own model, it uses Object as the type of every column.
To specify more precise column types, the table model must define the getColumnClass method appropriately, as demonstrated by TableDemo. Keep in mind that although renderers determine how each cell or column header looks and can specify its tool tip text, a renderer does not handle events. If you need to pick up the events that take place inside a table, the technique you use varies by the sort of event you are interested in: Situation To detect events from a cell that is being edited Use the cell editor or register a listener on the cell editor.
Use a selection listener as described in Detecting User Selections. To detect mouse events on a column header See TableSorter.
To detect other events Register the appropriate listener on the JTable object. The next few sections tell you how to customize display and editing by specifying renderers and editors. You can specify cell renderers and editors either by column or by data type. Using Custom Renderers This section tells you how to create and specify a cell renderer. You can set a type-specific cell renderer using the JTable method setDefaultRenderer.
To specify that cells in a particular column should use a renderer, you use the TableColumn method setCellRenderer. You can even specify a cell-specific renderer by creating a JTable subclass. It is easy to customize the text or image rendered by the default renderer, DefaultTableCellRenderer. You just create a subclass and implement the setValue method so that it invokes setText or setIcon with the appropriate string or image. The easiest way is to create a subclass of an existing component, making your subclass implement the TableCellRenderer interface.
Your implementation of this method should set up the rendering component to reflect the passed-in state, and then return the component. Here are excerpts from ColorRenderer. This section shows you how to use both techniques. To add a tool tip to a cell using its renderer, you first need to get or create the cell renderer. Then, after making sure the rendering component is a JComponent, invoke the setToolTipText method on it. An example of setting tool tips for cells is in TableRenderDemo.
Or, to compile and run the example yourself, consult the example index. The source code is in TableRenderDemo. An example of adding code to a cell renderer is in TableDialogEditDemo. The program TableToolTipsDemo shows how. The cells with tool tips are in the Sport and Vegetarian columns. Since prepareRenderer provides the view index, you need to translate the view index to a model index so you can be sure the intended column has been selected. Often, different column headers require different tool tip text.
Alternately, you can invoke TableColumn. An example of using the same tool tip text for all column headers is in TableSorterDemo. Here is how it sets the tool tip text: table. You will see the tool tips when you mouse over any column header except for the first two. No tool tips were suppled for the name columns since they seemed self-explanatory. Here is a picture of one of the column header tool tips: The following code implements the tool tips. Basically, it creates a subclass of JTableHeader that overrides the getToolTipText MouseEvent method so that it returns the text for the current column.
To associate the revised table header with the table, the JTable method createDefaultTableHeader is overridden so that it returns an instance of the JTableHeader subclass.
This provides a table that does a simple locale-specific sort when the user clicks on a column header. This is demonstrated in TableSortDemo. Comparator objects to sort its rows. A class that implements this interface must provide a method called compare that defines how any two objects are compared for the purpose of sorting.
You can define your own subclass, use the factory methods in Collator to obtain a Comparator for a specific locale, or use java. To determine which Comparator to use for a column, TableRowSorter attempts to apply each of the following rules in turn. Rules are followed in the order listed below; the first rule that provides the sorter with a Comparator is used, and the remainining rules ignored. If a comparator has been specified by invoking setComparator , use that comparator. If the table model reports that the column data consists of strings TableModel.
If the column class returned by TableModel. If a string convertor has been specified for the table by invoking setStringConverter , use a comparator that sorts the resulting string representations based on the current locale.
If none of the previous rules apply, use a comparator that invokes toString on the column data and sorts the resulting strings based on the current locale. For more sophisticated kinds of sorting, subclass TableRowSorter or its parent class javax. To specify the sort order and sort precedence for columns, invoke setSortKeys. Here is an example that sorts the table used in the examples by the first two columns. The precedence of the columns in the sort is indicated by the order of the sort keys in the sort key list.
In this case, the second column has the first sort key, so they rows are sorted by first name, then last name. SortKey 1, SortOrder. SortKey 0, SortOrder. This is known as filtering.
TableRowSorter implements filtering using javax. RowFilter objects. RowFilter implements several factory methods that create common kinds of filters. For example, regexFilter returns a RowFilter that filters based on a regular expression.
When the user enters complicated regular expressions, the try When a table uses a sorter, the data the users sees may be in a different order than that specified by the data model, and may not include all rows specified by the data model.
The data the user actually sees is known as the view, and has its own set of coordinates. The following example brings together the ideas discussed in this section. These include the code snippets earlier in this section, which provide a sorter for the main table, and use a text field to supply the filtering regular expression. The following screen shot shows TableFilterDemo before any sorting or filtering has been done. As with sorting, filtering can cause view coordinates to diverge from model coordinates: Here is the code that updates the status field to reflect the current selection: table.
The bold line of code sets up the combo box as the editor for a specific column. Using Other Editors Whether you are setting the editor for a single column of cells using the TableColumn setCellEditor method or for a specific type of data using the JTable setDefaultEditor method , you specify the editor using an argument that adheres to the TableCellEditor interface. Usually you do not have to explicitly specify a check box as an editor, since columns with Boolean data automatically use a check box renderer and editor.
What if you want to specify an editor other than a text field, check box, or combo box? As DefaultCellEditor does not support other types of components, you must do a little more work. You need to create a class that implements the TableCellEditor interface. The AbstractCellEditor class is a good superclass to use. The getTableCellEditorComponent method, required by TableCellEditor, should configure and return the component that you want to use as the editor.
Here is a picture of a table with a dialog that serves, indirectly, as a cell editor. When the user begins editing a cell in the Favorite Color column, a button the true cell editor appears and brings up the dialog, with which the user can choose a different color. Here is the code, taken from ColorEditor. Without this call, the editor would remain active, even though the modal dialog is no longer visible. The call to fireEditingStopped lets the table know that it can deactivate the editor, letting the cell be handled by the renderer again.
The error checking is a side effect of converting the entered text into an object of the proper type. The default editor creates this instance using a constructor that takes a String as an argument. For example, in a column whose cells have type Integer, when the user types in "" the default editor creates the corresponding Integer using code equivalent to new Integer "".
If you implement a class used as a column data type, you can use the default editor if your class supplies a constructor that takes a single argument of type String. The formatted text field can check the value either continuously while the user is typing or after the user has indicated the end of typing such as by pressing Enter. The following code makes the formatted text field the editor for all columns that contain data of type Integer.
Export Swing components to PDF
Print JTable to Pdf directly : JTable to Pdf « PDF RTF « Java
Java Swing | JTable