Results 1 to 5 of 5
- 12-12-2010, 07:07 AM #1
Member
- Join Date
- Dec 2010
- Posts
- 3
- Rep Power
- 0
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?
- 12-12-2010, 07:14 AM #2
Senior Member
- Join Date
- Mar 2010
- Posts
- 953
- Rep Power
- 4
Show us what you wrote, please. Please wrap your code in CODE tags when you do (the # icon in the editor toolbar).
-Gary-
- 12-12-2010, 07:44 AM #3
Member
- Join Date
- Dec 2010
- Posts
- 3
- Rep Power
- 0
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 07:50 AM.
- 12-12-2010, 07:54 AM #4
Senior Member
- Join Date
- Mar 2010
- Posts
- 953
- Rep Power
- 4
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.
Now you can access them dynamically by number -- combos[0] - combos[4].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; }
-Gary-
- 12-12-2010, 07:59 AM #5
Member
- Join Date
- Dec 2010
- Posts
- 3
- Rep Power
- 0
Similar Threads
-
comboboxes
By duncan913 in forum New To JavaReplies: 3Last Post: 12-09-2010, 06:26 AM -
Decimal Fill up
By maya700 in forum New To JavaReplies: 4Last Post: 07-28-2010, 06:15 PM -
Retrieving data from text file and putting it into comboboxes
By the reporter in forum AWT / SwingReplies: 3Last Post: 06-02-2010, 06:58 PM -
fill table with cells
By BigBear in forum AWT / SwingReplies: 3Last Post: 01-26-2010, 09:22 PM -
How to Fill Arc in Java
By Java Tip in forum java.awtReplies: 0Last Post: 06-23-2008, 11:14 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks