Re: JCheckBox in JTable cell



> 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);}
}

.



Relevant Pages

  • Re: Swing window layouts
    ... how can i go about precisely and per-pixel setting up and ... class testing extends JFrame ... public static void main ...
    (comp.lang.java.help)
  • Re: How to scroll to element of list?
    ... > specific index at start-up. ... class Testing extends JFrame ... DefaultListModel lm = new DefaultListModel; ... public static void main ...
    (comp.lang.java.gui)
  • Re: Grid layout
    ... > I am having trouble with putting simple GUI on my desktop. ... class Testing extends JFrame ... public static void main ...
    (comp.lang.java.gui)
  • Re: Jslider problem
    ... >class Testing extends JFrame ... > JPanel jp = new JPanel; ... > public static void main{new ... Prev by Date: ...
    (comp.lang.java.gui)
  • Re: JCheckBox in JTable cell
    ... but I'd like the control to be a JCheckBox (to apply s listener to each). ... > class Testing extends JFrame ... > DefaultTableModel dtm; ...
    (comp.lang.java.gui)