Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Switch Selection between 2 JTable?

 
Ahmed Aziz
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello,

assume that i have 2 JTables in JPanel and each contain one row is it possible to switch selection between these tables

in other words if i select the first table then after that i select the second table the first table will be unselected

this is my attempt but not like what i need

i need to switch selection between the two different tables here in the code when i select one table then select the other table the first table remains selected



any help please !!!
 
Rob Spoor
Sheriff
Pie
Posts: 20550
57
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Add ListSelectionListeners to the JTables through the object returned by getSelectionModel(). When a row is selected in one table you are then notified, and you can clear the selection in the second, and vice versa.
 
Ahmed Aziz
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you Rop Prime

i try with this code but still not what i need
 
Ahmed Aziz
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
any help please?
 
Rob Spoor
Sheriff
Pie
Posts: 20550
57
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I've done a bit of investigation, and the problem is that clearing the selection also triggers an event. So when you select something in the second table the following events are triggered:
1) selection of data in table 2, with getValueIsAdjusting() returning true
2) de-selection of data in table 1, with getValueIsAdjusting() returning false; this event is caused by the previous one
3) de-selection of data in table 2, with getValueIsAdjusting() returning true; this event is caused by the previous one
4) selection of data in table 2, with getValueIsAdjusting() returning false; this event matches the first event

The problem here is that event 2 triggers another event, clearing the selection of table 2. You will need to block this event somehow. I usually use a simple boolean for this. In a short hack based on your code (stripped from the renderers):
 
Ahmed Aziz
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you very much Rob

you solved my problem

Thanks

best wishes
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic