Results 1 to 15 of 15
- 05-13-2008, 11:27 AM #1
Member
- Join Date
- May 2008
- Posts
- 9
- Rep Power
- 0
[SOLVED] Parallel Arrays with Choice ComboBox - need assistance
I am quite new at Java as I am studying a course on it at uni. My problem is with Parallel arrays, they seem to be a right pain in the backside. basically I have 2 Parallel arrays, one with four values (which are strings) and the other also with four values (Integers). At the moment I have a Choice ComboBox being populated by the first Parallel Array (containing Strings), and what I need is for when one of the values in the ComboBox is clicked, it displays the value from the other Parallel array.
So the ComboBox has Rockhampton, Brisbane etc and when say "Rockhampton" is clicked I need "24" to be displayed in a Label, or if Brisbane is clicked then "30" is displayed. I already have a similar thing happening with Parralel arrays populating a checkbox group, but I can't seem to work this out?Java Code:final String CAMPUS_NAME [] = {"Rockhampton", "Brisbane", "Springfield", "Quohog"}; final double CAMPUS_PERCENTAGE [] = {24, 30, 12, 39};
Here is the code that populates the Choice ComboBox from the Parralel Array:
Any Help would be appreciatedJava Code:for( int j = 0; j < CAMPUS_NAME.length; j++ ) { if (j==0) { jcombo1.add ("Please Choose"); } ChoiceCname.add (CAMPUS_NAME[j] ); ChoiceCname.addItemListener(this); }
- 05-13-2008, 11:49 AM #2
copy the value returned from getSelectedIndex() method.
and use it for accessing the other array....freedom exists in the world of ideas
- 05-13-2008, 12:29 PM #3
Member
- Join Date
- May 2008
- Posts
- 9
- Rep Power
- 0
Ah I am quite new at this Sorry, im not sure i Follow. Here is the code for my project so far:
As you can see the thing I am actually trying to do I have already done with a Checkbox Group, I just dont know how to do it with a ComboBoxJava Code:import java.awt.*; // For GUI components import java.awt.event.*; // For event handling import javax.swing.JOptionPane; // For Swing Dialogs import java.text.DecimalFormat; // For DecimalFormat. import javax.swing.*; // For Swing components, frames, etc. public class assignment extends Frame implements ItemListener, ActionListener { Label TurbineTypeLabel = new Label ("Wind Turbine Type:"); Label TurbineInformationLabel = new Label (""); Label Campus_Details_Label = new Label (""); TextArea TurbineInfoTextArea = new TextArea ("", 10, 30); final String TURBINE_NAME [] = {"Judoon 3X", "Gallifrey 3000", "Veridian AX25", "Kasterborous M4", "Axxon 300", "Dradis CF9", "Kenobi 0B1", "J-Dorian"}; final double OUTPUT_CAPACITY [] = {1300, 1100, 2000, 2300, 1400, 2600, 2200, 2900}; final double COST [] = {11000, 13000, 10000, 17000, 20000, 19000, 21000, 24000}; final String CAMPUS_NAME [] = {"Rockhampton", "Brisbane", "Springfield", "Quohog"}; final double CAMPUS_PERCENTAGE [] = {24, 30, 12, 39}; CheckboxGroup Wind_Turbine_CheckboxGroup = new CheckboxGroup (); Checkbox Wind_Turbine_Checkbox [] = new Checkbox [TURBINE_NAME.length]; Checkbox Hidden_Checkbox = new Checkbox ("", true, Wind_Turbine_CheckboxGroup); Choice jcombo1 = new Choice(); Choice NumTurbines = new Choice(); Button AddButton = new Button("Add"); public assignment () { Panel TurbineLayoutPanel = new Panel (new BorderLayout ()); TurbineLayoutPanel.add (TurbineTypeLabel, BorderLayout.WEST); TurbineLayoutPanel.add (TurbineInformationLabel, BorderLayout.CENTER); // Define the Layout. this.setLayout (new GridLayout (4, 2, 1, 1)); for (int i = 0; i < TURBINE_NAME.length; i++) { // Create the radio button in the group and assign it a name. Wind_Turbine_Checkbox [i] = new Checkbox (TURBINE_NAME [i], false, Wind_Turbine_CheckboxGroup); // Make the radio button active (for mouse clicks). Wind_Turbine_Checkbox [i].addItemListener(this); // Add the radio button to the user interface. Panel TurbinePanel = new Panel (new BorderLayout ()); TurbinePanel.add (TurbineLayoutPanel, BorderLayout.SOUTH); TurbinePanel.add (Wind_Turbine_Checkbox [i], BorderLayout.NORTH); add (TurbinePanel); } //CampusName = new String[CAMPUS_NAME.length]; for( int j = 0; j < CAMPUS_NAME.length; j++ ) { if (j==0) { jcombo1.add ("Please Choose"); } jcombo1.add (CAMPUS_NAME[j] ); jcombo1.addItemListener(this); } Panel choice_panel = new Panel (new GridLayout (1, 1, 1, 1)); choice_panel.add (jcombo1); choice_panel.add (Campus_Details_Label); add (choice_panel); NumTurbines.add ("5"); NumTurbines.add ("10"); NumTurbines.add ("15"); NumTurbines.add ("20"); NumTurbines.add ("25"); NumTurbines.add ("30"); NumTurbines.add ("35"); NumTurbines.add ("40"); NumTurbines.add ("45"); NumTurbines.add ("50"); add (TurbineInfoTextArea); TurbineInfoTextArea.setEditable (false); AddButton.addActionListener (this); add (AddButton); addWindowListener ( new WindowAdapter() { public void windowClosing(WindowEvent e) { System.exit(0); // Program is terminating normally. } } ); // addWindowListener } public void actionPerformed (ActionEvent e) { } public static void main (String[] args) { assignment My_App = new assignment (); My_App.setBounds (10, 10, 700, 600); My_App.setTitle ("My_App v1.0"); My_App.setVisible (true); } // public static void main (String[] args) public void itemStateChanged(ItemEvent e) { int i = 0; boolean Item_Found = false; // Determine which checkbox is currently selected. while ((i < TURBINE_NAME.length) && (Item_Found == false)) { // If the checkbox is selected ... if (Wind_Turbine_Checkbox [i].getState () == true) { Display_Selected_Turbine (i); Item_Found = true; } i++; } } private void Display_Selected_Turbine (int Selected_Turbine) { // Display details of selected Turbine. if (Selected_Turbine >= 0) { TurbineInformationLabel.setText (TURBINE_NAME [Selected_Turbine] + ": " + OUTPUT_CAPACITY [Selected_Turbine] + "W, " + "$" + COST [Selected_Turbine] + ""); } } }
- 05-14-2008, 02:20 AM #4
In checkbox, you specifically use a fixed value to access the array.....I am actually trying to do I have already done with a Checkbox Group, I just dont know how to do it with a ComboBox
In combobox, use the getSelectedIndex() method, get it's value and use it to access the desired array.....freedom exists in the world of ideas
- 05-14-2008, 03:46 AM #5
Senior Member
- Join Date
- May 2008
- Location
- Makati, Philippines
- Posts
- 234
- Rep Power
- 6
Here matey. I made a simple example for you using Action Listener.
Main Class
Gui ClassJava Code:import javax.swing.SwingUtilities; public class Main { public static void main(String[] args) { SwingUtilities.invokeLater(new Runnable(){ public void run(){ sample win = new sample(); win.Gui(); } }) ; } }
I hope that helps.Java Code:import java.awt.Container; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.JComboBox; import javax.swing.JFrame; public class sample extends JFrame implements ActionListener{ final String CAMPUS_NAME [] = {"Rockhampton", "Brisbane", "Springfield", "Quohog"}; final String CAMPUS_PERCENTAGE [] = {"24", "30", "12", "39"}; private JComboBox Combo2 = new JComboBox(CAMPUS_PERCENTAGE); private JComboBox Combo1 = new JComboBox(CAMPUS_NAME); public void Gui(){ JFrame Samp = new JFrame("Combo Box Practice"); Samp.setBounds(100, 100, 400, 300); Samp.setVisible(true); Container container = Samp.getContentPane(); container.setLayout(null); Combo1.setBounds(5, 5, 150, 25); Combo1.addActionListener(this); container.add(Combo1); Combo2.setBounds(5, 35, 150, 25); container.add(Combo2); } public void actionPerformed(ActionEvent e) { int ptr = Combo1.getSelectedIndex(); Combo2.setSelectedIndex(ptr); } }Last edited by Eku; 05-14-2008 at 03:48 AM.
- 05-14-2008, 06:36 AM #6
Member
- Join Date
- May 2008
- Posts
- 9
- Rep Power
- 0
Thanks alot their Eku, really appreciate it, it works fine and does exactly what I need it too do.
Cheers :)
- 05-14-2008, 07:22 AM #7
Senior Member
- Join Date
- May 2008
- Location
- Makati, Philippines
- Posts
- 234
- Rep Power
- 6
Your Welcome Matey. =) Just mark this thread as [SOLVED] ^_^
- 05-14-2008, 08:35 AM #8
Member
- Join Date
- May 2008
- Posts
- 9
- Rep Power
- 0
Ah one minor issue which I have come accross actually, I need the value of the Percentage to be displayed in a Label, at the moment it is displayed in another ComboBox, so I figured it would be as easy as going:
I am pretty sure the bottom line should get the value of the Percentage checkbox (jcombo2) but all I get is this error:Java Code:int ptr = jcombo1.getSelectedIndex(); jcombo2.setSelectedIndex(ptr); String value = jcombo2.getSelectedItem ();
I can't work out why it is doing that :mad:Java Code:java:225: incompatible types found : java.lang.Object required: java.lang.String String value = jcombo2.getSelectedItem ();
- 05-14-2008, 09:34 AM #9
Senior Member
- Join Date
- May 2008
- Location
- Makati, Philippines
- Posts
- 234
- Rep Power
- 6
because the return value of the jcombo2.getSelectedItem() is an object. You must first convert it to string. ^_^
You may use any of the ff:
Java Code:String Temp = (String)jcombo2.getSelectedItem(); //OR String Temp2 = jcombo2.getSelectedItem().toString();
- 05-14-2008, 12:14 PM #10
Member
- Join Date
- May 2008
- Posts
- 9
- Rep Power
- 0
Ah that worked a charm, thanks very much for you help, I shall mark this thread as solved. But i fear this may be the first of a few problems to come, this assignment looks quite complex (for me anyway)
Cheers again :)
- 05-15-2008, 02:33 AM #11
Senior Member
- Join Date
- May 2008
- Location
- Makati, Philippines
- Posts
- 234
- Rep Power
- 6
Just try making your own code first and if you have some problems and errors consult the books or Mr.Google first. If still you cant resolve it, you can post it here. ^_^
Sometimes the most difficult error is just a simple typo error. =)
- 05-15-2008, 03:44 AM #12
and that should be avoided.... :)Sometimes the most difficult error is just a simple typo error. =)freedom exists in the world of ideas
- 05-15-2008, 05:25 AM #13
Senior Member
- Join Date
- May 2008
- Location
- Makati, Philippines
- Posts
- 234
- Rep Power
- 6
That is based from my own experienced. If your coding about 2000++ Lines of java codes and a bunch of class. There is a high probability of typo error even with the Help of Coffee and Energy Drinks. :)
Mind only knows what lies near the heart, it alone sees the depth of the soul.
- 05-25-2008, 01:21 PM #14
Member
- Join Date
- May 2008
- Posts
- 7
- Rep Power
- 0
Thread: Parallel Arrays with Choice ComboBox - need assistance
Hi there Eku, Ive been studying what you have informed the other person regardin the Parallel Arrays with Choice. Because i have the same problem. Unforunately for me i STILL couldnt work it out. I am also very VERY new to java and this Choice matter is really bugging me because so far its been a smooth sailing.... Anyway, i cannot give you my actual code because its directly related to my assignment.. however to understand the matter i have made a VERY VERY simple code. It only contains a Choice and a Output Label. I would highly appreciate it if you could kindly take a look at tell me exactly where im going wrong? ( i think its in the itemStateChanged method).
When i run that i get the following Errors and for the life of me i cannot seem to understand them.Java Code:import java.awt.*; import java.awt.event.*; import javax.swing.*; import java.applet.*; public class choice_array extends Applet implements ItemListener { //Car Array final String Car_Model [] = {"Ford", "Holden", "Toyota", "Honda", "Austin Martin"}; final int Car_Price [] = {33000, 40000, 25000, 31000, 90000}; //A new choice component has been created. The data of the choice menu are retrieved from the Car Model Array. Choice Car_Choice [] = new Choice [Car_Model.length]; Label Update_Label = new Label (""); public void init () { this.setLayout (new BorderLayout()); for (int i =0; i<Car_Model.length; i++) { Car_Choice.add (Car_Model [i]); Car_Choice.addItemListener (this); } add (Car_Choice, BorderLayout.NORTH); add (Update_Label, BorderLayout.SOUTH); }//init () public void itemStateChanged (ItemEvent choice) { int Selected_index = Car_Choice.getSelectedIndex (); Update_Label.setSelectedIndex (Selected_index); }// itemStateChanged }// Class
It would be highly appreciated if you explain to me in detail where im going wrong so i can learn from my mistake.Java Code:C:\Users\Tanzim\Desktop\choice_array.java:40: cannot find symbol symbol : method add(java.lang.String) location: class java.awt.Choice[] Car_Choice.add (Car_Model [i]); ^ C:\Users\Tanzim\Desktop\choice_array.java:41: cannot find symbol symbol : method addItemListener(choice_array) location: class java.awt.Choice[] Car_Choice.addItemListener (this); ^ C:\Users\Tanzim\Desktop\choice_array.java:47: cannot find symbol symbol : method add(java.awt.Choice[],java.lang.String) location: class choice_array add (Car_Choice, BorderLayout.NORTH); ^ C:\Users\Tanzim\Desktop\choice_array.java:59: cannot find symbol symbol : method getSelectedIndex() location: class java.awt.Choice[] int Selected_index = Car_Choice.getSelectedIndex (); ^ C:\Users\Tanzim\Desktop\choice_array.java:61: cannot find symbol symbol : method setSelectedIndex(int) location: class java.awt.Label Update_Label.setSelectedIndex (Selected_index); ^ 5 errors Tool completed with exit code 1
Thank you for taking the time to read my request. hope to hear back from you soon.
Taz
- 10-01-2008, 09:07 PM #15
Member
- Join Date
- Oct 2008
- Location
- Latvia
- Posts
- 1
- Rep Power
- 0
Similar Threads
-
HashMap and ComboBox
By banie in forum AWT / SwingReplies: 2Last Post: 03-25-2008, 11:58 PM -
Need help with T/F and Multiple Choice
By sayso36 in forum Advanced JavaReplies: 0Last Post: 03-12-2008, 04:39 PM -
Using java.awt.Choice
By Java Tip in forum Java TipReplies: 0Last Post: 01-02-2008, 06:33 PM -
ComboBox with database options
By Goldy in forum Advanced JavaReplies: 0Last Post: 12-01-2007, 09:43 PM -
i need assistance with a string triggered loop please!
By Phobos0001 in forum New To JavaReplies: 9Last Post: 11-14-2007, 02:44 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks