2013-02-24 223 views
-1

我創建了一個JListJTextField。在我的JList中,我寫了幾個選擇選項,並且我希望所選選項名稱出現在JTextField中。添加動作監聽器

什麼代碼,我需要在jTextField2動作監聽這麼寫,例如,如果我選擇計算機它會顯示電腦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) { 
    } 
}); 

回答

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

JList在哪裏? – 2013-02-24 13:27:59

1

這個怎麼樣:

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

(和jTextField2中必須final!)

+0

看到我的評論http://stackoverflow.com/a/15052245/203657 – kleopatra 2013-02-24 14:05:51

2

您需要添加ListSelectionListener這將在變化列表選擇炒魷魚:

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

ListSelectionEvent

private void jList1ValueChanged(ListSelectionEvent evt) { 

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

這裏是一個簡短的例子:

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

這是不完整的:你 - 最有可能 - 只想更改文本字段,如果選擇更改已準備就緒,即getValueIsAdjusting返回false。 – kleopatra 2013-02-24 14:04:52

+0

@kleopatra你的意思是'jList1ValueChanged'會工作兩次,並且使用MouseClicked insted的ValueChanged? – 2013-02-24 14:07:42

+0

re:_two times?_當你嘗試時發生了什麼;-) re:_mouseClicked_在我最後的評論中,你讀過我在談論一個低級別的監聽器;-)如果有語義監聽器,請選擇 – kleopatra 2013-02-24 14:11:36