Presentation is loading. Please wait.

Presentation is loading. Please wait.

Advanced Swing Tables.

Similar presentations


Presentation on theme: "Advanced Swing Tables."— Presentation transcript:

1 Advanced Swing Tables

2 Contents A Simple Table Table Models Working with Rows and Columns
Cell Rendering and Editing

3 I. A Simple Table

4 JTable A JTable does not store its own data but obtains its data from a table model. The JTable class has a constructor that wraps a two-dimensional array of objects into a default model. Object[][] cells = { { "Mercury", , 0, false, Color.YELLOW }, { "Venus", , 0, false, Color.YELLOW }, }

5 Supply the column names in a separate array of strings
String[] columnNames = { "Planet", "Radius", "Moons", "Gaseous", "Color" }; Construct a table from the cell and column name arrays JTable table = new JTable(cells, columnNames); table.setAutoCreateRowSorter(true); JScrollPane pane = new JScrollPane(table);

6

7

8 II. Table Models The AbstractTableModel class provides default implementations for most of the methods in the TableModel interface. You only need to supply three methods: public int getRowCount(); public int getColumnCount(); public Object getValueAt(int row, int column); The getColumnName method of the AbstractTableModel names the columns A, B, C, and so on. To change column names, override the getColumnName method.

9

10 Construct a table that shows some computed values, namely, the growth of an investment under different interest rate scenarios. Number of rows: Number of years Number of columns: determined from minimal rate and maximal rate

11

12

13

14 III. Working with Rows and Columns
Column Classes To give the table more information about the column types Class<?> getColumnClass(int columnIndex) The JTable class uses this information to pick an appropriate renderer for the class Type Rendered As Boolean Checkbox Icon Image Object String

15 Display images of planets in TablePlanet program
Insert one more column: images of planets Create a DefaultTableModel object that overwrite the method getColumnClass Return the class that describe the column type.

16

17

18 Accessing Table Columns
The JTable class stores information about table columns in objects of type TableColumn. A TableColumnModel object manages the columns. TableColumn column = table.getColumnModel() getColumn(columnIndex);

19 void setPreferredWidth(int width) void setMinWidth(int width)
Resizing Columns The TableColumn class gives you control over the resizing behavior of columns void setPreferredWidth(int width) void setMinWidth(int width) void setMaxWidth(int width) void setResizable(boolean resizable) void setWidth(int width) void setAutoResizeMode(int mode)

20

21 Resizing Rows Row heights are managed directly by the JTable class. table.setRowHeight(height); // All rows table.setRowHeight(row, height); // a row table.setRowMargin(margin);

22 table.getSelectionModel().setSelectionMode(mode);
Selecting Rows, Columns, and Cells Depending on the selection mode, the user can select rows, columns, or individual cells in the table. By default, row selection is enabled. Clicking inside a cell selects the entire row. Row Selection void setRowSelectionAllowed(boolean allowed) To set selection mode Retrieve the selection model and use its setSelectionMode method table.getSelectionModel().setSelectionMode(mode);

23 mode is one of the three values:
ListSelectionModel.SINGLE_SELECTION ListSelectionModel.SINGLE_INTERVAL_SELECTION ListSelectionModel.MULTIPLE_INTERVAL_SELECTION

24 Column Selection Column selection is disabled by default. You turn it on with the call table.setColumnSelectionAllowed( true) Cell Selection Enabling both row and column selection is equivalent to enabling cell selection. The user then selects ranges of cells. You can also enable that setting with the call table.setCellSelectionEnabled(tr ue)

25 removeColumn(TableColumn column);
Hiding and Displaying Columns The removeColumn method of the JTable class removes a column from the table view. The column data are not actually removed from the model—they are just hidden from view. removeColumn(TableColumn column); If you have the column number (for example, from a call to getSelectedColumns), you need to ask the table model for the actual table column object: TableColumnModel columnModel = table.getColumnModel(); TableColumn column = columnModel.getColumn(i); table.removeColumn(column);

26 Cell Rendering and Editing
The column type determines how the cells are rendered. There are default renderers: Boolean → A checkbox Icon → An Image All other types → A string , otherwise we need to install a custom renderer. Table cell renderers are similar to the list cell renderers. They implement the TableCellRenderer interface.

27 To tell the table to use this renderer with all objects of a type:
public interface TableCellRenderer { Component getTableCellRendererComponent( JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column); } That method getTableCellRendererComponent is called when the table needs to draw a cell. You return a component whose paint method is then invoked to fill the cell area. To tell the table to use this renderer with all objects of a type: setDefaultRenderer(Class columnClass, TableCellRenderer renderer)

28 To choose an appropriate renderer for the header value:
Rendering the Header To display an object (icon ...) in the header, call setHeaderValue of TableColumn: setHeaderValue(Object headerValue) To choose an appropriate renderer for the header value: setHeaderRenderer(TableCellRenderer headerRenderer)

29 public boolean isCellEditable(int r, int c)
Cell Editing To enable cell editing, the table model must indicate which cells are editable by defining the isCellEditable method. public boolean isCellEditable(int r, int c) Most commonly, you will want to make certain columns editable. In the example program, we allow editing in four columns. public boolean isCellEditable(int r, int c) { // return true if c satisfied some condition }

30 A DefaultCellEditor can be constructed with a JTextField, a JCheckBox, or a JComboBox.
Call setCellEditor to set an DefaultCellEditor object to a TableColumn object The JTable class automatically installs a checkbox editor for Boolean cells and a text field editor for all editable cells that don't supply their own renderer.

31 Ex: Set a combo box to a TableColumn object
JComboBox moonCombo = new JcomboBox(); for (int i = 0; i <= 20; i++) moonCombo.addItem(i); TableColumnModel columnModel = table.getColumnModel(); TableColumn moonColumn = columnModel.getColumn(columnIndex); moonColumn.setCellEditor( new DefaultCellEditor(moonCombo));

32 Custom Editors Extend the AbstractCellEditor class and implement the TableCellEditor interface. Define the getTableCellEditorComponent method to supply a component. Define the shouldSelectCell, stopCellEditing, and cancelCellEditing methods to handle the start, completion, and cancellation of the editing process. The stopCellEditing and cancelCellEditing methods should call the superclass methods to ensure that listeners are notified. Define the getCellEditorValue method to return the value that is the result of the editing process.

33 To tell the table to use the editor with all objects of a type:
setDefaultEditor(Class columnClass, TableCellEditor editor)

34

35

36

37

38

39

40

41

42


Download ppt "Advanced Swing Tables."

Similar presentations


Ads by Google