-2

I've created a JList and JTextField. In my JList I have written few selection choices, and I want the selected choice name to appear in JTextField.

What code do I need to write in jTextField2 Action listener so for instance if I select Computer it will show Computer in the JTextField

ListModel

jList1Model = new DefaultComboBoxModel (new String[] { "Computer","Mouse","HDD"});

jList1 = new JList();
getContentPane().add(jList1);
jList1.setModel(jList1Model);
jList1.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
jTextField2 = new JTextField();

getContentPane().add(jTextField2);
jTextField2.setEditable(false);
jTextField2.addActionListener(new ActionListener() {
    public void actionPerformed(ActionEvent evt) {
    }
});
Tom
  • 26,212
  • 21
  • 100
  • 111
JOHN
  • 33
  • 6

3 Answers3

2

You need to add ListSelectionListener Which will get fired on change in List selection:

  jList1.addListSelectionListener(new ListSelectionListener() {
        public void valueChanged(ListSelectionEvent evt) {
            jList1ValueChanged(evt);
        }
    });

and in ListSelectionEvent

 private void jList1ValueChanged(ListSelectionEvent evt) {

    if(!jList1.getValueIsAdjusting())
    { 
    jTextField2.setText((String) jList1.getSelectedValue());
    }
  }

Here is an short Example:

import javax.swing.AbstractListModel;
import javax.swing.JFrame;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.SwingUtilities;
import javax.swing.WindowConstants;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;

public class JListTest  {
    private JList jList1;
    private JPanel jPanel1;
    private JTextField jTextField2;
    private JFrame frame;


    public JListTest() {
        initComponents();
    }

    private void initComponents() {

        jPanel1 = new JPanel();
         jList1 = new JList();
        jTextField2 = new JTextField();
        frame =new JFrame();
        frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);

        jList1.setModel(new AbstractListModel() {
            String[] strings = { "Computer", "Mouse", "HDD" };
            public int getSize() { return strings.length; }
            public Object getElementAt(int i) { return strings[i]; }
        });
        jList1.addListSelectionListener(new ListSelectionListener() {
            public void valueChanged(ListSelectionEvent evt) {
                jList1ValueChanged(evt);
            }
        });


        jTextField2.setText("jTextField1");
        jPanel1.add(jList1);
        jPanel1.add(jTextField2);
        frame.add(jPanel1);
        frame.pack();
        frame.setVisible(true);


    }

  private void jList1ValueChanged(ListSelectionEvent evt) {

    if(!jList1.getValueIsAdjusting())
    { 
    jTextField2.setText((String) jList1.getSelectedValue());
    }
}


    public static void main(String args[]) {
        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                new JListTest();
            }
        });
    }
}
joey rohan
  • 3,505
  • 5
  • 33
  • 70
  • that's incomplete: you - most probably - only want to change the textfield if the selection change is ready, that is getValueIsAdjusting returns false. – kleopatra Feb 24 '13 at 14:04
  • @kleopatra do you mean `jList1ValueChanged` will work two times, And to use MouseClicked insted of ValueChanged? – joey rohan Feb 24 '13 at 14:07
  • re: _two times?_ what happened when you tried ;-) re: _mouseClicked_ where in my last comment did you read me talking about a low level listener ;-) Don't use them if there are semantic listeners like selection – kleopatra Feb 24 '13 at 14:11
  • @kleopatra two times as in ValueIsAdjusting Will make it fire two times.Edited.Is that what you want to point out? – joey rohan Feb 24 '13 at 14:20
1

How about this:

    jList1t.addListSelectionListener(new ListSelectionListener() {
        public void valueChanged(ListSelectionEvent e) {
            jTextField2.setText((String) jList1.getSelectedValue());
        }
    });

(and jTextField2 must be final!)

vikingsteve
  • 38,481
  • 23
  • 112
  • 156
-1

javax.swing.JComboBox cboSelect = new javax.swing.JComboBox(); 
javax.swing.JTextField tfResult = new javax.swing.JTextField(); cboSelect.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Computer", "Mouse", "HDD" })); cboSelect.addItemListener(new java.awt.event.ItemListener() { public void itemStateChanged(java.awt.event.ItemEvent evt) { tfResult.setText(cboSelect.getSelectedItem().toString()); } });