Thanks in advance,
I'm trying to put a JCheckBox (and will add listener to it) into a JTable cell.
Can someone take a look at this and tell me where I'm going wrong?
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.table.*;
import javax.swing.event.*;
public class Test extends JFrame
{ Container cp = getContentPane();
JTable table = null;
Object columnNames[] = {"Col1", "Col2", "Col3", "Col4"};
Object cell[][] = null;
Test()
{
table = new JTable();
cell = new CheckboxRenderer[1][4];
for (int row=0; row < 1; ++row)
{
for (int col=0; col < 4; ++col)
{
cell[row][col] = new CheckboxRenderer(table, row, col);
table.setDefaultRenderer(
((JCheckBox)cell[row][col].getCheckbox()).getClass(),cell[row][col]);
}
}
cp.setLayout(new BorderLayout());
cp.add( table = new JTable(cell, columnNames), BorderLayout.CENTER);
pack();
setVisible(true);
}
public static void main(String args[]) { new Test(); }
}
class CheckboxRenderer extends JPanel implements TableCellRenderer
{
public JCheckBox cb = new JCheckBox("",false);
protected JTable table = null;
protected int row = 0;
protected int col = 0;
public JCheckBox getCheckbox() { return cb; }
CheckboxRenderer(JTable table, int row, int col)
{
this.table = table;
this.row = row;
this.col = col;
setLayout(new FlowLayout());
add(cb);
}
public Component getTableCellRendererComponent(JTable table,
Object value, boolean isSelected, boolean hasFocus,
int row, int column)
{
return this;
}
}
Michael Dunn - 29 Oct 2005 22:18 GMT
> Thanks in advance,
>
[quoted text clipped - 58 lines]
>
> }
is this what you're trying to do?
import java.awt.*;
import javax.swing.*;
import javax.swing.table.*;
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+1)+" Col 2",
"Row "+(x+1)+" Col 3","Row "+(x+1)+" Col 4"});
}
pack();
}
public static void main (String[] args){new Testing().setVisible(true);}
}
:-o - 30 Oct 2005 09:46 GMT
> is this what you're trying to do?
Yes, but I'd like the control to be a JCheckBox (to apply s listener to each).
> import java.awt.*;
> import javax.swing.*;
[quoted text clipped - 25 lines]
> public static void main (String[] args){new Testing().setVisible(true);}
> }
Michael Dunn - 31 Oct 2005 03:05 GMT
> 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);}
}