JavaRanch » Java Forums »
Java »
Swing / AWT / SWT
| Author |
Switch Selection between 2 JTable?
|
Ahmed Aziz
Greenhorn
Joined: Jan 05, 2010
Posts: 4
|
|
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
Joined: Oct 27, 2005
Posts: 19216
|
|
|
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.
|
SCJP 1.4 - SCJP 6 - SCWCD 5
How To Ask Questions How To Answer Questions
|
 |
Ahmed Aziz
Greenhorn
Joined: Jan 05, 2010
Posts: 4
|
|
Thank you Rop Prime
i try with this code but still not what i need
|
 |
Ahmed Aziz
Greenhorn
Joined: Jan 05, 2010
Posts: 4
|
|
any help please?
|
 |
Rob Spoor
Sheriff
Joined: Oct 27, 2005
Posts: 19216
|
|
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
Joined: Jan 05, 2010
Posts: 4
|
|
Thank you very much Rob
you solved my problem
Thanks
best wishes
|
 |
 |
|
|
subject: Switch Selection between 2 JTable?
|
|
|
|