How to load a JTable control with data and customize its appearance.
You can run this technique code from:
NOTE First make sure that database is running on your localhost SilverStream Server | |
See the chapter on the Form Designer in the Tools Guide |
This example displays an AgcJTable control with other controls that allow the user to customize the table's appearance. The data in the table comes from the companies table and is loaded from an AgcData control.
To work with the classes for the parts of a table and its data model, include this import statement in your form:
import javax.swing.table.*;
In this example, the formActivate event initializes the controls. The code does these things:
setReorderingAllowed().
retrieveTheData()
, described in
Loading the table with data.
protected void formActivate() { // Save our header so people can make runtime changes. m_jTableHeader = jtableCompanies.getTableHeader(); try { // Populate the table rows retrieveTheData(); } catch(Exception ex) { agDialog.displayError(ex); } // Set the default states chkShowGrid.setState(true); chkVerticalLines.setState(true); chkHorizontalLines.setState(true); jSliderRow.setMajorTickSpacing(5); jSliderRow.setPaintTicks (true); jSliderRow.setValue(jtableCompanies.getRowHeight()); Color formBackground = new Color(193,206,212); jSliderRow.setBackground(formBackground); jSliderCell.setBackground(formBackground); jSliderCell.setPaintTicks (true); jSliderCell.setMajorTickSpacing(2); cbSelectionChoice.select(0); chkAllowReorder.setState(false); m_jTableHeader.setReorderingAllowed(false); }
getRowHeight()
and assigns it to the slider in the setValue()
method.
setPaintTicks()
, setMajorTickSpacing()
, and setBackground()
.
setReorderingAllowed()
is a method of JTableHeader, not JTable.
In this example, the table displays data from the companies table. There are two columns: company names and company descriptions.
To load the data, code in the formActivate event calls the user-defined method retrieveTheData()
. This method uses the loadFromRowCursor()
method to populate the table from dataCompanies
, an AgcData control that gets its data from the companies table.
public void retrieveTheData() { try { // Populate the table rows int[] columnIndexes = { dataCompanies.getPropertyIndex("companyname"), dataCompanies.getPropertyIndex("description") }; int valueColumnIndex = dataCompanies.getPropertyIndex("companyid"); dataCompanies.query(""); // Load the table from the row cursor jtableCompanies.loadFromRowCursor(dataCompanies, columnIndexes, null, valueColumnIndex, m_sColumnTitles); } catch(Exception ex) { agDialog.displayError(ex); } }
The loadFromRowCursor()
method uses properties of dataCompanies
as the columns in the table. You specify:
dataCompanies
that will become columns (the columnIndexes
array, which holds property indexes of dataCompanies
)
valueColumnIndex
variable)
m_sColumnTitles
in the declarations section of the form.
loadFromRowCursor()
must be called in a try/catch block.
The controls on the form change several aspects of the table's appearance. The table lists the appearance settings and the methods that affect them.
The form presents a color selection dialog so the user can select a color visually. The current color is the default. This is the code for the actionPerformed event of the imgBackgroundColor button:
Color colorBackground = JColorChooser.showDialog ( getParent(), "Selection Background Color", jtableCompanies.getSelectionBackground() ); jtableCompanies.setSelectionBackground (colorBackground);
The form uses sliders for row height and cell spacing.
This code for the valueChanged event of jSliderRow changes the row height based on the integer value of the slider:
jtableCompanies.setRowHeight (jSliderRow.getValue());
This code for the valueChanged event of jSliderCell uses the slider value to construct a Dimension object for the cell spacing:
int iSpacing = jSliderCell.getValue(); jtableCompanies.setIntercellSpacing( new Dimension(iSpacing, iSpacing));
The form uses check boxes for several settings, including grid display and column reordering.
This code for the actionPerformed event of chkShowGrid turns the grid on or off according to the check box state:
if (chkShowGrid.getState()) jtableCompanies.setShowGrid(true); else jtableCompanies.setShowGrid(false);
This code for the actionPerformed event of chkAllowReorder enables or disables reordering. The setReorderingAllowed()
method is in the JTableHeader class:
if (chkAllowReorder.getState()) m_jTableHeader.setReorderingAllowed(true); else m_jTableHeader.setReorderingAllowed(false);
This code gets the value the user selected:
int iSelectedItem = cmbxSelectionChoice.getSelectedIndex(); if (iSelectedItem < 0) iSelectedItem = 0; String sChoice = cmbxSelectionMode.getItemAt(iSelectedItem).toString();
The code in the actionPerformed event for cbSelectionChoice
combo box uses three JTable methods to control the selection type. For example, to enable cell selection:
if (sChoice.equals("Cell")) { jtableCompanies.setCellSelectionEnabled(true); jtableCompanies.setColumnSelectionAllowed (false); jtableCompanies.setRowSelectionAllowed (false); }
The user can sort column rows by clicking on the column header. This feature uses two utility objects called TableMap and TableSorter. These objects were placed in a JAR (SwingUtilities.jar) and the JAR file was then added to the form. The following import statement appears in the General section of the code:
import com.examples.utilityMethods.Swing.*;
The following code from the formActivate event activates the sort feature by calling methods on the utility objects:
// Enable sorting capability for the JTable by clicking on // the header TableSorter sorter = new TableSorter(jtableCompanies.getModel()); jtableCompanies.setModel(sorter); sorter.addMouseListenerToHeaderInTable(jtableCompanies);
The code for TableSorter and TableMap was obtained from Sun's tutorial section for Swing Controls. For access to the commented code, go to the TableSorterDemo on Sun's website.
When the button labeled "allow reordering is activated, the user can rearrange the column positions by dragging the table headers. The code sets the setReorderingAllowed()
method to true. This is a method called on the variable m_JTableHeader, an instance of the JTableHeader class.
private void handle_chkAllowReorder_actionPerformed( ActionEvent evt) { if (chkAllowReorder.getState()) m_jTableHeader.setReorderingAllowed(true); else m_jTableHeader.setReorderingAllowed(false); }