Sagewire Logo

Help to solve flickering Checkbox in JTable

8 Message(s) by 5 Author(s) originally posted in java api


From: Ben Munday Date:   Wednesday, January 14, 2004
Hi all,
I've a problem with a Checkbox in a JTable.
When I add the Checkbox int o the JTable it displays the String
representation of the Checkbox, which isn't 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.

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(itemDataTable);
itemDataScrollPane.setVisible(true);

contentPane.add(itemDataScrollPane, BorderLayout.CENTER);


From: ak Date:   Wednesday, January 14, 2004
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.

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



lol, its because you call setState(false) from
getTableCellRendererComponent();

throw newCheckbox.setState(false); away then everything work.
--

____________

http://reader.image ro.com the best JAVA image reader.


From: Ben Munday Date:   Thursday, January 15, 2004
I commented out the section you suggested, but the flicker was still
there. what I have in that method now is:

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

wrote in message:
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.
public Component getTableCellRendererComponent(JTable table, Object
cBox, boolean isSelected, boolean hasFocus, int row, int column)
{
Checkbox newCheckbox = (Checkbox)cBox;
newCheckbox.setState(false);
return newCheckbox;
}
lol, its because you call setState(false) from
getTableCellRendererComponent();
throw newCheckbox.setState(false); away then everything work.
--
____________
http://reader.imagero.com the best JAVA image reader.





From: Ben Munday Date:   Thursday, January 15, 2004
The solution was to change all the references to Class Checkbox to Class
JCheckBox.

Firstly, I appreciate your willingness to answer a question in comunity
newsgroups.

Secondly, if you are going to present you answers as correct, make sure
they are. Did you make the changes in the code and then test it? Or did
you think that you had the right answer so you did not bother to make
sure.....

Ben

wrote in message:

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.
public Component getTableCellRendererComponent(JTable table, Object
cBox, boolean isSelected, boolean hasFocus, int row, int column)
{
Checkbox newCheckbox = (Checkbox)cBox;
newCheckbox.setState(false);
return newCheckbox;
}
lol, its because you call setState(false) from
getTableCellRendererComponent();
throw newCheckbox.setState(false); away then everything work.
--
____________
http://reader.imagero.com the best JAVA image reader.





From: Andrew Thompson Date:   Thursday, January 15, 2004
wrote in message


|...if you are going to present you answers as correct, make sure
| they are.

That's rich! You come in here posting code snippets
(useless), then want to complain about the advice
you get from ak!

Next time, post an SSCCE and you might
get better answers.
http://www.physci.org/codes/sscce.jsp

|..Did you make the changes in the code and then test it?

That was impossible, given your post.

If you don't like the answers you are
getting off this group, then..
a) Demand a full refund, and..
b) Ensure the door doesn't smack in the ass
on the way out..

--
Andrew Thompson
* http://www.PhySci.org/ PhySci software suite
* http://www.1point1C.org/ 1.1C - Superluminal!
* http://www.AThompson.info/andrew/ personal site


From: NetExplorer Date:   Sunday, February 29, 2004
I will just address the first part of your code. Are you truly using a
Checkbox from AWT within a JTable? Do not mix awt and swing component s if you
can help it.
Read up in the JAVA Tutorial (on sun's site) and study the java API (application programming interface)for the
TableCellRenderer interface:

public Component getTableCellRendererComponent(JTable table,
Object value,
boolean isSelected,
boolean hasFocus,
int row,
int column)Returns the
component used for drawing the cell. This method is used to configure the
renderer appropriately before drawing. Parameters:
table - the JTable that is asking the renderer to draw; can be null
value - the value of the cell to be rendered. It is up to the specific
renderer to interpret and draw the value. For example, if value is the
string "true", it could be rendered as a string or it could be rendered as a
check box that is checked. null is a valid value
isSelected - true if the cell is to be rendered with the selection
highlighted; otherwise false
hasFocus - if true, render cell appropriately. For example, put a
special border on the cell, if the cell can be edited, render in the color
used to indicate editing
row - the row index of the cell being drawn. When drawing the header,
the value of row is -1
column - the column index of the cell being drawn
Note that the Object being passed in isn't, as you've written, the
checkbox, but the value of the cell. So you will have to get this Object's
boolean value and then your renderer will use JCheckbox's setSelected( )
method to indicate whether it's checked or not.

Doing just this much research in the API (application programming interface)documents and tutorial will
probably give you the rest of the information you need, or lead you to the
answers you seek, which will likely include information about cell editors.> 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 isn't 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.
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(itemDataTable);
itemDataScrollPane.setVisible(true);
contentPane.add(itemDataScrollPane, BorderLayout.CENTER);


>


From: perry Date:   Saturday, May 01, 2004
what version of jdk are you using?

wrote in message:
Hi all,
I've 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 isn't 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.
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(itemDataTable);
itemDataScrollPane.setVisible(true);
contentPane.add(itemDataScrollPane, BorderLayout.CENTER);





From: Andrew Thompson Date:   Sunday, May 02, 2004
I've a problem with a Checkbox in a JTable.



It's a bad idea to mix AWT and Swing.
<http://www.physci.org/guifaq.jsp>

Speaking of bad ideas, (you seem to be on
a roll) please don't cross-post.
<http://www.physci.org/codes/JAVAfaq.jsp#xpost>
(note that two of the groups to which
you are posting aren't valid groups)

For valid groups, check here.
<http://www.physci.org/codes/JAVAfaq.jsp#groups>

F'Ups set to c.l.j.help..

--
Andrew Thompson
http://www.PhySci.org/ Open-source software suite
http://www.PhySci.org/codes/ Web & IT Help
http://www.1point1C.org/ Science & Technology



Next Message: Applet, send a string to an ip adress



Programming | Sports | Autos

copyright 2006
Valid XHTML 1.0 Transitional