240 likes | 258 Views
How to Use Tables. The practice of JTable in swing. The JTable component. In many applications a Table is a natural representation to use: Displays a grid of data consisting of rows and columns similar to a spreadsheet.
E N D
How to Use Tables The practice of JTable in swing
The JTable component • In many applications a Table is a natural representation to use: • Displays a grid of data consisting of rows and columns similar to a spreadsheet. • The JTableclass is NOT a spreadsheet, but it supports many features that make it superior to a simple spreadsheet component. • The JTable component that provides the power to present large amounts of data in a simple 2D display • it supports custom data modeling and display rendering
A Simple table Example • Clicking the left mouse button on an individual cell will highlight the entire row and select the individual cell • Initially the select rectangle is surrounding the 89.2 value. • If you reduce the height of the window, the vertical scroll bar appears by default. • Resizing the frame in the horizontal direction automatically adds the horizontal scroll bar. • The application also inherits is the ability to reorganize and resize the columns: • Placing the mouse over a column name and dragging it (with the left mouse button pressed) allows the entire column to be moved to a new position. • When the mouse button is released, the selected column is dropped into its new location and the entire table is repainted to accommodate the adjustment. • Placing the mouse on the vertical separator between two column headers causes the pointer to change to a resize indicator. Holding down the left mouse button while dragging resizes the columns.
// Constructor of main frame public SimpleTableExample() { // Set the frame characteristics setTitle( "Simple Table Application" ); setSize( 300, 200 ); setBackground( Color.gray ); // Create a panel to hold all other components topPanel = new JPanel(); topPanel.setLayout( new BorderLayout() ); getContentPane().add( topPanel ); // Create columns names String columnNames[] = { "Column 1", "Column 2", "Column 3" }; // Create some data String dataValues[][] = { { "12", "234", "67" }, { "-123", "43", "853" }, { "93", "89.2", "109" }, { "279", "9033", "3092" } }; // Create a new table instance table = new JTable( dataValues, columnNames ); // Add the table to a scrolling pane scrollPane = new JScrollPane( table ); topPanel.add( scrollPane, BorderLayout.CENTER ); }
More complex example • Uses loader methods to load an array of eight columns by one hundred rows. • Additionally, the table is configured to show only the vertical grid lines and to allow simultaneous row and column selection. • Notice that with this example, when an individual cell is selected, a cross-hair selection pattern is formed, centered around the selected cell. • Also the foreground and background colours for the selection region have been altered for effect (since JTable supports colour changes for the selection area).
public AdvancedTableExample() { // Set the frame characteristics setTitle( "Advanced Table Application" ); setSize( 300, 200 ); setBackground( Color.gray ); // Create a panel to hold all other components topPanel = new JPanel(); topPanel.setLayout( new BorderLayout() ); getContentPane().add( topPanel ); // Create columns CreateColumns(); CreateData(); // Create a new table instance table = new JTable( dataValues, columnNames ); // Configure some of JTable's paramters table.setShowHorizontalLines( false ); table.setRowSelectionAllowed( true ); table.setColumnSelectionAllowed( true ); // Change the selection colour table.setSelectionForeground( Color.white ); table.setSelectionBackground( Color.red ); // Add the table to a scrolling pane scrollPane = table.createScrollPaneForTable( table ); topPanel.add( scrollPane, BorderLayout.CENTER ); }
Adding a Custom Data Model • The JTable supports the replacement of its data model in order to improve performance or to help reduce the size of the code required for a given application. • The custom data model feature used in JTable is actually simpler than the one used for (for example) the JTree class: • JTable only has to manage a simple 2D matrix of data • Re-engineer the code from the previous example. This example, • build a new application that uses a custom data model but • produces exactly the same results the previous example. • Though the output looks the same as the one shown previously, it offers much better performance, particularly as the size of the data set grows.
CustomDataTableExample • Contains the code for the main frame class, which is a modified version of the previous example: • the CreateData() method has been removed -- it is no longer required because the custom data model generates the data set. • the CreateColumns() method has been changed -- the code no longer uses an instance of the DefaultDataModel class (instantiated automatically by JTable in the previous example). • the application is now required to create its own columns -- the CreateColumns() method reverts to first principles by creating an instance of a TableColumn object and populating it with the appropriate text before adding it to the table. • the example now generates its own column data therefore it notifies the table not to attempt to determine this information from the data. It does this by calling JTable's setAutoCreateColumnsFromModel() method.
CustomDataModel • Thee custom data model, which extends the AbstractTableModel class and provides four methods called by the JTable code when data is accessed. • Since the data model is now synthesized, the getValueAt() method simply determines the row and column being accessed and generates a string representing the cell data. • The getColumnCount() method returns a value of zero, which may not be what you expected. Remember, though, that this method returns the number of columns managed by the table code. In the CreateColumns() method in the main CustomDataTableExample class, we informed the table that it should not attempt to generate any of its own columns. As a result, the value of getColumnCount() is zero. • Explore the full listings via the course web site
Custom Table Rendering • As in other components can alter the display by developing custom rendering classes. • Modify the initial example to implement a custom rendered JTable: • implements the custom rendering on top of the modified code from the first example • Instead of displaying numerical (X, Y) data, the code displays a card deck in tabular form • Each column represents a suit in the deck, with each cell drawn as a graphical suit and a card number.
CustomRenderTableExample • contains the code for the main frame of the application. The only significant notable in this file is the call the JTable's setCellRenderer() method, which informs the table object that all of its drawing will be handled by an external rendering class. • CustomCellRenderer • contains the code for the custom table cell renderer, implementing a method named getTableCellRendererComponent() to handle the drawing of individual cells. This method determines if the item it is drawing is selected or has the focus, and uses this information to control the foreground colour. Because of the restrictions with regards to painting the background colour, this class also provides a paint() method, which uses the selection and focus flags as well. • CustomDataModel • includes a custom data model, which is similar to previous examples. The only difference is in the number of rows in the model
Listening for Table Actions • Many situations demand that the application immediately detect changes initiated by the user. • These events can be sorted into two basic categories: • cell selections made by the user, and • user-invoked changes to the table itself.
Detecting Table Selections • The type of action usually involves a mouse click to select a single cell or a drag operation to select a range of values from the table. • JTable implements selection listeners in exactly the same way as the JList -- ListSelectionListener. • To enable selection events, the code adds a ListSelectionListener to the table's selection model. • valueChanged() method is required in order to implement ListSelectionListener.
public void valueChanged( ListSelectionEvent event ) { // See if this is a valid table selection if( event.getSource() == table.getSelectionModel() && event.getFirstIndex() >= 0 ) { // Get the data model for this table TableModel model = (TableModel)table.getModel(); // Determine the selected item String string = (String)model.getValueAt( table.getSelectedRow(), table.getSelectedColumn() ); // Display the selected item System.out.println( "Value selected = " + string ); } } • this method first determines if the event originates from our JTable instance's selection model, and then it ensures that the event references a valid selection. To access the selected cell, the code references the table's data model and simply extracts the data based on the selected row and column.
Detecting Column Property Changes • Second significant series of events produced by table activity relate to manipulations of the column presentation: • Any time the user moves a column from one place to another, or adds a new column, the table generates an event -- a column model change event. • To intercept these events, a listener must be associated with the table's column model. This is done using the following code: • // Handle the listener DefaultTableColumnModel columnModel = (DefaultTableColumnModel)table.getColumnModel(); columnModel.addColumnModelListener( this );
TableColumnModelListener Interface Methods called whenever the column model changes public void columnAdded( TableColumnModelEvent event ) public void columnRemoved( TableColumnModelEvent event ) public void columnMoved( TableColumnModelEvent event ) public void columnMarginChanged( ChangeEvent event ) public void columnSelectionChanged( ListSelectionEvent event ) • constants used as flags to control the resize state of columns in the table. These constants are used as parameters to the setAutoResizeMode() method. public static final int AUTO_RESIZE_LAST_COLUMN public static final int AUTO_RESIZE_OFF public static final int AUTO_RESIZE_ALL_COLUMNS
Key Variables • The following variables hold instances of the various models associated with a JTable object: • protected TableModel dataModel protected TableColumnModel columnModel protected ListSelectionModel selectionModel • The protected JTableHeader tableHeader variable holds the instance of the table headers for this JTable object. The header holds particulars, such as, the column object. • The following variables contain information to manage the attributes of a table row: • protected int rowHeight protected int rowMargin • The colour of the grid lines drawn in the table is stored in the protected Color gridColor variable.
More key variables • The following boolean flags contain information about the states of particular modes within the JTable object: • protected boolean showHorizontalLines protected boolean showVerticalLines protected boolean autoCreateColumnsFromModel protected boolean rowSelectionAllowed • The value of the protected int autoResizeMode variable determines if the table object automatically resizes the width the columns to occupy the entire width of the table. It is also used to decide how the resizing is done. • The protected Dimension preferredViewportSize variable is used by the scrolling pane instance associated with the table object. It determines the initial visible area for the table. • When the table is performing a cell editing operation, the protected transient Component editorComp variable holds an instance of the component used to handle the editing operation.
JTable variables • The protected transient TableCellEditor cellEditor variable holds an instance of the cell editor used by this table object. • When editing table cells, the following variables contain the row and column address of the cell being edited. • protected transient int editingRow protected transient int editingColumn • The following variables are used to keep track of the default cell editors and renderers known to this table object: • protected Hashtable defaultRenderersByColumnClass protected Hashtable defaultEditorsByColumnClass • These variables contain the colours of the foreground and background used to draw selected text. These values are overridden by any custom cell render code: • protected Color selectionForeground protected Color selectionBackground
Summary • Presented an Overview of JTree • Should read these notes alongside the example code available on the web site • Down run compile and run the code • Supplement by looking at the Sun tutorials on Jtree (be award of the1.6 dependency) • Next • Consider how the different components can best be put together by looking at the graphical design and interface principles