• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Table Row Selection problem while adding any Image in a JTable

 
buntha Choudhary
Ranch Hand
Posts: 136
Android Eclipse IDE Windows XP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 20611
63
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
buntha Choudhary
Ranch Hand
Posts: 136
Android Eclipse IDE Windows XP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
@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
Pie
Posts: 20611
63
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That depends on how you've implemented your renderer. Can you show us its fields and getTableRendererComponent method?
 
buntha Choudhary
Ranch Hand
Posts: 136
Android Eclipse IDE Windows XP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is my code snippet-


 
Rob Camick
Ranch Hand
Posts: 2613
9
  • Likes 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 20611
63
Chrome Eclipse IDE Java Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 136
Android Eclipse IDE Windows XP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
@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
Pie
Posts: 20611
63
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The renderer component also needs to be opaque.
 
buntha Choudhary
Ranch Hand
Posts: 136
Android Eclipse IDE Windows XP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks,

Solution I got by this way -

 
buntha Choudhary
Ranch Hand
Posts: 136
Android Eclipse IDE Windows XP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic