This week's book / license giveaways are in the JDBC and Relational Databases and Java in General forums.
We're giving away four copies each of PostGIS in Action and Java Advanced Topics Training and have the authors on-line!
See this thread and this one for details.
The moose likes Swing / AWT / SWT and the fly likes Switch Selection between 2 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 "Switch Selection between 2 JTable?" Watch "Switch Selection between 2 JTable?" New topic
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: 19968
    
  25

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 - OCEEJBD 6 - OCEJPAD 6
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: 19968
    
  25

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
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Switch Selection between 2 JTable?