Re: Help to solve flickering Checkbox in JTable
From: NetExplorer (someone_at_microsoft.com)
Date: 02/29/04
- Next message: NetExplorer: "Re: Dynamic updates in a Component"
- Previous message: Freeman Ding: "How to use "Ctrl+Tab" key to switch between JInternalFrames"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Date: Sun, 29 Feb 2004 08:49:29 GMT
I'll just address the first part of your code. Are you truly using a
Checkbox from AWT within a JTable? Don't mix awt and swing components if you
can help it.
Read up in the Java Tutorial (on sun's site) and study the java api 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 is not, as you have written, the
checkbox, but the value of the cell. So you'll 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 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.
"Ben Munday" <benny111@hotmail.com> wrote in message
news:400613be$1@duster.adelaide.on.net...
> 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.
>
> 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);
>
- Next message: NetExplorer: "Re: Dynamic updates in a Component"
- Previous message: Freeman Ding: "How to use "Ctrl+Tab" key to switch between JInternalFrames"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Relevant Pages
|