SORTable - Manual | |||||
Home | Sort Table | Manual | Demo | Other Products |
Overview
The SORTable classes provide a simple way to add sorted tables to your Java applications.
Two packages are provided
Quick Start using the JDK
How the SORTable classes work - building a sorted table with 'double-click' sorting
public class VMTableDemo1 extends javax.swing.JPanel { public com.virtualmachinery.sortedtable.SortedListPanel sortTable = new com.virtualmachinery.sortedtable.ListPanelWithSortButton(); }
Next add an initialize() method which will set the Panel into the main frame of the application -
private void initialize() { try { setName("VMTableDemo1"); setLayout(null); setSize(489, 239); add(sortTable, sortTable.getName()); } catch (java.lang.Throwable ivjExc) { System.out.println(ivjExc); } }
public VMTableDemo1(){ super(); initialize(); }
public static void main(java.lang.String[] args) { try { javax.swing.JFrame frame = new javax.swing.JFrame(); VMTableDemo1 aVMTableDemo = new VMTableDemo1(); // ******** Code to initialise table model is here *** com.virtualmachinery.sortedtable.demo.SimpleAccountTableModel aSimpleAccountTableModel = new com.virtualmachinery.sortedtable.demo.SimpleAccountTableModel(); String[][] data = com.virtualmachinery.sortedtable.demo.TableDemoFactory.getSampleData(); aSimpleAccountTableModel.addAllRecords(data); aVMTableDemo1.sortTable.setSortTableModel(aSimpleAccountTableModel ); // ******** End of code to initialise table model *** frame.setContentPane(aVMTableDemo1); frame.setSize(aVMTableDemo1.getSize()); frame.addWindowListener(new java.awt.event.WindowAdapter() { public void windowClosing(java.awt.event.WindowEvent e) { System.exit(0); }; }); frame.show(); java.awt.Insets insets = frame.getInsets(); frame.setSize(frame.getWidth() + insets.left + insets.right, frame.getHeight() + insets.top + insets.bottom); frame.setVisible(true); } catch (Throwable exception) { System.err.println("Exception occurred in main() of VMTableDemo1"); exception.printStackTrace(System.out); } }
How the SORTable classes work - building a sorted table with associated sort button
public class VMTableDemo extends javax.swing.JPanel { public com.virtualmachinery.sortedtable.ListPanelWithSortButton sortTable = new com.virtualmachinery.sortedtable.ListPanelWithSortButton(); }
To demonstrate how easy it is to modify the appearance of the underlying JTable you can add the code below to the main method of either of the classes that you created above. In this case we are striping the columns painting each alternate one yellow. We are also altering the size of the third column and applying right alignment to the fifth column - -
/**** Start of code to change the table to show alternate colours on each column ****/ javax.swing.JTable theTable = aVMTableDemo.sortTable.getScrollPaneTable(); javax.swing.table.DefaultTableCellRenderer aRenderer = new javax.swing.table.DefaultTableCellRenderer(); aRenderer.setBackground(java.awt.Color.yellow); theTable.getColumnModel().getColumn(3).setPreferredWidth(20); int i = 0; while ( i < theTable.getColumnModel().getColumnCount()) { if (i==4) { javax.swing.table.DefaultTableCellRenderer rend2 = new javax.swing.table.DefaultTableCellRenderer(); rend2.setBackground(java.awt.Color.yellow); rend2.setHorizontalAlignment(javax.swing.SwingConstants.RIGHT); theTable.getColumnModel().getColumn(i).setCellRenderer(rend2); } else {theTable.getColumnModel().getColumn(i).setCellRenderer(aRenderer);} i+=2; } /**** End of code to change the table to show alternate colours on each column ****/ frame.show();
Creating a new class that implements the SortedTableModelInterface
The easiest way to create a new class to use which implements the interface is to extend the SortedTableModel and state that the class implements SortedTableModelInterface. This is what we have done with the SimpleAccountTableModel provided with the demo classes. Here's a step by step guide to creating the new class -