2016-04-27 98 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

非常感谢你:) –