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
thanks
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
thanks
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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);
}
}
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
ASKER
how about the item listener?
Add the checkbox component to the cell as given in the example
and add your listener to that.
and add your listener to that.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
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(ne w ColumnHeaderListener());
MyItemListener myItemListener = new MyItemListener();
Enumeration enumeration = table.getColumnModel().get Columns();
while (enumeration.hasMoreElemen ts()) {
TableColumn aColumn = (TableColumn)enumeration.n extElement ();
aColumn.setHeaderRenderer( new CheckBoxHeader(myItemListe ner));
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
Checkbox header is create by:
JTableHeader header = table.getTableHeader();
header.addMouseListener(ne
MyItemListener myItemListener = new MyItemListener();
Enumeration enumeration = table.getColumnModel().get
while (enumeration.hasMoreElemen
TableColumn aColumn = (TableColumn)enumeration.n
aColumn.setHeaderRenderer(
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.
ASKER
i think i can use :
table.getModel().addTableM odelListen er(new MyTableModelListener(table ));
table.getModel().addTableM
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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().addTa bleModelLi stener(new MyTableModelListener());
all the best
R.K
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().addTa
all the best
R.K
ASKER
my problem on the tableName.getModel().addTa bleModelLi stener(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(ne w ColumnHeaderListener());
MyItemListener myItemListener = new MyItemListener();
table.getModel().addTableM odelListen er(new MyTableModelListener(table ));
Enumeration enumeration = table.getColumnModel().get Columns();
while (enumeration.hasMoreElemen ts()) {
customCheckbox = new CheckBoxHeader(myItemListe ner);
TableColumn aColumn = (TableColumn)enumeration.n extElement ();
aColumn.setHeaderRenderer( customChec kbox);
break;
}
thanks
JTableHeader header = table.getTableHeader();
header.addMouseListener(ne
MyItemListener myItemListener = new MyItemListener();
table.getModel().addTableM
Enumeration enumeration = table.getColumnModel().get
while (enumeration.hasMoreElemen
customCheckbox = new CheckBoxHeader(myItemListe
TableColumn aColumn = (TableColumn)enumeration.n
aColumn.setHeaderRenderer(
break;
}
thanks
ASKER
do i need to refresh the table?
For table data change..
((AbstractTableModel)table .getModel( )).fireTab leDataChan ged();
For structure change of table
((AbstractTableModel)table .getModel( )).fireTab leStructur eChanged() ;
((AbstractTableModel)table
For structure change of table
((AbstractTableModel)table
ASKER
just wonder why the customCheckbox.setSelected (false); will not set the checkbox to false.
public class CheckBoxHeader extends JCheckBox
public class CheckBoxHeader extends JCheckBox
ASKER
can i just refresh the table header?
thanks
thanks
You can't do that...
ASKER
can anyone tell me why the customCheckbox.setSelected (false); will not uncheck the checkbox?
thanks
thanks
Try calling the updateUI () method on the table.
ASKER
>> customCheckbox.updateUI();
still the same.
still the same.
ASKER
the checkbox will become uncheck when i click on the table header.
I said: >> on the table.
table.updateUI () ;
table.updateUI () ;
ASKER
how can i call the click table header function in my program?
thanks
thanks
ASKER
Sorry,
>> table.updateUI () ;
also the same.
>> table.updateUI () ;
also the same.
ASKER
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
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
That TableModel looks familiar :)
ASKER
i get my solution by repaint the panel........
:)
Thanks for accepting
best of luck..
R.K
R.K
or
??
Regards,
Muruga