[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 510
  • Last Modified:

JList ListActionListener Question - JAVA!!

I have a JList of Strings in a panel.  How do I get out/return the string that I single-clicked?  I think I need to use ListActionListener but am not sure how.

Thanks.
0
bcsmess
Asked:
bcsmess
  • 10
  • 6
  • 4
1 Solution
 
kiranhkCommented:
// Create a list
    String[] items = {"A", "B", "C", "D"};
    JList list = new JList(items);
   
    // Add a listener for mouse clicks
    list.addMouseListener(new MouseAdapter() {
        public void mouseClicked(MouseEvent evt) {
            JList list = (JList)evt.getSource();
            if (evt.getClickCount() == 2) {          // Double-click
                // Get item index
                int index = list.locationToIndex(evt.getPoint());
            } else if (evt.getClickCount() == 3) {   // Triple-click
                // Get item index
                int index = list.locationToIndex(evt.getPoint());
   
                // Note that this list will receive a double-click event before this triple-click event
            }
        }
    });



also check out other examples in this

http://javaalmanac.com/egs/javax.swing/list_ListDblClick.html?l=rel
0
 
bcsmessAuthor Commented:
Quick simple question:  How do you return the string now that we have the index?
0
 
bcsmessAuthor Commented:
Also, it tells me that MouseAdapter() cannot be resolved/not a type error.  I have imported all the api necessary.  What's the problem here?

Thanks.
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
kiranhkCommented:
instead of locationToIndex use this

String value = list.getSelectedValue();  

this will return u the value
0
 
kiranhkCommented:
java.awt.event.MouseAdapter
0
 
bcsmessAuthor Commented:
Ok yeah I had gotten that.  My last question and then I should be good is that now that I have the string, apparently you can't  refer to a non-final variable inside the inner class and so how do I get this variable out?

      list.addMouseListener(new MouseAdapter() {
            public void mouseClicked(MouseEvent evt) {
                  JList lst = (JList)evt.getSource();
                  if (evt.getClickCount() == 2) {          // Double-click
                        int index = lst.locationToIndex(evt.getPoint());
                        selectedValue = (String) lst.getSelectedValue();
                  }
            }
      });

How do I get out selectedValue?

Thanks again, and I'll award points after this.
0
 
kiranhkCommented:
better keep all these event handling things in a seperate class and add it as the listener...
this way it will be easier to make changes and add features later.....
0
 
bcsmessAuthor Commented:
not sure exactly how to do this yet.  Is there a simple way to get out that value to be used.
0
 
kiranhkCommented:
0
 
objectsCommented:
I already posted how to achieve that in your earlier question:

list.addListSelectionListener(new ListSelectionListener()
{
   public void valueChanged(ListSelectionEvent e)
   {
      JList list = e.getSource();
      String selected = (String) list.getSelectedValue();
      // ...
   }
}
0
 
bcsmessAuthor Commented:
     list.addMouseListener(new MouseAdapter() {
            public void mouseClicked(MouseEvent evt) {
                  JList lst = (JList)evt.getSource();
//                  if (evt.getClickCount() == 1) {          // Double-click
                        int index = lst.locationToIndex(evt.getPoint());
                        String selectedValue = (String) lst.getSelectedValue();
                                 ....
//                  }
            }
      });

doesn't work.

and this:

final class SelectionListenerLeftPane implements ListSelectionListener{
      FeedTreePane fp;
      JEditorPane br;
      public SelectionListenerLeftPane(FeedTreePane fp, JEditorPane br){
            this.fp = fp;
            this.br = br;
      }

      public void valueChanged(ListSelectionEvent evt) {
                   JList lst = (JList)evt.getSource();

                   String selectedValue = (String) lst.getSelectedValue();
                        ...
      };
}
also doesn't work.


Please help me figure out how to actually return the selected value each time an item is selected.  I still can't get it.

Thanks.
0
 
kiranhkCommented:
final class SelectionListenerLeftPane implements ListSelectionListener{
     FeedTreePane fp;
String selectedValue = "";
     JEditorPane br;
     public SelectionListenerLeftPane(FeedTreePane fp, JEditorPane br){
          this.fp = fp;
          this.br = br;
     }

     public void valueChanged(ListSelectionEvent evt) {
                JList lst = (JList)evt.getSource();

                selectedValue = (String) lst.getSelectedValue();
                        ...
     };
}
0
 
kiranhkCommented:
add a method which returns the selectedValuess

public String getSelectedValue()
{
return selectedValue;
}
0
 
objectsCommented:
how exactly doesn't it work?
0
 
bcsmessAuthor Commented:
How do you incorporate list.addListSelectionListener(???);  into the method to implement this listener class
0
 
kiranhkCommented:
SelectionListenerLeftPane xyz = new SelectionListenerLeftPane ();
list.addListSelectionListener(xyz);
String selectedValues = xyz.getSelectedValue();
0
 
objectsCommented:
did u try the code I posted?

list.addListSelectionListener(new ListSelectionListener()
{
   public void valueChanged(ListSelectionEvent e)
   {
      JList list = e.getSource();
      String selected = (String) list.getSelectedValue();
      System.out.println("Selected: "+selected);
      // ...
   }
}
0
 
kiranhkCommented:
Objects, you didnt go thro' the thread properly...
he needs to get the selected value out of the Inner class method.....
0
 
objectsCommented:
> he needs to get the selected value out of the Inner class method.....

then you just need to call a method in the outer class
0
 
kiranhkCommented:
thats what i am trying to help him with...
if you have other ideas pl do post it...
thx
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

  • 10
  • 6
  • 4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now