File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Swing / AWT / SWT and the fly likes JTable row selection behaviour Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "JTable row selection behaviour" Watch "JTable row selection behaviour" New topic
Author

JTable row selection behaviour

Aniruddha Pimpalkhare
Greenhorn

Joined: Dec 20, 2001
Posts: 4
Hi
By default, user can select multiple rows in a JTable by using shift+click / control+click or simple dragging.
One of our user is interested in changing this behaviour. He does not want to touch the keyboard while selecting multiple rows.
What he wants is :
1. after clicking, say, 3rd row it should get selected --- default JTable behaviour.
2. then if he clicks 7th row, both original selection (3rd row) and 7th row should remain selected and so on. (by default 3rd row is unselected and 7th becomes selected)
Please advice. Thank you.
Gregg Bolinger
GenRocket Founder
Ranch Hand

Joined: Jul 11, 2001
Posts: 15286
    
    6

gotha,
Welcome to Javaranch! Looks like you might have skipped over one of the very few rules we have here when creating your account.
We have a Naming Policy here at Javaranch that all our users must adhere to. Please adjust your display name accordingly.
You can change your display name here
Thanks.


GenRocket - A Test Data Generation Platform
Mathews P Srampikal
Ranch Hand

Joined: Nov 26, 2002
Posts: 211
put this for your JTable
table.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);


Thanks,
Mathews
Aniruddha Pimpalkhare
Greenhorn

Joined: Dec 20, 2001
Posts: 4
Hi Mathews,
SINGLE_SELECTION means we can not select more than one row.
So when we click on some row, original selection will go away.
What user wants is: every time he clicks on a new row (without pressing control or shift ), original selection PLUS the new row should remain selected.
For example:
0) no rows are selected
1) user clicks on 3rd row (shift or control key NOT touched): 3rd row gets selected
2) user clicks on 7th row (shift or control key NOT touched): 3rd row remains selected, 7th row also gets selected
3) user selects an already selected row: that row gets un-selected all others remain selected
thanks
Ani
[ March 15, 2004: Message edited by: Ani ]
Rajan Punchouty
Greenhorn

Joined: Mar 15, 2004
Posts: 21
Define your own implementation of ListSelectionModel and ListSelectionListener used in JTable
Nathan Pruett
Bartender

Joined: Oct 18, 2000
Posts: 4121

"Jesus Christ" & "Ani" -

Welcome to the JavaRanch! Please adjust your displayed name to meet the
JavaRanch Naming Policy.
You can change it here.

Thanks! and welcome to the JavaRanch!


-Nate
Write once, run anywhere, because there's nowhere to hide! - /. A.C.
Aniruddha Pimpalkhare
Greenhorn

Joined: Dec 20, 2001
Posts: 4
Thanks a lot. That helped a lot. Below seems to work:
// Create a new table instance
table.setSelectionModel(new MyDefaultListSelectionModel ());
// where MyDefaultListSelectionModel looks as below
import javax.swing.DefaultListSelectionModel;
public class MyDefaultListSelectionModel extends DefaultListSelectionModel {
public MyDefaultListSelectionModel (){
super();
}
public void setSelectionInterval(int index0, int index1) {
if (isSelectedIndex(index0)) {
super.removeSelectionInterval(index0, index1);
}
else {
super.addSelectionInterval(index0, index1);
}
}
}
[ March 17, 2004: Message edited by: Ani ]
 
Consider Paul's rocket mass heater.
 
subject: JTable row selection behaviour
 
Similar Threads
JTable selection problem
JTable selection problem
JTable
JTable
Jtable Event Handling