Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Adding new item to a combo box dynamically 1

Status
Not open for further replies.

gh0st32

Programmer
Jan 24, 2008
8
US
Hi All,

I have an issue similar to thread269-1403865

I have a combo box that pulls data from sql database. After a new record is inserted I would like the combobox to display the new information. The following pseudo-code breaks down the steps.

JComboBox edit_key_list = new JComboBox();
//method invoked that pulls the data and puts it into the combo box
//component is added
//action event called where new info is inserted
edit_key_list.addItem(new_event); //in action performed

I have tried re-calling the data load method after the new data is inserted but I cannot re-add the component to the panel. I am thinking I will have to make the app multi-threaded. But it would be a P.I.T.A. for something this simple. I tried using repaint() & update() as well as a long shot.

So would there be a way to make this happen without making the app multi-threaded?

Thanks
 
What type of object is new_event that you're adding to the JComboBox?

How are you adding the data to the combo?

I've tried with a simple code that just calls addItem(String) to 3 JComboBox (one created empty, one created with a Vector and one created with a String[]) and it works, without calling update nor repaint. The 3 combos show the items added.

Code:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.util.Vector;

public class Prueba extends JFrame implements ActionListener
{
	JButton b;
	JComboBox c1, c2, c3;
	public static void main(String args[])
	{
		new Prueba();
	}

	public Prueba()
	{
		b = new JButton("b");
		c1 = new JComboBox();
		
		Vector v1 = new Vector();
		v1.add("e");
		v1.add("e2");
		v1.add("e3");
		c2 = new JComboBox(v1);
		String v2[] = new String[2];
		v2[0] = "a1";
		v2[1] = "a2";
		c3 = new JComboBox(v2);
		Container cont = this.getContentPane();
		cont.setLayout(new GridLayout(1,4));
		cont.add(b);
		cont.add(c1);
		cont.add(c2);
		cont.add(c3);
		this.setLocationRelativeTo(null);
		b.addActionListener(this);
		this.addWindowListener(new WindowAdapter()
		{
			public void windowClosing(WindowEvent we)
			{
				System.exit(0);
			}
		});
		this.pack();
		this.setVisible(true);
	}

	public void actionPerformed(ActionEvent ae)
	{
		Object source = ae.getSource();
		if (source == b)
		{
			c1.addItem("a");
			c2.addItem("a");
			c3.addItem("a");
		}
	}
}

I've used Java 1.5.0_14.
 
Thanks for your response!!!!! I can see now what I was doing wrong. Too much time in .net land
 
Thanks for the star.

Another thing. You said "I cannot re-add the component to the panel."

This can be done, just delete the component and re-add it in the same place/position:
Code:
...
public void actionPerformed(ActionEvent ae)
	{
		Object source = ae.getSource();
		if (source == b)
		{
			cont.remove(c1);
			c1 = new JComboBox();
		//Re-load and re-add data to the combo
			c1.addItem("removed and added");
			cont.add(c1,1);
			this.pack();
		}
	}
...

A little more tricky if using mixed Layouts, but can be done.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top