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!
-----

-

CheckBox in a TableHeader


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

table header

How to use the CheckBoxHeader ?


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

    //Create an ItemListener 
    MyItemListener myItemListener = new MyItemListener(); 
    //Create for each column a CheckBoxHeader 
    Enumeration enumeration = table.getColumnModel().getColumns(); 
    while (enumeration.hasMoreElements()) { 
        TableColumn aColumn = (TableColumn)enumeration.nextElement(); 
        aColumn.setHeaderRenderer(new CheckBoxHeader(myItemListener)); 
    } 




where MyItemListener implements the ItemListener interface, e.g.:

    class MyItemListener implements ItemListener { 
        public void itemStateChanged(ItemEvent e) { 
            String state = (e.getStateChange() == ItemEvent.SELECTED) ? "checked" : "unchecked"; 
            int column = ((CheckBoxHeader)(e.getItem())).getColumn(); 
            System.out.println(column + " - " + state); 
        } 
    } 
  

How does it work ?

The TableCellRenderer in a TableColumn doesn't receive any events by default. Thus, if we put a JCeckBox (or any other Component) into a TableCellRenderer it won't react on mouseevents. Unfortunately, a TableCellEditor is missing in Swing.

To perform an action on mouseevents we have to add a MouseListener to the Renderer and do a 'Click' to the JCheckBox for ourself.
Here are the important parts of the TableCellRenderer:


    public class CheckBoxHeader extends JCheckBox 
        implements TableCellRenderer, MouseListener { 

        public CheckBoxHeader(ItemListener itemListener) { 
            addItemListener(itemListener); 
        } 

        //implements TableCellRenderer 
        public Component getTableCellRendererComponent(...) { 
            ... 
            JTableHeader header = table.getTableHeader(); 
            header.addMouseListener(this); 
            return this; 
        } 

        //implements MouseListener 
        public void mouseClicked(MouseEvent e) { 
            ... 
            //JCheckBox method: 
            doClick(); 
        } 
    } 




Files

HeaderTableMain.java
creates a sample JTable and ItemListener. And puts the CheckBoxHeader to the JTable's Headers.

CheckBoxHeader.java
implements a TableCellRenderer, which is able to listen to mouseevents.


Some Notes

Dozens of mouseevents occur for only one mouse click. To perform only one 'Click', I had to write a workaround.

Implementing your own TableColumn.setHeaderEditor() could be another (better) approach.


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).


Download Files


Posted On: 4-Aug-1999

internet.commerce



Acceptable Use Policy


The Network for Technology Professionals

Search:

About Internet.com

Legal Notices, Licensing, Permissions, Privacy Policy.
Advertise | Newsletters | E-mail Offers