Re: Help to solve flickering Checkbox in JTable

From: NetExplorer (someone_at_microsoft.com)
Date: 02/29/04


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);
>



Relevant Pages

  • Re: Search only values, then replace
    ... subordinate to the Formulas checkbox, though, so I didn't touch that. ... and enter your search string and replace string and ... whose *value* contains the search string, ... inside the cell with a replacement string if the cell doesn't contain ...
    (microsoft.public.excel)
  • Re: Dealing with Tables in VBA
    ... The only thing consistent about each cell ... column 2 is that there's a checkbox, a space, and the text "No Filter ... string is always the last line of the cell. ... Find a occurance of a text string "Data Filter Requirements" after ...
    (microsoft.public.word.vba.general)
  • Re: Dealing with Tables in VBA
    ... n-1 paragraphs in any cell in the second column of any table after the ... "No Filter Required" ... Anytime the checkbox next to "No FIlter Required" is checked, ... Find a occurance of a text string "Data Filter Requirements" ...
    (microsoft.public.word.vba.general)
  • Re: Dealing with Tables in VBA
    ... column 2 is that there's a checkbox, a space, and the text "No Filter ... the last line of the cell" ... string is always the last line of the cell. ... Find a occurance of a text string "Data Filter Requirements" after the ...
    (microsoft.public.word.vba.general)
  • Re: Dealing with Tables in VBA
    ... That is the end of cell marker and will be treated as a separate paragraph. ... However, if the checkbox that you are looking for is on the previous line, ... "No Filter Required" ... Find a occurance of a text string "Data Filter Requirements" ...
    (microsoft.public.word.vba.general)