• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Using ListSelectionListener

 
Ranch Hand
Posts: 377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am trying to write a selection listener and am not understanding something. I have a JTable with a selection model set to single selection. My objective is to add the selected row to an ArrayList but at this point I am just printing out values to understand what I am getting. Here is my listener:

The first problem is that it seems to get invoked twice for each selection. That is, one click results in 2 identical lines of output. After clicking one row, then clicking another I get the original selection listed once plus the new one listed twice. Can someone explain when the listener is invoked and if it is invoked multiple times per click how I can identify the first invocation and ignore the rest? TIA.
 
Bartender
Posts: 5167
11
Netbeans IDE Opera Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Print the values returned by e.getFirstIndex(), e.getLastIndex() and e.getValueIsAdjusting() and you'll gain a better understanding of the selection change notification process.
 
Dennis Putnam
Ranch Hand
Posts: 377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for the reply. Two things happened when I did that. The first is that the double invocation per click stopped (gremlins?). The second showed I really have a different problem. Apparently setting mode to SINGLE_SELECTION is not working or I am not understanding what it does. How do I set the mode so I get one index per click (i.e firstIndex=lastIndex every time not just the first time), if not with SINGLE_SELECTION?
 
Bartender
Posts: 1104
10
Netbeans IDE Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You did not print the e.getValueIsAdjusting() as suggested. Print it outside of the if and you will get an idea.
 
Dennis Putnam
Ranch Hand
Posts: 377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I did but I don't see how that changes my question, I still cannot get SINGLE_SELECTION to work as expected.
 
Darryl Burke
Bartender
Posts: 5167
11
Netbeans IDE Opera Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Post an SSCCE and report what / where you clicked and what was printed to the console and we'll take it from there.

I still cannot get SINGLE_SELECTION to work as expected.


Then getExpectationIsAdjusting() should return true.
 
Dennis Putnam
Ranch Hand
Posts: 377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What is getExpectationIsAdjusting()?
 
Marshal
Posts: 28226
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Darryl missed the I think. If your code isn't doing what you expected, then you have to adjust your expectation before you can fix the code.
 
Dennis Putnam
Ranch Hand
Posts: 377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Duh! I missed that.
 
Dennis Putnam
Ranch Hand
Posts: 377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
On my way to looking up other things I finally figured this out. I mean a solution not necessarily the answer to my question. All I needed to do was clear the selection in the listener, then each invocation of the listener has a single selection.
[CODE=Java]
table.getSelectionModel().addListSelectionListener(new ListSelectionListener() {

@Override
public void valueChanged(ListSelectionEvent e) {
if (e.getValueIsAdjusting()) {
int row=e.getFirstIndex();
JTable table=ListActors.getTable();
System.out.println(table.getValueAt(row, 0)+" "+table.getValueAt(row, 1)+" "+table.getValueAt(row, 2)+" "+table.getValueAt(row, 3)+" "+table.getValueAt(row, 4));
table.getSelectionModel().clearSelection();
}
}
});
 
Dennis Putnam
Ranch Hand
Posts: 377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
On my way to looking up other things I finally figured this out. I mean a solution not necessarily the answer to my question. All I needed to do was clear the selection in the listener, then each invocation of the listener has a single selection.
 
Ranganathan Kaliyur Mannar
Bartender
Posts: 1104
10
Netbeans IDE Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
I don't think that is the right solution. You are trying to clear the selection inside a selection listener which I think would fire selection events all over again.

A simpler solution would be to use getSelectedRow() in conjunction with e.getValueIsAdjusting(), like:

The table.getSelectedRow() correctly returns the currently selected row (for SINGLE_SELECTION). You can make use of the firstIndex and lastIndex values if you need to do some processing on 'deselecting' an earlier row.
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic