EarthWeb
Developer.com
Site
windows 2000
visual c++
java
visual basic
javascripts
recommend it
 
Book
thinking in java
 
Interact
forum
guest book
jobs
jokes
what's new

share code
 
Resource
add resource
modify resource
new resource
 

[Internet Jobs]
-----
Java by E-mail:

Get the weekly e-mail highlights on Java!
-----

-

Selection of any Cell in a JTable


Author: Jan-Friedrich Mutter
Author's WebSite: http://www.bigfoot.com/~jmutter

The Swing JTable has only one ListSelectionModel for all columns. Thus is it not possible to select any and as many cell we want in a JTable. To make that possible, we have to extend the JTable and its BasicTableUI. To keep the information of the selection we have to write our own TabeleSelectionModel either. And if we want to keep track of changes of the selection we need to implement our own TableSelectionEvent and TableSelectionListener.

table

To use the new JTable we just have to create a new instance of it:


    DefaultTableModel dataModel = new DefaultTableModel(...); 
    AnySelectionTable table = new AnySelectionTable(dataModel); 

To keep track of changes we can add a listener to the TableSelectionModel which is now the default SelectionModel of the table:


    table.getTableSelectionModel().addTableSelectionListener(new MyTableSelectionListener()); 

where MyTableSelectionListener is our own implementation of the interface TableSelectionListener which comes with this distribution, e.g.:


    public class MyTableSelectionListener implements TableSelectionListener { 

        public void valueChanged(TableSelectionEvent e) { 
            TableSelectionModel tsm = (TableSelectionModel)(e.getSource()); 
            int column = e.getColumnIndex(); 
            int firstIndex = e.getFirstIndex(); 
            int lastIndex = e.getLastIndex(); 
            boolean isAdjusting = e.getValueIsAdjusting(); 
            String strValue = e.toString(); 
        } 
    } 

Notice that TableSelectionEvent is extended from ListSelectionEvent. Thus TableSelectionEvent.getColumnIndex() is the only new method in that class. It returns the column of the last new (de-)selected cell.

Please notice also that the order of the columns in the TableSelectionModel doesn't change even if the order in the JTable does.
To translate from JTable column index to TableSelectionModel column index you need to use the JTable's method convertColumnIndexToModel().

Here's a brief description of the source files:

SelTableMain.java
Creates an instance of an AnySelectionTable and defines a sample implementaion of a TableSelectionListener

AnySelectionTable.java
Provides the cell selection. The main purpose is to forward the isCellSelected() method to the TableSelectionModel

AnySelectionTableUI.java
Listens to mouseevents to update the TableSelectionModel

TableSelectionModel.java
Keeps the information of the current selection

TableSelectionEvent.java
An event that characterizes a change in the current selection

TableSelectionListener.java
Notified when a table selection value changes


Some notes:

The TableSelectionModel implements the PropertyChangeListener, ListSelectionListener and TableModelListener.

  • It is added to the Table as a PropertyChangeListener. It will be noticed when the Table has a new TableModel. So it can adjust itself to the new TableModel and can add itself as a TableModelListener to the new TableModel.
  • It is added to the TableModel as a TableModelListener to be noticed when the TableModel changes. The number of ListSelectionModels in the TableSelectionModel must be the same as the number of rows in the TableModel. If the number of columns changes it adds/removes the columns.
  • It is added as a ListSelectionListener to all its ListSelectionModels. If the TableSelectionModel changes one of its ListSelectionModels, the ListSelectionModel fires a new ListSelectionEvent which is received by TableSelectionModel. After receiving that event, the TableSelectionModel fires a TableSelectionEvent. That approach saves me from calculating 'firstIndex', 'lastIndex' and 'valueIsAdjusting' for myself and makes sure that a TableSelectionEvent is fired whenever the selection changes.

The code was extracted from my diploma thesis where I needed that kind of behaviour. The diploma thesis was assisted by the Institute of Computer Science, Programming and Software Engineering, Ludwig Maximilian University, Munich ( http://www.pst.informatik.uni-muenchen.de/ ) and the company software design & management ( http://www.sdm.de ).

Send me a postcard if you like this code.


Download source code

Posted On: 4-Aug-1999

internet.commerce