İlker Çetin
İlker Çetin

Reputation: 51

How can I add text to combobox from textfield?

I want to create a combobox and a textbox. And user will enter a text into the textbox, and the text will be added as an item of combobox.

How can I do it? I wrote a code, but I couldn't find what will I write in 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();
    }
}

Upvotes: 1

Views: 6246

Answers (2)

Priyamal
Priyamal

Reputation: 2969

 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); 
                }

            }
        });
}

you have to set your editable property in comboBox to true otherwise you wont able to write on comboBox. make sure you call addActionListners() method at the startup(constructor). i am giving the combobox the functionality of a jtext field by changing the editor of the combobox to jtextComponent. try this example

Upvotes: 0

Clark Kent
Clark Kent

Reputation: 1176

I added a button, and put the functionality on adding to the JComboBox on the button. Here's an example:

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

Upvotes: 1

Related Questions