• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1173
  • Last Modified:

How to use JComboBox

I use a JComboBox in my program. What I want is: After I typed some text and pressed RETURN, the typed text will add to item.

I use the ActionListener (Is this a correct listener?), then I do follows:

...
jComboBox.addActionListener(new java.awt.event.ActionListener() {
  public void actionPerformed(java.awt.event.ActionEvent e) {    
    jComboBox.addItem(???);
...
}
What should it be for the ???


0
chenwei
Asked:
chenwei
  • 7
  • 2
  • 2
1 Solution
 
zzynxSoftware engineerCommented:
jComboBox.addItem(yourTextField.getText());
0
 
zzynxSoftware engineerCommented:
>> After I typed some text and pressed RETURN
Oh, do you mean you type the text in the editable combo box?
0
 
zzynxSoftware engineerCommented:
>> Oh, do you mean you type the text in the editable combo box?
So, you want to "remember" the manually entered items in the combo box for future use?

That is:

jComboBox.addActionListener(new java.awt.event.ActionListener() {

  public void actionPerformed(java.awt.event.ActionEvent e) {    
        if (!evt.getActionCommand().equals("comboBoxEdited")) return;
        jComboBox.addItem( jComboBox.getSelectedItem());      
  }

}
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
chenweiAuthor Commented:
What's "comboBoxEdited"? The text I typed? How can the program know what text I will type?
0
 
zzynxSoftware engineerCommented:
>> What's "comboBoxEdited"? The text I typed?
Not at all.

actionPerformed() is triggered twice when pressing Enter:
once with actionCommand == "comboBoxChanged"
once with actionCommand == "comboBoxEdited"

To avoid the same item added twice you need that if statement.

Just try it.
0
 
chenweiAuthor Commented:
Thanks. But I solve my problem myself. I do folows:

...
public void actionPerformed(java.awt.event.ActionEvent e) {
 jComboBoxCommand.addItem(jComboBoxCommand.getSelectedItem());
...
...
0
 
zzynxSoftware engineerCommented:
You asked
>> What should it be for the ???
I answered. You use it now.
0
 
zzynxSoftware engineerCommented:
So, kindly accept my comment
0
 
nesnemisCommented:
First, add an actionlistener to the textfield that you want to input your text

Then in actionPerformed add:
   combo.addItem(text.getText());

To make it easier to input many values you can also add this code after additem:
   text.setText("");
0
 
nesnemisCommented:
The whole program is:

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

import javax.swing.JComboBox;
import javax.swing.JDialog;
import javax.swing.JPanel;
import javax.swing.JTextField;

/**
 * @author Espen
 */
public class ComboMaker extends JDialog implements ActionListener
{
      JTextField text;
      JComboBox combo;
      /**
       * Constructor for ComboMaker.
       */
      public ComboMaker()
      {
            super();
            
            JPanel panel = new JPanel();
            panel.setBackground(Color.BLUE);
            
            combo = new JComboBox();
            
            text = new JTextField(10);
            text.addActionListener(this);
            
            panel.add(text);
            panel.add(combo);
            
            getContentPane().add(panel);
            
            setSize(200, 200);
      }
      public void actionPerformed(ActionEvent ae)
      {
            if(ae.getSource() == text)
            {
                  combo.addItem(text.getText());
                  text.setText("");
            }
      }
}
0
 
zzynxSoftware engineerCommented:
The author says he solved his problem, but the code he gave is just a copy of my comment,
so I think my comment deserves to be accepted.
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 7
  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now