Virtual Machinery logo SORTable - Manual
Home Sort Table Manual Demo Other Products
Using the SORTable Classes



Overview

The SORTable classes provide a simple way to add sorted tables to your Java applications.

Two packages are provided

  • the core SORTable Java code as provided by Virtual Machinery - com.virtualmachinery.sortedtable
  • a package containing the SORTable demo code - com.virtualmachinery.sortedtable.demo

How you use the code will depend on your development environment. This page covers details on how to use the classes if you are using the JDK or some other development environment. If you are using IBMs VisualAge for Java then click here. Changes that are independent of platform can be found here.

Quick Start using the JDK

How the SORTable classes work - building a sorted table with 'double-click' sorting

  • Create a new class called VMTableDemo1 which extends JPanel and has an attribute sortTable which is an instance of com.virtualmachinery.sortedtable.ListPanelWithSortButton. The code for this will look something like this -
  • 
    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);
    	}
    }
    
  • Then add a constructor which will create and initialize a new instance of the class -
  • 
    public VMTableDemo1(){
    	super();
    	initialize();
    }
    
  • Your final task is to create a main method which can be used to create and display an instance of the class. Note the code which creates the data and adds the SimpleAccountTableModel to the panel -
  • 
    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);
    	}
    }
    
  • Run the code and you should see something like this -


  • SORTable Demo - click sort

    How the SORTable classes work - building a sorted table with associated sort button

  • Create a new class called VMTableDemo2 which extends JPanel and has an attribute sortTable which is an instance of com.virtualmachinery.sortedtable.ListPanelWithSortButton. The code for this will look something like this -
  • 
    public class VMTableDemo extends javax.swing.JPanel {
    	public com.virtualmachinery.sortedtable.ListPanelWithSortButton sortTable = new com.virtualmachinery.sortedtable.ListPanelWithSortButton();
    }
    
  • Next add an initialize() method which will set the Panel into the main frame of the application - this method will be identical to that used in VMTable1 above.
  • Then add a constructor which will create and initialize a new instance of the class - again this method will be identical to that used in VMTable1 above.
  • Your final task is to create a main method which can be used to create and display an instance of the class. Note the code which creates the data and adds the SimpleAccountTableModel to the panel - again this method will be identical to that used in VMTable1 above.
  • Run the main method and you should see something like this -


  • SORTable Demo - with sort button
  • How the SORTable classes work - modifying the presentation of a sorted table

    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();
    
  • Run the main method and you should see something like this -


  • SORTable Demo - striped columns

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 -

  • Create your new class extending the SortedTableModel and implementing SortedTableModelInterface
  • Create a method getColumnNames() which returns a string array containing the text that you wish to appear in your column headers in the order in which you wish to see it displayed
  • Provide methods to add records to the backing data - the methods addAllRecords(String[][]) and addRecord(String[], int) provided are simple examples of how to do this
  • Create the method sortOnField(int field, boolean ascending) which allows you to choose which method you will use to sort particular columns. You may of course choose not to allow sorting on a particular field - in this case you can do nothing but it would make sense to give some feedback to the user that sorting is not allowed on this field. You can send your feedback (e.g. a popup window) from this method as it will always be called when the user attempts to perform a sort operation