Results 1 to 6 of 6
Thread: Add element to Jcombobox
- 08-30-2010, 02:37 PM #1
Member
- Join Date
- Oct 2009
- Posts
- 88
- Rep Power
- 0
- 08-30-2010, 04:14 PM #2
Senior Member
- Join Date
- Jul 2010
- Posts
- 124
- Rep Power
- 0
You need to declare the combobox as an array for however many items you need. i.e.:
Java Code:JComboBox[1] myComboBox = new JComboBox[1](); String[10] myString = {"0","1","2","3","4","5","6","7","8","9"}; myComboBox[0] = (myString); //continue processing until you get a state changed... myComboBox[1] = "greetings"; //Now, to access the selections later, declare a variable private above the methods signature (or public,etc): private String allChoices; //.... public void itemStateChanged(ItemEvent e){ int selection; selection = e.getSelectedIndex(); if (selection == 0){ } //...continue }Last edited by crikey; 08-30-2010 at 04:23 PM.
-
- 08-31-2010, 07:53 AM #4
Member
- Join Date
- Jul 2010
- Location
- Hyderabad
- Posts
- 21
- Rep Power
- 0
I attached the code for creating JComboBox and adding item to JComboBox here.try it once.
public class Addcombo extends JFrame implements ActionListener {
JComboBox jComboBox1 = new javax.swing.JComboBox();
JButton b=new JButton("Add");
public Addcombo (){
super("combox");
jComboBox1.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "item1","item2","item3"}));
JFrame j=new JFrame();
getContentPane().add(jComboBox1,BorderLayout.CENTE R);
getContentPane().add(b,BorderLayout.SOUTH);
setVisible(true);
pack();
j.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
b.addActionListener(this);
}
public void actionPerformed(ActionEvent ae)
{
DefaultComboBoxModel dc=(DefaultComboBoxModel)jComboBox1.getModel();
dc.addElement("item4");
}
public static void main(String s[]){
Addcombo p=new Addcombo ();
}
}
- 08-31-2010, 07:55 AM #5
Member
- Join Date
- Jul 2010
- Location
- Hyderabad
- Posts
- 21
- Rep Power
- 0
add element to JcomboBox
I attached the code for creating JComboBox and adding item to JComboBox here.try it once.
public class Addcombo extends JFrame implements ActionListener {
JComboBox jComboBox1 = new javax.swing.JComboBox();
JButton b=new JButton("Add");
public Addcombo (){
super("combox");
jComboBox1.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "item1","item2","item3"}));
JFrame j=new JFrame();
getContentPane().add(jComboBox1,BorderLayout.CENTE R);
getContentPane().add(b,BorderLayout.SOUTH);
setVisible(true);
pack();
j.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
b.addActionListener(this);
}
public void actionPerformed(ActionEvent ae)
{
DefaultComboBoxModel dc=(DefaultComboBoxModel)jComboBox1.getModel();
dc.addElement("item4");
}
public static void main(String s[]){
Addcombo p=new Addcombo ();
}
}
- 08-31-2010, 11:56 AM #6
Similar Threads
-
How to get a node value of an XML element?
By rsenth99 in forum Java ServletReplies: 9Last Post: 02-15-2010, 11:35 AM -
Activate JComboBox 1 when object is selected in JComboBox 2...
By bahumbaba in forum AWT / SwingReplies: 2Last Post: 12-10-2009, 01:58 PM -
How to access element in a CSV?
By venkatteshb in forum New To JavaReplies: 11Last Post: 08-17-2008, 11:37 AM -
Max element in an Array
By mew in forum New To JavaReplies: 5Last Post: 12-03-2007, 05:26 PM -
a no such element exception
By headlice1 in forum New To JavaReplies: 1Last Post: 08-07-2007, 05:36 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks