You Could
Determine the source of the event and branch your logic based on the result...
public void valueChanged(ListSelectionEvent e) {
if (e.getSource() == ricList) {
//...
else if (e.getSource() == invList) {
//...
}
}
This assumes that you want to perform different logic for each source. This can become a little cumbersome if you have more than a few possible sources...
Instead of "implementing" ListSelectionListener
at the class level...
You Could...
Define a completely external class that does the specific job you want...
public class MyListSelectionListener implements ListSelectionListener {
public void valueChanged(ListSelectionEvent s) {
// Custom job here...
}
}
This can be a good and bad thing depending on your needs...One problem with this is you may be required to provide additional context to the class in order for it to perform it's operations, as the event object may not provide enough context alone
You Could
Create inner classes that do a specific job, in particular for the class they've been defined within...
public class Inbox {
//...
Inbox() {
//...
ricList.addListSelectionListener(new RicListSelectionListener());
//...
invList.addListSelectionListener(new InvListSelectionListener());
}
//...
public class RicListSelectionListener implements ListSelectionListener {
public void valueChanged(ListSelectionEvent s) {
String str = (String) ricList.getSelectedValue();
if(str.equals("Alex"))
System.out.println("Alex");
else if(str.equals("John"))
System.out.println("John");
else
System.out.println("Other name");
}
}
public class InvListSelectionListener implements ListSelectionListener {
public void valueChanged(ListSelectionEvent s) {
String str = (String) invList.getSelectedValue();
if(str.equals("Alex"))
System.out.println("Alex");
else if(str.equals("John"))
System.out.println("John");
else
System.out.println("Other name");
}
}
}
The nice thing about this is the classes have access to all the instance variables and methods within the parent class and are neat and tidy and easy to find when you need...
Take a look at Inner Class Example for more details
You Could...
Use anonymous classes. These are a little like inner classes, except they are defined in line...
public class Inbox {
//...
Inbox() {
//...
ricList.addListSelectionListener(new ListSelectionListener {
public void valueChanged(ListSelectionEvent s) {
String str = (String) ricList.getSelectedValue();
if(str.equals("Alex"))
System.out.println("Alex");
else if(str.equals("John"))
System.out.println("John");
else
System.out.println("Other name");
}
});
//...
invList.addListSelectionListener(new ListSelectionListener() {
public void valueChanged(ListSelectionEvent s) {
String str = (String) invList.getSelectedValue();
if(str.equals("Alex"))
System.out.println("Alex");
else if(str.equals("John"))
System.out.println("John");
else
System.out.println("Other name");
}
});
}
}
These can clutter the code if they are too long and should be used sparingly for that reason, but are really good for plumbing together existing functionality.
Take a look at Anonymous Classes for more details