Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

JList List Selection Listener

Posted on 2003-11-18
3
Medium Priority
?
1,330 Views
Last Modified: 2012-05-04
Hello all,
I am working on a project for my java class that is GUI based. Its basically a bank application. There are two JLists on the GUI, one holds customers, and the other holds the accounts associated with the customer that is selected in the customer JList. I know I have to have the ListSelectionListener, and I need it on both of the JLists. I have to keep track of the current customer selected, and the current account that is selected. So I do need the listeners for both of these JLists correct? But when I have both of the JLists have listeners, I just got a bunch of dispatch errors and it looks like it selects an account item in some kind of loop and then terminates my program. I am confused. I have written the valueChanged(ListSelectionEvent e) as the following:

 public void valueChanged(ListSelectionEvent e)
   {
      Object selectedData = ((JList)e.getSource()).getSelectedValue();

      if(selectedData != null)
      {
        //Get the number of the customer selected in the list
        StringTokenizer st = new StringTokenizer(selectedData.toString());
        int custNum = Integer.parseInt(st.nextToken());

        //Set the current customer and show their accounts
        currentCustomer = theBank.getCustomer(custNum);

        accountList.setListData(currentCustomer.getTotalAccountInfo());
        accountList.setSelectedIndex(0);

        if(accountList.getModel().getSize() > 0)
        {
          //Get the account number of the account selected in the list
          StringTokenizer st2 = new StringTokenizer(accountList.getSelectedValue().toString());
          int acctNum = Integer.parseInt(st2.nextToken());
          currentAccount = currentCustomer.locateAccount(acctNum);
        }
        else
        {
           currentAccount = null;
        }
      }

      System.out.println(currentCustomer);
      System.out.println(currentAccount);
   }

So my problem is that I need to get the current selected account. The current customer part works fine, but when I added the account list listener, it went crazy. I can send the whole code as well, but I think the problem lies within this code.

Thanks in advance!
0
Comment
Question by:LBoogie
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
3 Comments
 
LVL 15

Accepted Solution

by:
jimmack earned 320 total points
ID: 9773425
If the same listener is used for both lists, you should do something like this:

if (e.getSource() == firstList)
{
   // processing when first list changes
}
else if (e.getSource() == secondList)
{
   // processing when second list changes
}
0
 
LVL 92

Expert Comment

by:objects
ID: 9774527
Or just use different listeners for easch list
0
 

Author Comment

by:LBoogie
ID: 9774887
jimmack,
That is what I needed. Thanks =)

0

Featured Post

The top UI technologies you need to be aware of

An important part of the job as a front-end developer is to stay up to date and in contact with new tools, trends and workflows. That’s why you cannot miss this upcoming webinar to explore the latest trends in UI technologies!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…
Suggested Courses

670 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question