By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,740 Members | 844 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,740 IT Pros & Developers. It's quick & easy.

JList - first element won't work

Nepomuk
Expert 2.5K+
P: 3,112
Hi!
I'm trying to use a JList. Here's the code:
Expand|Select|Wrap|Line Numbers
  1. String[] languages = {
  2.     "deutsch",        // german
  3.     "english",        // english
  4.     "castellano",    // castilian
  5.     "français",    // french
  6.     "italiano",    // italian
  7.     "日本語",            // japanese
  8.     "한국어한국어한국어한국어",            // korean
  9.     "svensk",        // swedish
  10.     "汉语",            // chinese (simplified)
  11.     "漢語"};            // chinese (traditional/taiwan)
  12. JList languageList = new JList(languages);
  13. languageList.setAlignmentX(0.5f);
  14. languageList.addListSelectionListener(new ListSelectionListener()
  15. {
  16.     public String chosenLanguage = "";
  17.  
  18.     public void valueChanged(ListSelectionEvent e) 
  19.     {
  20.         System.out.println("before: " + chosenLanguage);
  21.         switch(e.getLastIndex())
  22.         {
  23.              case 0 :
  24.                 chosenLanguage = "de";
  25.                 break;
  26.             case 1 :
  27.                 chosenLanguage = "en";
  28.                 break;
  29.             case 2 :
  30.                 chosenLanguage = "es";
  31.                 break;
  32.             case 3 :
  33.                 chosenLanguage = "fr";
  34.                 break;
  35.             case 4 :
  36.                 chosenLanguage = "it";
  37.                 break;
  38.             case 5 :
  39.                 chosenLanguage = "ja";
  40.                 break;
  41.             case 6 :
  42.                 chosenLanguage = "ko";
  43.                 break;
  44.             case 7 :
  45.                 chosenLanguage = "sv";
  46.                 break;
  47.             case 8 :
  48.                 chosenLanguage = "zh_cn";
  49.                 break;
  50.             case 9 :
  51.                 chosenLanguage = "zh_tw";
  52.                 break;
  53.             default :
  54.                 chosenLanguage = "en";
  55.         }
  56.         System.out.println("after: " + chosenLanguage);
  57.     }
  58.     // some further code
  59. });
  60.  
Now, something must be wrong there, because when I choose the first element, it only works, if it is the first element which I select. After I have selected any other language, it just stays with that language (and prints before: ... and after: ... with the previous language).
What mistake have I made?
Aug 27 '07 #1
Share this Question
Share on Google+
4 Replies


10K+
P: 13,264
I don't see anything wrong with it. It's also working fine on my machine.
The valueChanged method is called twice for each mouse press that's all.
Aug 27 '07 #2

Nepomuk
Expert 2.5K+
P: 3,112
I don't see anything wrong with it. It's also working fine on my machine.
The valueChanged method is called twice for each mouse press that's all.
That's strange... You tried choosing "deutsch" after choosing a different language and it worked?
The "calling twice" surprised me too. Any idea, how to get rid of that?
Aug 27 '07 #3

10K+
P: 13,264
That's strange... You tried choosing "deutsch" after choosing a different language and it worked?
The "calling twice" surprised me too. Any idea, how to get rid of that?
Ok I see your problem now.

This is how I respond to list events



Expand|Select|Wrap|Line Numbers
  1. public void valueChanged(ListSelectionEvent e) {
  2.     if (e.getValueIsAdjusting())
  3.       return;
  4.  
  5.     JList theList = (JList) e.getSource();
  6.     if (theList.isSelectionEmpty()) {
  7. System.out.println("nothing selected");
  8.     } else {
  9.       int index = theList.getSelectedIndex();
  10. switch(index) {
  11.     ......
  12.    ....
  13. }
  14.     } 
  15.  
  16.  
Aug 27 '07 #4

Nepomuk
Expert 2.5K+
P: 3,112
Ok I see your problem now.

This is how I respond to list events



Expand|Select|Wrap|Line Numbers
  1. public void valueChanged(ListSelectionEvent e) {
  2.     if (e.getValueIsAdjusting())
  3.       return;
  4.  
  5.     JList theList = (JList) e.getSource();
  6.     if (theList.isSelectionEmpty()) {
  7. System.out.println("nothing selected");
  8.     } else {
  9.       int index = theList.getSelectedIndex();
  10. switch(index) {
  11.     ......
  12.    ....
  13. }
  14.     } 
  15.  
  16.  
Thanks, that solves it! :-)
Aug 27 '07 #5

Post your reply

Sign in to post your reply or Sign up for a free account.