2016-04-27 136 views
-1

我想創建comboboxtextbox。用戶將在文本框中輸入文本,文本將被添加爲combobox的項目。如何從文本框添加文本到組合框?

我該怎麼辦呢?我寫了一個代碼,但是我找不到我在actionlistener中寫什麼。

import java.awt.FlowLayout; 
import java.awt.event.ActionEvent; 
import java.awt.event.ActionListener; 

import javax.swing.JComboBox; 
import javax.swing.JFrame; 
import javax.swing.JTextField; 

public class Q2 extends JFrame { 

    JTextField t; 
    JComboBox combobox = new JComboBox(); 

    public Q2() { 
     t = new JTextField("Enter text here", 20); 
     t.setEditable(true); 
     t.addActionListener(new act()); 
     add(t); 
     add(combobox); 

     combobox.addItem(t.getText().toString()); 
     setLayout(new FlowLayout()); 
     setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
     setSize(300, 300); 
     setLocationRelativeTo(null); 
     setVisible(true); 
    } 

    public class act implements ActionListener { 
     public void actionPerformed(ActionEvent e) { 

     } 
    } 

    public static void main(String[] args) { 
     Q2 test = new Q2(); 
    } 
} 
+0

爲什麼你需要一個文本框在jcombobox上寫入數據,你可以讓組合編輯。我只是說。 – Priyamal

+0

我是新來的java和我想學習所有的代碼:)而且我也不知道可編輯組合框.. –

+0

k生病發布它作爲一個答案,然後等待。 – Priyamal

回答

0

我添加了一個按鈕,並把功能添加到按鈕上的JComboBox。這裏有一個例子:

import java.awt.FlowLayout; 
import java.awt.event.ActionEvent; 
import java.awt.event.ActionListener; 

import javax.swing.JButton; 
import javax.swing.JComboBox; 
import javax.swing.JFrame; 
import javax.swing.JTextField; 

public class Q2 extends JFrame { 

    JTextField t; 
    JComboBox combobox; 
    JButton b; 

    public Q2() { 
     combobox = new JComboBox(); 
     t = new JTextField("Enter text here", 20); 
     t.setEditable(true); 
     b = new JButton("Add"); 
     b.addActionListener(new act()); //Add ActionListener to button instead. 
     add(t); 
     add(combobox); 
     add(b); 

     //combobox.addItem(t.getText().toString()); //Moved to ActionListener. 
     setLayout(new FlowLayout()); 
     setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
     setSize(300, 300); 
     setLocationRelativeTo(null); 
     setVisible(true); 
    } 

    public class act implements ActionListener { 
     public void actionPerformed(ActionEvent e) { 
      combobox.addItem(t.getText()); //Removed .toString() because it returns a string. 
     } 
    } 

    public static void main(String[] args) { 
     Q2 test = new Q2(); 
    } 
} 
+0

謝謝:)這比我想做的要好:) –

0
private JTextComponent comboboxEditor; 

Vector ComboData = new Vector(); 

public void addActionListners() { 

     //adding action listner to the NameComboBox 
     this.comboboxEditor = (JTextComponent) yourCombo.getEditor().getEditorComponent(); 
     comboboxEditor.addKeyListener(new KeyAdapter() { 

      public void keyReleased(KeyEvent evt) { 
       int i = evt.getKeyCode(); 
       if (i == 10) { 
        //combobox action on enter 
        ComboData.add(comboboxEditor.getText()); 
        yourCombo.setListData(ComboData); 
       } 

      } 
     }); 
} 

你必須設置在組合框中真正的可編輯的屬性,否則你不會能夠在組合框寫。確保在啓動時(構造函數)調用addActionListners()方法 。我通過將組合框的編輯器更改爲jtextComponent來爲組合框提供jtext字段的功能。嘗試這個例子

+0

非常感謝你:) –