Table Widget: A Tutorial

Table Widget
Introduction
Model
Demo
<<Tutorial>>
Documentation
Download

The Table widget is part of the package COM.Subrahmanyam.table available in compressed format (in table.jar). See download instructions for more information. Also download the documentation from the download section.

Before using this package, make sure you add table.jar in your CLASSPATH.

The following code requires JDK1.1 or later and presumes that you're familiar with the delegation event model.



Skelton code

Here is some skeleton code to create a Frame containing an instance of the Table Widget. See SampleTable.java for complete source.

// Import the Table.
import COM.Subrahmanyam.table.*;

// Imort the AWT.
import java.awt.*;
import java.awt.event.*;

public class SampleTable extends Frame {
    
    private Table table;
    
    public SampleTable() {
	
	// Create a table with some size.
	table = new Table();
	table.setSize(300, 200);
	
	// Set some properties:
	table.showHorizontalSeparator(false); 
	table.showVerticalSeparator(true);    
	table.setHiliteMode(Table.HILITE_ROW);
	table.setClickMode(Table.DOUBLE_CLICK);
	table.setLineSpacing(1.25);            
	table.setFonts(new Font("Helvetica", Font.PLAIN, 12),
		       new Font("Helvetica", Font.PLAIN, 12));
	
	// Create some data to be added to the table.
	String column[] = {new String("Welcome"), 
			   new String("to the"), 
			   new String("demo")};
	
	// Let the title of the first column be "One". 
	//Add the column.
	table.addColumn("One", column);
	
	// Add another column.
	table.addColumn("Two", column);
	
	// Now add some rows.
	String row[] = {new String("Hi"), new String("There")};
	table.addRow(row);
	
	// Add listeners.
	table.addItemListener(new IL());
	table.addActionListener(new AL());
	
	// Now add the table to the frame.
	setLayout(new BorderLayout());
	add("Center", table);
    }
    
    // Rest of the code here.
}

This code creates an instance of the Table widget.

The other possible constructors are:

      public Table(int width, int height, boolean multipleSelection)

      public Table(int width, int height, boolean multipleSelection, 
                   int ClickMode)
		

The first of these accepts a boolean argument. When this argument true multiple selections are allowed.

The second constructor accepts another argument clickMode which should be one of Table.HILITE_ELEMENT, Table.HILITE_ROW and Table.HILITE_COLUMN. For more details on these, see Table Widget: Model. Since multiple-selections are not allowed in the single-click mode, the arguments multipleSelection and clickMode should be consistent with each other. When clickMode is Table.SINGLE_CLICK, the argument multipleSelection is neglected.

After construction, the above code sets the following properties for the table:

In addition, the Table class provides the following methods to handle properties.


    public void allowMultipleSelections(boolean 
	                                allowMultipleSelections)
		   
    public void setClickMode(int m)

    public void setColors(Color headFG, Color headBG,
                          Color dataFG, Color dataBG,
			  Color revDataFG, Color revDataBG)

    public void setColors(int row, int col,
                          Color dataFG, Color dataBG,
			  Color revDataFG, Color revDataBG)

    public void setColumnColors(int col,
                                Color dataFG, Color dataBG,
				Color revDataFG, Color revDataBG)

    public void setRowColors(int row, 
                             Color dataFG, Color dataBG,
                             Color revDataFG, Color revDataBG)


    public void setFonts(Font fh, Font fd)

    public void setScale(int[] scaleArr)			     

		 

Browse the documentation for more details.

Event Handling

Following the JDK 1.1 delegation event model, crete add the following event listener classes handle events.

    // Listener to listen item events.
    class IL implements ItemListener 
    {
	public void itemStateChanged(ItemEvent ie) {
	    System.out.println(ie.toString());
	}
    }

    // Listener to listen action events.
    class AL implements ActionListener 
    {
	public void actionPerformed(ActionEvent ae) 
	    {
		System.out.println(ae.toString());
	    }
    }
		

You may add of the following methods to query for data over which the above events occured.


    public int getSelColumnIndex()

    public int getSelRowIndex()

    public Object[] getSelectedObjects()

    public boolean isElementSelected(int row, int col)

	    

See TableFrame.java to see how these are used to display the selection information in the status bar of the Demo.

Demo Source


Last modified: Fri Jun 18 11:49:31 India Standard Time 1999

© Subbu Allamaraju 1998, 1999. All rights reserved.

All copyrights and trademarks acknowledged.