Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java Swing How to detect a list selection event

I'm making a simple GUI where the user can select an item from a list of Strings using the JList<String> component, and I want my program to update a JTextField with some data describing the selected item. I know I need an event listener, but I'm confused as to what I should use to detect a change in selection in my list.

like image 788
Andrew Lalis Avatar asked Oct 31 '25 09:10

Andrew Lalis


1 Answers

You need to add it to the JList object:

myJList.addListSelectionListener(new ListSelectionListener() {

    @Override
    public void valueChanged(ListSelectionEvent e) {
        System.out.println("Hello you selected me!  "
            + dataList.getSelectedValue().toString());
    }
});
like image 123
ergonaut Avatar answered Nov 02 '25 00:11

ergonaut



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!