Article View: borland.public.jbuilder.java.swing
Article #9498Re: selected cell is not showed in a JTable
From: John P. McGrath
Date: Wed, 29 Mar 2000 00:00
Date: Wed, 29 Mar 2000 00:00
55 lines
2513 bytes
2513 bytes
On Tue, 28 Mar 2000 16:47:40 -0600, Fam. Moreno-Lehmann wrote:
> I have a labelCellRender for a JTable, because I want to display
> an imageIcon depending on something. Everything works fine except
> that when I select the cell, the selectionColor does not show,
> so you canοΏ½t see which cell is being selected.
:
:
> class labelCellRenderer extends JLabel implements TableCellRenderer {
> public labelCellRenderer() {
> super();
> }
> public Component getTableCellRendererComponent( JTable table,
> Object value, boolean isSelected, boolean hasFocus,
> int row, int column )
> {
> if(value==null) { return this;}
> if(value instanceof Detail) {
> Detail detail = (Detail)value;
> int level = detail.getLevel();
> String description = detail.getDescription();
> setText(description);
> this.setIcon(image[level]);}
> }
> }
Your CellRenderer is not doing anything with the "isSelected" argument,
which is what tells the renderer that it should render the cell as
selected. The normal thing to do here would be to set the background of
the label to the selection color.
It would also be normal to draw a focus indicator if the "hasFocus"
argument is true. Here, the normal thing is to draw a box around the
cell, using a dashed line.
But there is no need to mess with either the selection or the focus,
since this is already implemented in the DefaultTableCellRenderer class.
Try the following code, instead:
class labelCellRenderer extends DefaultTableCellRenderer {
protected void setValue(Object value) {
if (value instanceof Detail) {
Detail detail = (Detail) value;
int level = detail.getLevel();
String description = detail.getDescription();
setText(description);
setIcon(image[level]);
}
}
}
--
John P. McGrath
<JPMcGrath@fast.net>
<jpm@nscs.fast.net>
Technical Details
Message-ID:
<MPG.134b8d5c4e2cd59598983c@forums.borland.com>Path:
arc1-ca.pugleaf.net!unknown!not-for-mailReferences:
<38E1368C.A8C514BC@intercable.net>Show Full Headers
Complete Article Headers
X-Google-Language: ENGLISH,ASCII X-Google-Thread: 111d45,e9e741b43927d24a X-Google-Attributes: gid111d45,public From: John P. McGrath <JPMcGrath@fast.net> Subject: Re: selected cell is not showed in a JTable Date: 2000/03/29 Message-ID: <MPG.134b8d5c4e2cd59598983c@forums.borland.com> X-Deja-AN: 603782635 References: <38E1368C.A8C514BC@intercable.net> Organization: Another Netscape Collabra Server User Newsgroups: borland.public.jbuilder.java.swing