Re: JCheckBox in JTable cell
- From: "Michael Dunn" <m_odunn@xxxxxxxxx>
- Date: 30 Oct 2005 18:05:52 -0800
> Yes, but I'd like the control to be a JCheckBox (to apply s listener to each).
It's not what you think - you don't add a component, it is just a
representation of a component (right term?)
adding a tableModelListener will do what you want
something like this
(a couple of lines will wrap)
import java.awt.*;
import javax.swing.*;
import javax.swing.table.*;
import javax.swing.event.*;
class Testing extends JFrame
{
String colNames[] = {"Col1", "Col2", "Col3", "Col4"};
Object[][] data = null;
DefaultTableModel dtm;
public Testing()
{
setLocation(400,100);
setDefaultCloseOperation(EXIT_ON_CLOSE);
dtm = new DefaultTableModel(data,colNames);
JTable table = new JTable(dtm);
JScrollPane sp = new JScrollPane(table);
TableColumn tc = table.getColumnModel().getColumn(0);
tc.setCellEditor(table.getDefaultEditor(Boolean.class));
tc.setCellRenderer(table.getDefaultRenderer(Boolean.class));
getContentPane().add(sp);
for(int x = 0; x < 5; x++)
{
dtm.addRow(new Object[]{new Boolean(false),"Row "+x+" Col 2",
"Row "+x+" Col 3","Row "+x+" Col 4"});
}
pack();
dtm.addTableModelListener(new TableModelListener(){
public void tableChanged(TableModelEvent tme) {
if (tme.getType() == TableModelEvent.UPDATE) {
int row = tme.getFirstRow();
int col = tme.getColumn();
if (col == 0){
JOptionPane.showMessageDialog(getContentPane(),"Checkbox at row "+
row+" = "+(((Boolean)dtm.getValueAt(row, col)).booleanValue()?
"checked":"UNchecked"));
}
}
}
});
}
public static void main (String[] args){new
Testing().setVisible(true);}
}
.
- References:
- JCheckBox in JTable cell
- From: :-o
- Re: JCheckBox in JTable cell
- From: Michael Dunn
- Re: JCheckBox in JTable cell
- From: :-o
- JCheckBox in JTable cell
- Prev by Date: Re: Help - Image icon
- Next by Date: Re: SWT: multiline tablecell
- Previous by thread: Re: JCheckBox in JTable cell
- Next by thread: Help - Image icon
- Index(es):
Relevant Pages
|