Skip to Main Content

Java SE (Java Platform, Standard Edition)

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Booleans not rendering as JCheckBoxes in JTable

843806Mar 17 2008 — edited Mar 25 2008
I've read several posts on trying to get JCheckBoxes to render in a JTable, and I've looked at the Tables tutorial, but I still can't figure out why my JTable isn't rendering Booleans as check-boxes. :-(

I thought rendering check-boxes was the the default behavior for cells with Boolean values. This doesn't seem to be the case. I can add a cell editor of type JCheckBox by simply instantiating a new DefaultCellEditor, but adding a cell renderer doesn't look so easy (I believe I have to create my own implementation class). I was trying to avoid that, because a) I'm working in Servoy (which means I'm working in a Rhino editor and need to access my own custom classes as plugins or beans), and b) my Java skills are weak.

So can anybody see anything about my code that is obviously wrong? Or do I have to roll up my sleeves and write my own DefaultTableCellRenderer bean/plugin?

Here's my code (remember - it's javascript instances of java objects). (Note: I'm not sure exactly what kinds of objects JSDataSet.getAsTableModel() returns to the JTable model, but creating my own JTable with a vector of vectors of Boolean objects, or setting a Boolean in the cell after the fact doesn't work either.)

Any tips would be greatly appreciated.
// convert dataset into JTable
var table = elements.monitoring;
table.model = treatments.getAsTableModel();

// modify column headers
var columns = table.columnModel;
columns.getColumn(0).setHeaderValue('Quantity');
columns.getColumn(0).setPreferredWidth(60);
columns.getColumn(1).setHeaderValue('Repeats/hr.');
columns.getColumn(1).setPreferredWidth(75);
columns.getColumn(2).setHeaderValue('TREATMENT');
columns.getColumn(2).setPreferredWidth(225);

// start treatment times at 8am
var j=8;
for (i=3;i<44;++i) {
	var j_int = Packages.java.lang.Integer(j);
	columns.getColumn(i).setHeaderValue(j_int);
	columns.getColumn(i).setPreferredWidth(10);
	if (j == 12) {
		j=0;
	}
	++j;
}

// center cell data
var cell_class = Packages.java.lang.String;
var table_renderer = table.getDefaultRenderer(cell_class);
table_renderer.setHorizontalAlignment(0);

// add JTable to JScrollPane viewport (necessary to display headers)
var viewport = elements.monitoring_pane.viewport;
viewport.add(table);
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Apr 22 2008
Added on Mar 17 2008
14 comments
422 views