• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

JTable row selection behaviour

 
Aniruddha Pimpalkhare
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 15302
6
Chrome IntelliJ IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Mathews P Srampikal
Ranch Hand
Posts: 211
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
put this for your JTable
table.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
 
Aniruddha Pimpalkhare
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Define your own implementation of ListSelectionModel and ListSelectionListener used in JTable
 
Nathan Pruett
Bartender
Posts: 4121
IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"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!
 
Aniruddha Pimpalkhare
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic