Connecting Tech Pros Worldwide Help | Site Map

Help to solve flickering Checkbox in JTable

  #1  
Old July 17th, 2005, 11:32 PM
Ben Munday
Guest
 
Posts: 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);

  #2  
Old July 17th, 2005, 11:32 PM
hiwa
Guest
 
Posts: n/a

re: Help to solve flickering Checkbox in JTable


Ben Munday <benny111@hotmail.com> wrote in message news:<400613fd$1@duster.adelaide.on.net>...
[color=blue]
> http://www.users.on.net/benmunday/Label/[/color]
This URL is invalid.
[color=blue]
> public Component getTableCellRendererComponent(JTable table, Object
> cBox, boolean isSelected, boolean hasFocus, int row, int column)
> {
> Checkbox newCheckbox = (Checkbox)cBox;
> newCheckbox.setState(false);
> return newCheckbox;[/color]

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.
Closed Thread