Results 1 to 5 of 5
  1. #1
    Psyclone is offline Member
    Join Date
    Jan 2010
    Posts
    81
    Rep Power
    0

    Default Question about JLists

    I have 2 JLists in the same GUI with 3 ComboBoxes and a bunch of Radio Buttons. I have the radio buttons grouped together.

    In the GUI, when I selected an item from a ComboBox or RadioButton (which highlights it) and then select another component, the ComboBox/RadioButton item becomes unselected.

    Whenever I choose an item from a JList and then click on a radio button, combo box, or another list... the item remains highlighted. Is there a way to have the selected item in the list become unselected (unhighlighted) when I click on another component?
    Last edited by Psyclone; 02-21-2010 at 03:01 AM.

  2. #2
    Psyclone is offline Member
    Join Date
    Jan 2010
    Posts
    81
    Rep Power
    0

    Default

    Sorry, forgot to mention, I have tried using list.clearSelection(). But whenever I do this, the first list i click on gets selected normally. When I choose an item from the opposite list, it just outlines the selection (doesn't highlight it). If I click a 2nd time (on any item from the list) it becomes highlighted like normal. Then the problem just reverses and I must click on the other list twice to get an item to work.

    This does not occur when I don't use clearSelection(), but the item from the opposing list remains highlighted at all times.

  3. #3
    Michael Dunn is offline Member
    Join Date
    Jul 2008
    Posts
    62
    Rep Power
    0

    Default

    one way might be to add a class field
    int selectedIndex = -1;

    then add a focusListener to the JList (list)

    Java Code:
    list.addFocusListener(new FocusListener(){
      public void focusGained(FocusEvent fe){
        list.setSelectedIndex(selectedIndex);
      }
      public void focusLost(FocusEvent fe){
        selectedIndex = list.getSelectedIndex();
        list.clearSelection();
      }
    })

  4. #4
    Psyclone is offline Member
    Join Date
    Jan 2010
    Posts
    81
    Rep Power
    0

    Default

    Thanks, I got it to work, but I had to use...

    Java Code:
            list.addFocusListener(new FocusListener(){
                public void focusGained(FocusEvent fe){
                    list.setSelectedIndex(list.getSelectedIndex());
                }
                public void focusLost(FocusEvent fe){
                    list.clearSelection();
                }
            });
    The way you had it was fine except that it selected the last known index when the list became focused instead of the item clicked.

    THANKS!

  5. #5
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

Similar Threads

  1. Question mark colon operator question
    By orchid in forum Advanced Java
    Replies: 9
    Last Post: 12-19-2010, 08:49 AM
  2. JLists - Filtering
    By Psyclone in forum AWT / Swing
    Replies: 1
    Last Post: 02-16-2010, 06:09 AM
  3. question
    By ayoood in forum Java Software
    Replies: 6
    Last Post: 07-07-2008, 01:32 PM
  4. a question
    By slytheman in forum Java Servlet
    Replies: 0
    Last Post: 03-12-2008, 04:11 AM
  5. Need help with a question please
    By sonal in forum New To Java
    Replies: 1
    Last Post: 11-29-2007, 09:17 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •