By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
438,554 Members | 1,146 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 438,554 IT Pros & Developers. It's quick & easy.

Help to solve flickering Checkbox in JTable

P: n/a
Hi all,
I have a problem with a Checkbox in a JTable.
When i add the Checkbox into the JTable it displays the String
representation of the Checkbox, which is not what i want.
So, i created a CheckBoxRenderer which extens Checkbox and implements
TableCellRenderer. This seems to have done the job, in that the Checkbox
is displayed and not the String representation. But, and it's a big
'but', the column that holds the Checkbox continually updates the
fields. I cannot select the Checkbox and i can only just see that the
correct info for the Checkbox is in the cell.
I think it has something to do with repainting the Checkbox, but i can't
figure out how to stop it from happening.

Many thanx.
Ben

Here is some of the code, the bit's i think are relevent:
All code is at:
http://www.users.on.net/benmunday/Label/

public class CheckBoxRenderer extends Checkbox implements TableCellRenderer
{

....

public Component getTableCellRendererComponent(JTable table, Object
cBox, boolean isSelected, boolean hasFocus, int row, int column)
{
Checkbox newCheckbox = (Checkbox)cBox;
newCheckbox.setState(false);
return newCheckbox;
}
}

JTable itemDataTable = new JTable(new MyTableModel(data));
itemDataTable.setDefaultRenderer(data[0][0].getClass(),
new CheckBoxRenderer());

itemDataScrollPane.getViewport().setView(itemDataT able);
itemDataScrollPane.setVisible(true);

contentPane.add(itemDataScrollPane, BorderLayout.CENTER);

Jul 17 '05 #1
Share this Question
Share on Google+
1 Reply


P: n/a
Ben Munday <be******@hotmail.com> wrote in message news:<40********@duster.adelaide.on.net>...
http://www.users.on.net/benmunday/Label/ This URL is invalid.
public Component getTableCellRendererComponent(JTable table, Object
cBox, boolean isSelected, boolean hasFocus, int row, int column)
{
Checkbox newCheckbox = (Checkbox)cBox;
newCheckbox.setState(false);
return newCheckbox;


I believe you better install conditionals upon isSelected and
hasFocus in the method. Another thing is that JComponent uses
double buffering by default so I believe you should use JCheckBox
instead of awt.CheckBox. In general, mixing use of the awt
component and Swing component is not a good, let alone best,
practice.
Jul 17 '05 #2

This discussion thread is closed

Replies have been disabled for this discussion.