Search code examples
javaswingjtextfieldjlistlistselectionlistener

How can I get JList item to be displayed in a JTextField


I have made a simple JList with 4 options, and I have a JTextField beside the JList. How can I get the user's choice from the JList to be displayed in the JTextField? (Code has been edited to include Listener class)

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

public class JListExample extends JFrame
{   
private JPanel p1, p2;
private JList jList;                                                                        // instance variables        
private JScrollPane scrollPane;
private JTextField jtfChoice;

public JListExample()                                                                   // constructor
{
    String[] itemList =  {"alpla", "beta", "gamma", "delta", "omega"};      // array  of Strings for list of items
    jList = new JList(itemList);
    jList.setSelectedIndex(1);                                                          // default item selected
    jList.setVisibleRowCount(3);                                                        // no. of visible rows
    jList.setSize(220, 200);

    p1 = new JPanel();
    p1.add(jtfChoice = new JTextField(8), BorderLayout.CENTER);

    p2 = new JPanel();
    p2.add(scrollPane = new JScrollPane(jList), BorderLayout.WEST);
    p2.add(p1);

    add(p2, BorderLayout.EAST);
    ListenerClass ListSelectionListener = new ListenerClass();
    jList.addListSelection(listener);
}

public static void main(String[] args)
{       
    JListExample frame = new JListExample();                                    // new frame object 
    frame.setTitle("JList Example");                                            // set frame title
    frame.pack();                                                                           // sizes the frame so components fit frame  
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);                   // ends program on frame closing
    frame.setLocationRelativeTo(null);                                              // centre frame
    frame.setVisible(true);                                                 // make frame visible
}
private class ListenerClass implements ListSelectionListener
{   
    public void valueChanged(ListSelectionEvent e)
    {
        JTextField.setText();
    }
}
}

Solution

  • You're referencing the class name, not the variable:

    JTextField.setText();
    

    What you want is:

    jtfChoice.setText();
    

    Also, you're importing the awt events, when you should be importing the swing events:

    import javax.swing.event.*;
    

    Also, you never declare listener:

    ListenerClass ListSelectionListener = new ListenerClass();
    jList.addListSelection(listener);  //listener doesn't exist
    

    Here's what you should be doing:

    ListenerClass listener = new ListenerClass();
    jList.addListSelectionListener(listener);
    

    All told, the final, functional class looks like this:

    import javax.swing.*;
    import javax.swing.event.*;
    import java.awt.*;
    
    public class JListExample extends JFrame {
    
        private JPanel p1, p2;
        private JList jList;
        private JScrollPane scrollPane;
        private JTextField jtfChoice;
    
        public JListExample() // constructor
        {
            String[] itemList = {"alpla", "beta", "gamma", "delta", "omega"};
            jList = new JList(itemList);
            jList.setSelectedIndex(1);
            jList.setVisibleRowCount(3);
            jList.setSize(220, 200);
    
            p1 = new JPanel();
            p1.add(jtfChoice = new JTextField(8), BorderLayout.CENTER);
    
            p2 = new JPanel();
            p2.add(scrollPane = new JScrollPane(jList), BorderLayout.WEST);
            p2.add(p1);
    
            add(p2, BorderLayout.EAST);
            ListenerClass listener = new ListenerClass();
            jList.addListSelectionListener(listener);
        }
    
        public static void main(String[] args) {
            JListExample frame = new JListExample();
            frame.setTitle("JList Example");
            frame.pack();
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            frame.setLocationRelativeTo(null);
            frame.setVisible(true);
        }
    
        private class ListenerClass implements ListSelectionListener {
    
            public void valueChanged(ListSelectionEvent e) {
                jtfChoice.setText(jList.getSelectedValue().toString());
            }
        }
    }
    

    On a completely unrelated note: your comments don't really add anything. When you say something like:

    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); // ends program on frame closing
    

    All you're doing is duplicating what the code already says. Good code should be self-documenting, and comments should explain the why and the how, not the what.