aspose file tools*
The moose likes Java in General and the fly likes ListSelectionEvent problems Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "ListSelectionEvent problems" Watch "ListSelectionEvent problems" New topic
Author

ListSelectionEvent problems

Colin McTaggart
Ranch Hand

Joined: Feb 01, 2011
Posts: 46
Hi all,
I have an app with two tables that both have list selection listeners applied to them. I am able to fire of an event when I select an item from either table but I am not able to identify which table fired the event.

I need to use one of two different methods depending on which table the user selected an item from. When I try to use .getSource() it returns javax.swing.DefaultListSelectionModel instead of the name of the table as I expected.

Something else I dont understand is that the event is fired once for every item in the table rather than just one time.

Can anyone help my understand where I am going wrong?
Ralph Cook
Ranch Hand

Joined: May 29, 2005
Posts: 479
There's not quite enough information here to be of real help to you, I'm afraid.

You say you have "two tables that both have list selection listeners applied to them". This sounds like you have one listener listening to one table, and another listener applied to the other table. But if that were true, you would have NO problem figuring out which table was selected, so I'm just confused by your information. I could make some guesses, but there's only a small chance I'd guess correctly.

getSource is going to return a reference to the source; it appears that you have somehow obtained the name of the class of that source. If you want to use getSource to determine where the event came from, then, when you create the list, keep a reference to the list object in your code at a place where you can test it in the listener; you can use

Object obj = getSource();
if (obj == listReference)

to see if the source is that particular reference.

Your third sort-of question is not developed enough; under what circumstances do you get an event fired for "every item in the table".

rc
Rob Camick
Ranch Hand

Joined: Jun 13, 2009
Posts: 2215
    
    7
it returns javax.swing.DefaultListSelectionModel instead of the name of the table as I expected.


Yes the source is the model NOT the component the model belongs to because models can be shared by multiple components which appears to be what you are doing here.

Since you need to do different processing for each table you need to have different listeners for each table.
Colin McTaggart
Ranch Hand

Joined: Feb 01, 2011
Posts: 46
So does this mean I need to use different models for the tables? If so is there a good tutorial available anywhere as I'm having a hard time understanding tables :S
Colin McTaggart
Ranch Hand

Joined: Feb 01, 2011
Posts: 46
if it helps here is the code I use to create my tables:



In both cases index is a two dimensional array where the first value is a String and the second value is a Boolean. Here is the code for my custom cell renderer:



Also to clarify my second problem, I put System.out.print(e.getSource()+"\n"); at the start of my valuechaged method so that it would show me the event source whenever I select something from a table, however instaid of showing the source just once it shows it once for every item in the table, so if I have 20 items in the table it shows the source 20 times in the console. Can anyone see where I am going wrong?
Rob Camick
Ranch Hand

Joined: Jun 13, 2009
Posts: 2215
    
    7
So does this mean I need to use different models for the tables?


What did my answer say?

at the start of my valuechaged method


The code you posted doesn't even include a valueChanged method. Post your SSCCE that demonstrates the problem.
Colin McTaggart
Ranch Hand

Joined: Feb 01, 2011
Posts: 46
your answer said I have to have different listeners for each table, I simply asked if I did this by using different models for each table, I am a beginner and apologize if I have gaps in my understanding, that's why I'm asking for help.

Here is my ValueChanged method as it stands, I can write the rest when I can differentiate the source.



as it stands it outputs "I can't tell " once for every item on the table. I need it to output either "it was the restore table" or "it was the restore table" once.

As for creating an example I'll try and do it as quickly as I can but be aware that I code in netbeans and so its wysiwyg gui code will be included.
Martin Vajsar
Sheriff

Joined: Aug 22, 2010
Posts: 3611
    
  60

Colin,

just a hint, thought this is not what you asked about: if you redefine the getColumnClass() method of your DefaultTableModel subclass to return String and Boolean for first and second columns respectively, you can remove your CustomCellRenderer. JTable will render checkboxes and handle their editing automatically. The default implementation is more efficient actually, as it does not create a new instance of checkbox for every rendered cell.

As for your question: you already have two distinct instances of table model. You can however create two implementations of listener and use each for just a single table. That way you don't need to determine in the valueChanged() method which table the event came from.
Colin McTaggart
Ranch Hand

Joined: Feb 01, 2011
Posts: 46
How do I do that? I begin my class with:



How do I change this to implement a second ListSelectionListener? Or is there a better way to implement a listener? This is the way I learned at Uni...
Rob Camick
Ranch Hand

Joined: Jun 13, 2009
Posts: 2215
    
    7
The following code was taken from the TableFilterDemo example from the Swing tutorial on How to Use Tables:



You could also do it like this:



These are examples of creaing an annonymous inner class. This way each table has a custom listener and you can invoke custom code for each specific table.
Colin McTaggart
Ranch Hand

Joined: Feb 01, 2011
Posts: 46
Thanks for all the help guys, I'll try applying this stuff tonight and post results tomorrow
Colin McTaggart
Ranch Hand

Joined: Feb 01, 2011
Posts: 46
Success, I have managed to get both tables performing different methods properly so thanks muchly .

As for my second problem I think it may be caused by this:


Martin Vajsar wrote:
just a hint, thought this is not what you asked about: if you redefine the getColumnClass() method of your DefaultTableModel subclass to return String and Boolean for first and second columns respectively, you can remove your CustomCellRenderer. JTable will render checkboxes and handle their editing automatically. The default implementation is more efficient actually, as it does not create a new instance of checkbox for every rendered cell.
.

Just so I know what I'm trying to do, do you mean that I need to create my own custom getColumnClass and extend it in order to customize it? I really need to sort this as it seems to be causing a big hit on performance.
Martin Vajsar
Sheriff

Joined: Aug 22, 2010
Posts: 3611
    
  60

Colin McTaggart wrote:Just so I know what I'm trying to do, do you mean that I need to create my own custom getColumnClass and extend it in order to customize it? I really need to sort this as it seems to be causing a big hit on performance.

getColumnClass is a method in DefaultTableModel class. You're already overriding isCellEditable methods in your code, so you'll add the getColumnClass method to the same place and stop calling setRenderer for your columns. See the documentation for DefaultTableModel and maybe this tutorial.
Colin McTaggart
Ranch Hand

Joined: Feb 01, 2011
Posts: 46
Thanks for clarifying, when I wrote this code originally I had help, I have a much better idea of whats going on now
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: ListSelectionEvent problems