The next sections cover aspects of JTable that you might want to include in your project. Although most of them are not required, they can improve the user experience without the risk of introducing errors. Some of these examples are inspired from The Java Developers Almanac 1.4 (Addison Wesley Professional, 2002, ISBN 0201752808; see "Need to Know More?" at the end of this chapter for an online version), an excellent book of code snippets. Setting Column Header Tool TipsIn my project submission, I added tool tips to the column headers. The tool tips didn't require much code, but adding them is justified by making the GUI easier to use, a factor in scoring the client. The trick is to install a mouse motion listener on the header component, as shown here: JTable table = new JTable(rows, cols); JTableHeader header = table.getTableHeader(); header.setToolTipText("Click a header to sort table by that column"); Using a Scrollable JTable ComponentI recommend a scrollable JTable. By default, a table is created with auto resizing enabled, so if the user changes the table width, the columns automatically expand or shrink to keep their contents visible. However, this mode removes the horizontal scrollbar. The following code snippet demonstrates how to create a new JTable and then make it automatically scroll when the table's contents exceed the visible grid's area onscreen: // Create a table with 10 rows and 5 columns int numRows = 3; int numColumns = 4 JTable table = new JTable(numRows, numColumns); JScrollPane scrollPane = new JScrollPane(table); table.setAutoResizeMode(JTable.AUTO_RESIZE_OFF); Handling Cell SelectionsHow do you allow cell selection? You must enable row selections so that clicking on any cell selects the entire row containing that cell. It works the same for columns. The following code snippet shows you how to handle cell selection that takes place when a user clicks on the table: JTable table = new JTable(); //Row selection on, column selection off table.setColumnSelectionAllowed(false); table.setRowSelectionAllowed(true); //Both on to get cell selection table.setColumnSelectionAllowed(true); table.setRowSelectionAllowed(true); Changing the Name of a ColumnYou can change the name of a column in a JTable component, but be careful. If you change the column name, be sure you don't rely on the column name for any logic, such as searching the database by the JTable column instead of the database column. The following code shows you how to place a name at the top of a table column: JTableModel tableModel = new DefaultTableModel(); JTable table = new JTable(tableModel); tableModel.addColumn("firstColumn"); tableModel.addColumn("secondColumn"); // Change name of first column table.getColumnModel().getColumn(0).setHeaderValue("newName"); //Resize and repaint header table.getTableHeader().resizeAndRepaint(); Getting and Setting a Cell ValueMost likely, you will need to get the value of an individual cell when a user clicks on your JTable. Some candidates allow users to change the value (for example, the seat number) right in the table. I chose to get the cell value when a user clicked on that row and place the value into another component. This manner of handling user input removed the possibility of typing errors, and it seemed to give the GUI a more natural feel for user input. Note that if the value in the view or model changes, you must add code to synchronize the two.The following code snippet demonstrates how to get a user selection from the table after the user clicks it: int row = 8; int column = 7; Object o = table.getValueAt(row, column); // Get value in model o = table.getModel().getValueAt(row, column); // Change table cell table.setValueAt("Kasienne", row, column); // Change model cell table.getModel().setValueAt("Kasienne", row, column);
|