[Swing] JTable: how to change properties of a component located in acell

H

Hole

Hi all,


I would like to get the reference of a component (in my case is a
JCheckbox) and call the setVisible method on it.
I tried to do it with getTableCellEditorComponent/
getTableCellRendererComponent but it doesn't work properly: it seems
that the property is being set on every component of the column.
Thanks in advance.

Below, the code I used:

for (int i=1; i<model.getRowCount();i++) {
Component check = siteResultsTbl.getCellEditor(i,
0).getTableCellEditorComponent(siteResultsTbl, null, false, i, 0);

Long vid1=(Long)model.getValueAt(i-1, 2);
Long vid2=(Long)model.getValueAt(i, 2);
System.out.println(vid1+" "+vid2);
if (vid1.equals(vid2)) {
check.setVisible(false);

System.out.println("check at "+i+" should be not
visible");

}
}
 
J

John B. Matthews

Hole said:
I would like to get the reference of a component (in my case is a
JCheckbox) and call the setVisible method on it. I tried to do it
with getTableCellEditorComponent/ getTableCellRendererComponent but
it doesn't work properly: it seems that the property is being set on
every component of the column.

I think this is expected. You may want to review the tutorial's
"Concepts: Editors and Renderers" for more detail:

<http://java.sun.com/docs/books/tutorial/uiswing/components/table.html
#editrender>

Instead, why not just query the JTable's data model?
 
H

Hole

 Hole said:
I think this is expected. You may want to review the tutorial's
"Concepts: Editors and Renderers" for more detail:

<http://java.sun.com/docs/books/tutorial/uiswing/components/table.html
#editrender>

Well, I guess I need it, even if I thought there was a direct way to
obtain the rendered widget's reference.
Instead, why not just query the JTable's data model?

If I query data model, I get the value of the column (a boolean false
or true) while I cannot get the single component rendered in the cell
(to call the setVisible() method), can I?
I need to set properties on rendered widgets on a cell basis, under
certain conditions found in other cells (in previous rows).
 
J

John B. Matthews

Hole said:
table.html#editrender>

Well, I guess I need it, even if I thought there was a direct way to
obtain the rendered widget's reference.

Yes. In particular, there is no "rendered widget". As explained in the
tutorial, "Swing tables are implemented differently."
Instead, why not just query the JTable's data model?
If I query [the] data model, I get the value of the column (a boolean
false or true) while I cannot get the single component rendered in
the cell (to call the setVisible() method), can I?

As noted in the tutorial, there is no individual component for each
cell; there is a component that is used to render each cell in a column
based on the value obtained from the data model for each row in that
column.
I need to set properties on rendered widgets on a cell basis, under
certain conditions found in other cells (in previous rows).

If the values in a column have dependencies, adjust them in your data
model's setValueAt() method.

If the appearance of cells in a column have dependencies, implement your
own renderer, giving it knowledge of the those dependencies.

If you extend AbstractTableModel, as outlined in the tutorial, you can
fire the appropriate notification to update any dependent cells. In that
way, inter-dependent cells will be updated automatically when any one is
changed.

As you haven't specified the dependencies, an SSCCE would be helpful:

<http://pscode.org/sscce.html>
 
H

Hole

If the values in a column have dependencies, adjust them in your data
model's setValueAt() method.

If the appearance of cells in a column have dependencies, implement your
own renderer, giving it knowledge of the those dependencies.

If you extend AbstractTableModel, as outlined in the tutorial, you can
fire the appropriate notification to update any dependent cells. In that
way, inter-dependent cells will be updated automatically when any one is
changed.

Many thanks, John.
At the end I've realized how the renderer works and tried to implement
my own.
Now it "works" but:

1. setVisible on checkbox doesn't work (but with setEnabled(false) and
setOpaque(true) got sense)
2. when you sort the table the relation is being lost, since the not
enabled checkbox cells don't move togheter with the others.

Actually, what I really need is a "rowspannable" JTable - where one
checkbox cell is shared with rows having a same value in a cell
- ...with a correct behaviour for sorting (I've found this SWT
component http://agilegrid.sourceforge.net/ but I use Swing and I
don't know it it's possible to use Swing and SWT togheter without
conflicts).
Also, I've found this tutorial: http://www.swingwiki.org/howto:column_spanning
that could be useful.


Now I'll check if implementing an AbstractTableModel works for me.


Thanks again for your clear answer!!!
 
J

John B. Matthews

Hole said:
At the end I've realized how the renderer works and tried to
implement my own. Now it "works" but:

1. setVisible on checkbox doesn't work (but with setEnabled(false) and
setOpaque(true) got sense)

I see this. Alternatively, one may return any desired component, even
null, to achieve various effects.
2. when you sort the table the relation is being lost, since the not
enabled checkbox cells don't move togheter with the others.

I often sort in the data model, despite the required coordinate
transformation, using a custom Comparator; but I see that Java 1.6 added
setRowSorter() to JTable, and DefaultRowSorter permits setComparator().

[Interesting span examples...]
Now I'll check if implementing an AbstractTableModel works for me.

Recalling a time when I was somewhat recalcitrant in this area, I can
only advocate it, now.
Thanks again for your clear answer!!!

You're welcome.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,578
Members
45,052
Latest member
LucyCarper

Latest Threads

Top