File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Swing / AWT / SWT and the fly likes JComboBox Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Java Interview Guide this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "JComboBox" Watch "JComboBox" New topic


Anagha Medicharla

Joined: Feb 06, 2001
Posts: 11
I want to take information from user and that information I want to add to JComboBox.For that I have created few textfields.
Means user will fill information into that textfields and if he click 'Add' button it should go to JComboBox.for that I have created ActionPerformedEvent.What is happening is it's working correct.But When I again run the same application previously added information is not there.Means whatever I am adding through 'Add' button it's not getting added to JcomboBox permanently. Why is this happening?If anybody has an idea please reply immediately.

Bryan Fagan

Joined: Feb 26, 2001
Posts: 17
One reason could be the constructor you are using with the JComboBox. In order to use JComboBox.addItem(Object) method, you are supposed to use the default constructor.
JComboBox jCombo = new JComboBox();
I wrote a small program that adds text from a textfield to a jcombobox. I'm unable to repro your problem.
Let me know if this helps:
* Test for combo boxes.
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
public class Test extends JFrame {
private JComboBox jCombo;
private JTextField jText;
private JButton jBut;

public Test() {
getContentPane().setLayout(new GridLayout(3,1));

jCombo = new JComboBox();
jText = new JTextField();
jBut = new JButton("Add me!");
jBut.addActionListener(new Button_Click(jText,jCombo));
this.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent we) {

public static void main(String[] args) {
Test tst = new Test();
class Button_Click implements ActionListener {

private JTextField jText;
private JComboBox jCombo;
Button_Click(JTextField jText, JComboBox jCombo) {
this.jText = jText;
this.jCombo = jCombo;
public void actionPerformed(ActionEvent ae) {
try {
} catch(Exception e) {
System.err.println("Exception: " + e.getMessage());
Mike Curwen
Ranch Hand

Joined: Feb 20, 2001
Posts: 3695

When I again run the same application previously added information is not there

It can't be.

If you open Notepad and type a letter, close Notepad, and choose not to save the letter, do you expect the letter to be there when you open the same application again?

The same thing will happen to text entered into a TextBox, that is then shifted into a ComboBox. From one running of a program to another, there is no saving of that information unless you explicitly code it.

If I've misunderstood, I apologise.
Anagha Medicharla

Joined: Feb 06, 2001
Posts: 11
Hi Brayan,
I tried your code.It is not solving my problem.But thank you very much for your help.I think what Mike saying is correct.

HI Mike,
Can you give me little hint about how to write an explicit code to save such program?

I agree. Here's the link:
subject: JComboBox
jQuery in Action, 3rd edition