4

All I want to do is when user selects one element on the JScrollPane and clicks button, I want to grab this object/element of the JList it holds. How can I do that?

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
lunar
  • 1,172
  • 7
  • 18
  • 29
  • 2
    _One element_ of what? What's in the `JScrollPane`? – trashgod Nov 19 '11 at 07:29
  • Do you have many JList objects in one JScrollPane or you want to grab object from JList, which enclosed into JScrollPane? – Vadeg Nov 19 '11 at 07:49
  • Thank you Andrew. Yes, I apparently was looking in the wrong place. This whole swing stuff is something completely new to me and, as I see now, I was looking at JScrollPane classinsted of JList. Sorry, my mistake. – lunar Nov 19 '11 at 07:52
  • *"I was looking at JScrollPane class insted of JList."* I hear you, but it took me around 2 minutes to glance over the docs for `JList`. If I'd considered `JButton`, `JScrollPane` and `JPanel` as well, it would've taken around 8 minutes to get the names of helpful looking methods. The JavaDocs are your friend & mine. The docs are not bookmarked in my browser - I keep a tab open that points at the [Java 7 class list](http://download.oracle.com/javase/7/docs/api/allclasses-frame.html). Not a very pretty document, but provides very fast access to the latest docs. for the classes of the J2SE. – Andrew Thompson Nov 19 '11 at 08:52
  • 1
    I understand, it will not happened again. Thanks for the link. – lunar Nov 19 '11 at 08:59
  • *"it will not happened again"* Great comment. Far more useful, far better, than 'sorry'. :) – Andrew Thompson Nov 19 '11 at 10:12

2 Answers2

8

See JList.getSelectedIndex() or JList.getSelectedValue().

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
6

You can see the sample code below:

import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JList;
import javax.swing.JOptionPane;

public class TestJList {

public static void main(String[] args) {
    String[] items = new String[]
        {
                "One", "Two", "Three", "Four"
        };
    final JList list = new JList(items);
    final JFrame frame = new JFrame();
    frame.setLayout(new BorderLayout());
    frame.add(list, BorderLayout.CENTER);

    JButton btn = new JButton("Get selected");
    btn.addActionListener(new ActionListener() {

        @Override
        public void actionPerformed(ActionEvent e) {
            JOptionPane.showConfirmDialog(frame, "You Selected : " + list.getSelectedValue(), "Display",
                    JOptionPane.PLAIN_MESSAGE);

        }
    });

    frame.add(btn, BorderLayout.SOUTH);
    frame.pack();
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setVisible(true);
}
}
mprabhat
  • 20,107
  • 7
  • 46
  • 63
  • *"just shows the selected element"* So does `showMessageDialog(...)`, but the latter does not ask the user to confirm or cancel the item that was obviously selected (whether the user OKs it or not). OTOH, if the title of your pane were changed from *"Display"* to something like *"Delete this item"*, it would suddenly make a whole lot of sense. :) +1 for the code example. – Andrew Thompson Nov 19 '11 at 10:18
  • 1
    my bad... used confirm instead of message :) – mprabhat Nov 19 '11 at 10:39
  • 1
    That's what edits to answers are good for. You can pretend you meant `showMessageDialog` all along. ;) – Andrew Thompson Nov 19 '11 at 10:44
  • How do you access the JList from the JScrollPane? The example above does not place the JList inside a JScrollPane – paolov May 30 '18 at 23:00