This week's book giveaway is in the Mac OS forum.
We're giving away four copies of a choice of "Take Control of Upgrading to Yosemite" or "Take Control of Automating Your Mac" and have Joe Kissell on-line!
See this thread for details.
The moose likes Swing / AWT / SWT and the fly likes Table Row Selection problem while adding any Image in a JTable Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Table Row Selection problem while adding any Image in a JTable" Watch "Table Row Selection problem while adding any Image in a JTable" New topic
Author

Table Row Selection problem while adding any Image in a JTable

buntha Choudhary
Ranch Hand

Joined: Jul 03, 2009
Posts: 136

I am adding an Image in JTable

through the above logic but when I try to select the row it is not selecting this particular column.

For example My table has 4 Columns and the image is being set in Column 4th.
Then on selecting any Row , default 3 columns are getting selected or highlighted and the 4th Column is not getting highlighted.
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19697
    
  20

Are you sure it's not being selected? The visual selection is part of the renderer. DefaultTableCellRenderer renders the value with a different colour if the cell is selected; that's what the isSelected parameter indicates. If your renderer does not use a different colour if isSelected is true then the cell will be selected, just not look selected.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
buntha Choudhary
Ranch Hand

Joined: Jul 03, 2009
Posts: 136

@Rob Spoor
Thanks for the input but is there no way to make the look identical for the entire row like SWT wizard where entire row shows highlighted blue color.
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19697
    
  20

That depends on how you've implemented your renderer. Can you show us its fields and getTableRendererComponent method?
buntha Choudhary
Ranch Hand

Joined: Jul 03, 2009
Posts: 136

This is my code snippet-


Rob Camick
Ranch Hand

Joined: Jun 13, 2009
Posts: 2191
    
    7
Don't create a custom renderer. JTable already has a renderer to display images.

Just override the getColumnClass(....) method to return Icon.class and the Icon renderer will be used.
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19697
    
  20

And if you want to keep your class because of the MouseListener, all you need to do is change the background colour inside the getTableCellRendererComponent method. Take a look at the source of DefaultTableCellRenderer (inside the src.zip file in your JDK root directory) for how you could do that.
buntha Choudhary
Ranch Hand

Joined: Jul 03, 2009
Posts: 136

@Rob..

I tried to change the code as suggested by you ..
So I added rendererLabel.setBackground(Color.BLUE); inside renderer getter method but I am not getting the expected.
On looking over the source code-


setBackground was the method need to be changed or I am somewhere wrong.
Please suggest.


Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19697
    
  20

The renderer component also needs to be opaque.
buntha Choudhary
Ranch Hand

Joined: Jul 03, 2009
Posts: 136

Thanks,

Solution I got by this way -

buntha Choudhary
Ranch Hand

Joined: Jul 03, 2009
Posts: 136

Now everything as expected is working fine.. but I realized a thing while working on the same ..
Actually when the JTable window opens and if I click directly on the JLabel (image) based Column , then the image is gone for a while and the table returns the selected row as -1.
One more point is that the Row selection as well restricted to the previous column.

Precisely , If my Table has 4 column and Image(JLabel) is on the column number 4th, then if I directly click on the image or column 4th , then the row selection happens till the 3rd column and it returns the row selection as -1.
But If I select any other column, the everything is proper and works fine.


I am still using the same logic what I mentioned previously. What is the mistake I am committing.
 
GeeCON Prague 2014
 
subject: Table Row Selection problem while adding any Image in a JTable