Results 1 to 5 of 5
  1. #1
    Stevemc is offline Member
    Join Date
    Dec 2010
    Posts
    3
    Rep Power
    0

    Default Trying to fill multiple comboboxes by using one.

    I am trying to learn java form a book, making very simple programs to see how it works. I am trying to use one combobox to fill in the other 5 with differant values. I use an action listener (works) to fill in one. I tried an array to store the combobox names so i could use a for loop, but the .addItem does not seem to work with a variable. I could be doing it all wrong.

    What is the best way to do this?

  2. #2
    gcalvin is offline Senior Member
    Join Date
    Mar 2010
    Posts
    952
    Rep Power
    10

    Default

    Show us what you wrote, please. Please wrap your code in CODE tags when you do (the # icon in the editor toolbar).

    -Gary-

  3. #3
    Stevemc is offline Member
    Join Date
    Dec 2010
    Posts
    3
    Rep Power
    0

    Default

    I was trying to replace jcomobox1 with a a variable, so it would change comboboxes 1-5. I tried various arrays.

    I am not even sure if this is the right approch.

    Java Code:
        String [] cb  = {"jComboBox1","jComboBox2","jComboBox3","jComboBox4         ","jComboBox5"};
    
       public class BaseSkillListen implements ActionListener{
        public void actionPerformed(ActionEvent e){
    
         for(int i = 1; i < 5; i++)
             
            cb(i).removeAllItems();             
        //    jComboBox1.addItem("Skill");
        //    jComboBox1.addItem("Dist");
        //    jComboBox1.addItem("L");
        }
        }
    Last edited by Stevemc; 12-12-2010 at 08:50 AM.

  4. #4
    gcalvin is offline Senior Member
    Join Date
    Mar 2010
    Posts
    952
    Rep Power
    10

    Default

    OK, to do that, you need an array of JComboBox objects. You are creating an array of String objects, which is not what you want.
    Java Code:
        JComboBox[] combos = new JComboBox[5];
        for (int i = 0; i < 5; i++) {
            JComboBox combo = new JComboBox();
            // do other initialization, such as cb.addItem(...)
            combos[i] = combo;
        }
    Now you can access them dynamically by number -- combos[0] - combos[4].

    -Gary-

  5. #5
    Stevemc is offline Member
    Join Date
    Dec 2010
    Posts
    3
    Rep Power
    0

Similar Threads

  1. comboboxes
    By duncan913 in forum New To Java
    Replies: 3
    Last Post: 12-09-2010, 07:26 AM
  2. Decimal Fill up
    By maya700 in forum New To Java
    Replies: 4
    Last Post: 07-28-2010, 07:15 PM
  3. Replies: 3
    Last Post: 06-02-2010, 07:58 PM
  4. fill table with cells
    By BigBear in forum AWT / Swing
    Replies: 3
    Last Post: 01-26-2010, 10:22 PM
  5. How to Fill Arc in Java
    By Java Tip in forum java.awt
    Replies: 0
    Last Post: 06-24-2008, 12:14 AM

Posting Permissions

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