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

x
?
Solved

Trigger Action in JLIST

Posted on 2011-05-01
8
Medium Priority
?
794 Views
Last Modified: 2013-11-23
I have 4 buttons that when pressed should emulate the Up, Down, Left, Right action in a JList. My question is how can I trigger the action in the JList without mapping action to my buttons (i.e. new JButton(action)?

For example, I assume I would have to get the action like this, but how do I evoke it in the JList.
Action action = list.getActionMap().get("left-arrow");
0
Comment
Question by:ABlahus
  • 6
  • 2
8 Comments
 
LVL 47

Expert Comment

by:for_yan
ID: 35501046
You can add ItemListener to you JList and define itemStateChanged(temEvent ie) metghod
0
 
LVL 47

Expert Comment

by:for_yan
ID: 35501053
But I'm not sure I understan what you mean by buttons
especially Right and Left
JList is a dropdown element - what do you do with Right and Left buuton

 
0
 
LVL 47

Expert Comment

by:for_yan
ID: 35501058
You can sertainly use "Up" and ""Down" buttons to chenge selected items in the list,
but that is rather uncommon use - JList is normally used to allow user to choose an option from the dropdown
menu
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:ABlahus
ID: 35501087
I don't want to use the mouse or keyboard "arrows" to navigate JList. User would navigate JList with attached image of JButton layout.

NavPad.bmp
0
 
LVL 47

Expert Comment

by:for_yan
ID: 35501088
Thai is the simplest use of JList with the attached ItemListener

Vector v = new Vector();
v.addElement("1");
v.addElement("2");
v.addElement("3");


JList list = new JList(v);

list.addItemListener(new ItemListener(){
public void itemStateChanged(ItemEvent ie){
         System.out.println("Item: " +e.getItem());
//do something with selected item
});



0
 
LVL 47

Expert Comment

by:for_yan
ID: 35501100
OK. Then you should attach ActionListener or MouseListener to your buttons, beacuse that's from where
you expect events - and then change selected item of the list based on
which button you got event from.
BUt you still cannot navigate right and left within JList
If you want to navigate in all four directions you should
probably use JTable not JList, or maybe a set of checkboxes or radioboxes
0
 

Author Comment

by:ABlahus
ID: 35501136
I will look at JTable, but I don't think I have clearly stated my objective based on your answer. Here is the scenario.

class NavPadListener implements ActionListener {
            public void actionPerformed(ActionEvent e) {
                  if ("up".equals(e.getActionCommand())) {
                        Action action = list.getActionMap().get("up-arrow");                        
            >> how do I send action to my list? <<<
                  }
            }
I want to send action to my list like the user hit the up arrow on the keyboard.
0
 
LVL 47

Accepted Solution

by:
for_yan earned 2000 total points
ID: 35501156
You don't need any ACtionMap.
You do just that:

class NavPadListener implements ActionListener {
            public void actionPerformed(ActionEvent e) {
                  if ("up".equals(e.getActionCommand())) {
     int i =  list.getSelectedIndex();
   if(i== 0) return;
   else
    list.setSelectedIndex(i-1);
                       // Action action = list.getActionMap().get("up-arrow");                        
            >> how do I send action to my list? <<<
                  }
            }
Note that in JList you cannot go higer than upermost elment (index = 0).
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
Suggested Courses
Course of the Month20 days, 14 hours left to enroll

810 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