4

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.

1 Answer 1

6

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());
    }
});

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.