Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Grid with checkboxes

Status
Not open for further replies.

Strannik

Programmer
Jul 4, 2002
132
0
0
UA
I need grid which displays checkboxes in the last column.I've found sample where TableCellRenderer and TableCellEditor interfaces are inherited.
Unfortunately this sample works if I use DefaultTableModel only. As soon as I changed model to my own checkboxes are not shown:

table = new MyTable();
table.setModel(new MyTableModel());

Are there any working samples with table model involved ?
 
The DefaultTableCellRenderer is a JLabel.
instead build a custom renderer:

public class YourRenderer extends JCheckBox implements TableCellRenderer {...}

implement getTableCellRendererComponent(...) and return
'this'.

If it's editable, you may wish to build a custom cell editor as well.
 
In your tableModel do you implement the method
Code:
public Class getColumnClass(int column)
to return a "class.Boolean" for that column ?
 
Thanks to all for help. I spent two days to investigate the issue and achieved something.

to hologram:

Yes, this is rather simple method to define col renderer/editor on the class level:

TR = new TextRenderer(true);
table.setDefaultRenderer(JTextArea.class, TR);

But I used another solution:

table.getColumnModel().getColumn(0).setCellRenderer(new MyBooleanRenderer());

This will allow to set custom renderer/editor for each column irrespectively of others.

But I ran across a problem and couldn't fix it. Just use another way. When I added new columns through table.getColumnModel().addColumn(column) all the columns displayed correctly.But getValueAt method of my table model was called for first column only(there was col = 0 passed only). And I had to define my own collection of columns to resolve situation.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top