Results 1 to 6 of 6
  1. #1
    Join Date
    Oct 2009
    Posts
    88
    Rep Power
    0

    Default Add element to Jcombobox

    How to add elements to jcombo box after shown on the screen

  2. #2
    crikey is offline Senior Member
    Join Date
    Jul 2010
    Posts
    125
    Rep Power
    0

    Default

    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.

  3. #3
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default

    Quote Originally Posted by anilkumar_vist View Post
    How to add elements to jcombo box after shown on the screen
    What have you tried? What problems are you currently having with this attempt? Have you gone through the tutorial?

  4. #4
    prasad.vara is offline Member
    Join Date
    Jul 2010
    Location
    Hyderabad
    Posts
    21
    Rep Power
    0

    Default

    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 ();
    }




    }

  5. #5
    prasad.vara is offline Member
    Join Date
    Jul 2010
    Location
    Hyderabad
    Posts
    21
    Rep Power
    0

    Thumbs up 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 ();
    }




    }

  6. #6
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,189
    Rep Power
    19

    Default

    Quote Originally Posted by prasad.vara View Post
    I attached the code for creating JComboBox and adding item to JComboBox here.try it once.
    Do you mean twice?:D

Similar Threads

  1. How to get a node value of an XML element?
    By rsenth99 in forum Java Servlet
    Replies: 9
    Last Post: 02-15-2010, 11:35 AM
  2. Replies: 2
    Last Post: 12-10-2009, 01:58 PM
  3. How to access element in a CSV?
    By venkatteshb in forum New To Java
    Replies: 11
    Last Post: 08-17-2008, 11:37 AM
  4. Max element in an Array
    By mew in forum New To Java
    Replies: 5
    Last Post: 12-03-2007, 05:26 PM
  5. a no such element exception
    By headlice1 in forum New To Java
    Replies: 1
    Last Post: 08-07-2007, 05:36 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •