Link to home
Start Free TrialLog in
Avatar of Kennywen
Kennywen

asked on

add check box and listener in table

I have a table and i want to add the checkbox and checkbox listener at the left hand side. But how can i add it?

thanks
Avatar of mmuruganandam
mmuruganandam
Flag of United States of America image

u want to add the checkbox within the table

or

??


Regards,
Muruga
Avatar of Kennywen
Kennywen

ASKER

yes, i want to add the checkbox within the table. (E.g. when user click on the particular checkbox then i will do something on the data for the specify row.)

thanks
ASKER CERTIFIED SOLUTION
Avatar of mmuruganandam
mmuruganandam
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Mick Barry
Just add Boolean's to a model something like:

public class MyTableModel extends DefaultTableModel
{
   public boolean isCellEditable(int row, int col)
   {
       return col==0;
   }

   public Class getColumnClass(int col)
   {
      return col==0 ? Boolean.class : super.getColumnClass(col);
   }

}
you'll also need to add a setValueAt() method
how about the item listener?
Add the checkbox component to the cell as given in the example
and add your listener to that.
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
currently i'm using boolean datatype to make the checkbox and i have a checkbox for the table header too.

Checkbox header is create by:
JTableHeader header = table.getTableHeader();  
header.addMouseListener(new ColumnHeaderListener());
MyItemListener myItemListener = new MyItemListener();
Enumeration enumeration = table.getColumnModel().getColumns();
while (enumeration.hasMoreElements()) {
      TableColumn aColumn = (TableColumn)enumeration.nextElement();
      aColumn.setHeaderRenderer(new CheckBoxHeader(myItemListener));
      break;
}

when the user click on the checkbox on the header then i will set all the checkbox in the table to become checked. but when the user uncheck all the checkbox on the table then how can i uncheck the checkbox on the table header.

thanks
you need to define a header cell editor.
i think i can use :
table.getModel().addTableModelListener(new MyTableModelListener(table));
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
The trick is to listen to the model of the table.

Table models fire events in the TableModelListener interface.

If you implement this interface, you can add a listener to the table model as follows:

  tableName.getModel().addTableModelListener(new MyTableModelListener());

all the best

R.K
my problem on the tableName.getModel().addTableModelListener(new MyTableModelListener()); is when i call customCheckbox.setSelected(false); the checkbox will not become uncheck but when i minimize and then maximize the window the checkbox will become uncheck. why?

JTableHeader header = table.getTableHeader();  
header.addMouseListener(new ColumnHeaderListener());
MyItemListener myItemListener = new MyItemListener();
table.getModel().addTableModelListener(new MyTableModelListener(table));
Enumeration enumeration = table.getColumnModel().getColumns();
while (enumeration.hasMoreElements()) {
      customCheckbox = new CheckBoxHeader(myItemListener);
      TableColumn aColumn = (TableColumn)enumeration.nextElement();
      aColumn.setHeaderRenderer(customCheckbox);
      break;
}

thanks
do i need to refresh the table?
For table data change..

((AbstractTableModel)table.getModel()).fireTableDataChanged();


For structure change of table

((AbstractTableModel)table.getModel()).fireTableStructureChanged() ;
just wonder why the customCheckbox.setSelected(false); will not set the checkbox to false.

public class CheckBoxHeader extends JCheckBox
can i just refresh the table header?

thanks
You can't do that...
can anyone tell me why the customCheckbox.setSelected(false); will not uncheck the checkbox?

thanks
Try calling the updateUI () method on the table.
>> customCheckbox.updateUI();

still the same.
the checkbox will become uncheck when i click on the table header.
I said: >> on the table.

table.updateUI () ;
how can i call the click table header function in my program?

thanks
Sorry,
>> table.updateUI () ;

also the same.
actually the checkbox is unchecked when i call customCheckbox.setSelected(false); the only problem is the tick graphic still in the checkbox. after i call customCheckbox.setSelected(false); the checkbox will become checked when i try to click on the checkbox. the only problem is the tick graphic....
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
That TableModel looks familiar :)
i get my solution by repaint the panel........
Thanks for accepting
best of luck..

R.K