Solved

How to use JComboBox

Posted on 2004-09-17
14
1,148 Views
Last Modified: 2008-03-10
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
Comment
Question by:chenwei
  • 7
  • 2
  • 2
14 Comments
 
LVL 37

Expert Comment

by:zzynx
ID: 12083001
jComboBox.addItem(yourTextField.getText());
0
 
LVL 37

Expert Comment

by:zzynx
ID: 12083009
>> After I typed some text and pressed RETURN
Oh, do you mean you type the text in the editable combo box?
0
 
LVL 37

Accepted Solution

by:
zzynx earned 20 total points
ID: 12083231
>> 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
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

 

Author Comment

by:chenwei
ID: 12083428
What's "comboBoxEdited"? The text I typed? How can the program know what text I will type?
0
 
LVL 37

Expert Comment

by:zzynx
ID: 12083468
>> 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
 

Author Comment

by:chenwei
ID: 12083683
Thanks. But I solve my problem myself. I do folows:

...
public void actionPerformed(java.awt.event.ActionEvent e) {
 jComboBoxCommand.addItem(jComboBoxCommand.getSelectedItem());
...
...
0
 
LVL 37

Expert Comment

by:zzynx
ID: 12083739
You asked
>> What should it be for the ???
I answered. You use it now.
0
 
LVL 37

Expert Comment

by:zzynx
ID: 12084011
So, kindly accept my comment
0
 
LVL 3

Expert Comment

by:nesnemis
ID: 12090679
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
 
LVL 3

Expert Comment

by:nesnemis
ID: 12090681
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
 
LVL 37

Expert Comment

by:zzynx
ID: 12284395
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

Comprehensive Backup Solutions for Microsoft

Acronis protects the complete Microsoft technology stack: Windows Server, Windows PC, laptop and Surface data; Microsoft business applications; Microsoft Hyper-V; Azure VMs; Microsoft Windows Server 2016; Microsoft Exchange 2016 and SQL Server 2016.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
egit plugin on eclipse 8 64
runtime exception 2 30
maven module vs maven project 3 23
String array comparison 4 34
Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…

832 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question