JCheckBox in JTable cell
- From: ":-o" <fcrutch@xxxxxxxxxxxx>
- Date: Sat, 29 Oct 2005 16:48:50 -0400
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;
}
}
.
- Follow-Ups:
- Re: JCheckBox in JTable cell
- From: Michael Dunn
- Re: JCheckBox in JTable cell
- Prev by Date: Re: Swing Dockable Windows
- Next by Date: Re: JCheckBox in JTable cell
- Previous by thread: panel-color question..
- Next by thread: Re: JCheckBox in JTable cell
- Index(es):
Relevant Pages
|