Find selected item of a JList and display it in real time
Asked Answered
A

3

16

I have a JList, where i am displaying some ID's. I want to capture the ID the user clicked and dis play it on a JLabel.

String selected = jlist.getSelectedItem().toString();

The above code gives me the selected JList value. But this code has to be placed inside a button event, where when i click the button it will get the JList value an assign it to the JLabel.

But, what i want to do is, as soon as the user clicks an item of the JList to update the JLabel in real time. (without having to click buttons to fire an action)

Alanalana answered 10/12, 2012 at 12:16 Comment(0)
S
30

A simple example would be like below using listselectionlistener

import java.awt.Dimension;
import java.awt.FlowLayout;

import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;

public class JListDemo extends JFrame {

    public JListDemo() {

        setSize(new Dimension(300, 300));
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setLayout(new FlowLayout());
        final JLabel label = new JLabel("Update");
        String[] data = { "one", "two", "three", "four" };
        final JList dataList = new JList(data);

        dataList.addListSelectionListener(new ListSelectionListener() {

            @Override
            public void valueChanged(ListSelectionEvent arg0) {
                if (!arg0.getValueIsAdjusting()) {
                  label.setText(dataList.getSelectedValue().toString());
                }
            }
        });
        add(dataList);
        add(label);

        setVisible(true);

    }

    public static void main(String args[]) {
        new JListDemo();
    }

}
Scarrow answered 10/12, 2012 at 12:26 Comment(8)
please let me know the reason behind downvote. it might help meScarrow
Too much clutter, keep your answers precise. Since the author already knows how to set up Java classes, listeners etc., only the listener portion is useful. I'm removing my downvote though since the answer is still correct.Butchery
@Butchery Thats really a good suggestion for me. I'll concider it with my further answers. Thanks a lotScarrow
@vini when i print the Jlist value to the console, i got it printed twice. So do you have any clue as in why it get's called twice ?Alanalana
@Butchery certainly a matter of taste - mine is that SSCCEs are welcome :-)Holna
@sharonHwk all anwers have a little error: look at the event.getValueIsAdjusting property - and do your stuff only if falseHolna
@Holna Thank you that worked. Wish i could select all the answers posted as the correct answer. I took little parts from all of themAlanalana
@sharonHwk Don't worry about it, we all missed the part kleopatra mentioned. :)Butchery
B
10

Why don't you put a ListSelectionListener on your JList, and add your above code in to it.

I'm assuming you already know how to create listeners on JButtons, based on your question, so you just need to tweak it to create a ListSelectionListener instead, then assign the listener to your JList using jlist.addListSelectionListener(myListener);

There is a nice tutorial here that should get you started, or refer to the documentation

You should be aiming for something like this...

jlist.addListSelectionListener(new ListSelectionListener() {
    public void valueChanged(ListSelectionEvent event) {
        if (!event.getValueIsAdjusting()){
            JList source = (JList)event.getSource();
            String selected = source.getSelectedValue().toString();
        }
    }
});
Boleslaw answered 10/12, 2012 at 12:18 Comment(1)
incomplete .. (the effect of which @sharon already noticed in the other answer)Holna
B
5

Use a ListSelectionListener:

JList list = new JList(...);
list.addListSelectionListener(new ListSelectionListener() {
  public void valueChanged(ListSelectionEvent evt) {
    if (!evt.getValueIsAdjusting()) {
      // code here
    }
  }
});
Butchery answered 10/12, 2012 at 12:19 Comment(1)
incomplete .. (the effect of which @sharon already noticed in the other answer)Holna

© 2022 - 2024 — McMap. All rights reserved.