Java Forums

Main Menu
Home
Today's Posts
FAQ
Search
Contact Us

Java Network
Linux Archive
Java Tips
Java Tips Blog

Sponsored Links





Welcome to the Java Forums.

You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community, you will:

  • have access to post topics
  • communicate privately with other members (PM)
  • not see advertisements between posts
  • have the possibility to earn one of our surprises if you are an active member
  • access many other special features that will be introduced later.

Registration is fast, simple and absolutely free so please, join our community today!

If you have any problems with the registration process or your account login, please contact us.

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 05-13-2008, 01:27 PM
Member
 
Join Date: May 2008
Posts: 9
Judoon_Platoon is on a distinguished road
[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.

Code:
final String CAMPUS_NAME [] = {"Rockhampton", "Brisbane", "Springfield", "Quohog"}; final double CAMPUS_PERCENTAGE [] = {24, 30, 12, 39};
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?

Here is the code that populates the Choice ComboBox from the Parralel Array:

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); }
Any Help would be appreciated
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 05-13-2008, 01:49 PM
sukatoa's Avatar
Senior Member
 
Join Date: Jan 2008
Location: Cebu City, Philippines
Posts: 527
sukatoa is on a distinguished road
Send a message via Yahoo to sukatoa
copy the value returned from getSelectedIndex() method.

and use it for accessing the other array....
__________________
A specific, detailed, simple, well elaborated, and "tested before asking" question may gather more quick replies. hopefully
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
Bookmark Post in Technorati
Reply With Quote
  #3 (permalink)  
Old 05-13-2008, 02:29 PM
Member
 
Join Date: May 2008
Posts: 9
Judoon_Platoon is on a distinguished road
Ah I am quite new at this Sorry, im not sure i Follow. Here is the code for my project so far:

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] + ""); } } }
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 ComboBox
Bookmark Post in Technorati
Reply With Quote
  #4 (permalink)  
Old 05-14-2008, 04:20 AM
sukatoa's Avatar
Senior Member
 
Join Date: Jan 2008
Location: Cebu City, Philippines
Posts: 527
sukatoa is on a distinguished road
Send a message via Yahoo to sukatoa
Quote:
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 checkbox, you specifically use a fixed value to access the array.....

In combobox, use the getSelectedIndex() method, get it's value and use it to access the desired array.....
__________________
A specific, detailed, simple, well elaborated, and "tested before asking" question may gather more quick replies. hopefully
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
Bookmark Post in Technorati
Reply With Quote
  #5 (permalink)  
Old 05-14-2008, 05:46 AM
Eku Eku is offline
Senior Member
 
Join Date: May 2008
Location: Makati, Philippines
Posts: 228
Eku is on a distinguished road
Here matey. I made a simple example for you using Action Listener.

Main Class
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(); } }) ; } }
Gui Class
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); } }
I hope that helps.

Last edited by Eku : 05-14-2008 at 05:48 AM.
Bookmark Post in Technorati
Reply With Quote
  #6 (permalink)  
Old 05-14-2008, 08:36 AM
Member
 
Join Date: May 2008
Posts: 9
Judoon_Platoon is on a distinguished road
Thanks alot their Eku, really appreciate it, it works fine and does exactly what I need it too do.

Cheers
Bookmark Post in Technorati
Reply With Quote
  #7 (permalink)  
Old 05-14-2008, 09:22 AM
Eku Eku is offline
Senior Member
 
Join Date: May 2008
Location: Makati, Philippines
Posts: 228
Eku is on a distinguished road
Your Welcome Matey. =) Just mark this thread as [SOLVED] ^_^
Bookmark Post in Technorati
Reply With Quote
  #8 (permalink)  
Old 05-14-2008, 10:35 AM
Member
 
Join Date: May 2008
Posts: 9
Judoon_Platoon is on a distinguished road
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:

Code:
int ptr = jcombo1.getSelectedIndex(); jcombo2.setSelectedIndex(ptr); String value = jcombo2.getSelectedItem ();
I am pretty sure the bottom line should get the value of the Percentage checkbox (jcombo2) but all I get is this error:

Code:
java:225: incompatible types found : java.lang.Object required: java.lang.String String value = jcombo2.getSelectedItem ();
I can't work out why it is doing that
Bookmark Post in Technorati
Reply With Quote
  #9 (permalink)  
Old 05-14-2008, 11:34 AM
Eku Eku is offline
Senior Member
 
Join Date: May 2008
Location: Makati, Philippines
Posts: 228
Eku is on a distinguished road
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:
Code:
String Temp = (String)jcombo2.getSelectedItem(); //OR String Temp2 = jcombo2.getSelectedItem().toString();
Bookmark Post in Technorati
Reply With Quote
  #10 (permalink)  
Old 05-14-2008, 02:14 PM
Member
 
Join Date: May 2008
Posts: 9
Judoon_Platoon is on a distinguished road
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
Bookmark Post in Technorati
Reply With Quote
  #11 (permalink)  
Old 05-15-2008, 04:33 AM
Eku Eku is offline
Senior Member
 
Join Date: May 2008
Location: Makati, Philippines
Posts: 228
Eku is on a distinguished road
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. =)
Bookmark Post in Technorati
Reply With Quote
  #12 (permalink)  
Old 05-15-2008, 05:44 AM
sukatoa's Avatar
Senior Member
 
Join Date: Jan 2008
Location: Cebu City, Philippines
Posts: 527
sukatoa is on a distinguished road
Send a message via Yahoo to sukatoa
Quote:
Sometimes the most difficult error is just a simple typo error. =)
and that should be avoided....
__________________
A specific, detailed, simple, well elaborated, and "tested before asking" question may gather more quick replies. hopefully
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
Bookmark Post in Technorati
Reply With Quote
  #13 (permalink)  
Old 05-15-2008, 07:25 AM
Eku Eku is offline
Senior Member
 
Join Date: May 2008
Location: Makati, Philippines
Posts: 228
Eku is on a distinguished road
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.
Bookmark Post in Technorati
Reply With Quote
  #14 (permalink)  
Old 05-25-2008, 03:21 PM
Member
 
Join Date: May 2008
Posts: 1
Taz_84 is on a distinguished road
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).

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
When i run that i get the following Errors and for the life of me i cannot seem to understand them.

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
It would be highly appreciated if you explain to me in detail where im going wrong so i can learn from my mistake.

Thank you for taking the time to read my request. hope to hear back from you soon.

Taz
Bookmark Post in Technorati
Reply With Quote
  #15 (permalink)  
Old 10-01-2008, 11:07 PM
Member
 
Join Date: Oct 2008
Location: Latvia
Posts: 1
Tresurimase is on a distinguished road
Send a message via ICQ to Tresurimase
Hi from Latvia
Hi

This is a terrific forum, I been lurking here for a while.
Now I decieded to register.


theshyzirty
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
HashMap and ComboBox banie AWT / Swing 2 03-26-2008 01:58 AM
Need help with T/F and Multiple Choice sayso36 Advanced Java 0 03-12-2008 06:39 PM
Using java.awt.Choice Java Tip Java Tips 0 01-02-2008 08:33 PM
ComboBox with database options Goldy Advanced Java 0 12-01-2007 11:43 PM
i need assistance with a string triggered loop please! Phobos0001 New To Java 9 11-14-2007 04:44 PM


All times are GMT +3. The time now is 08:18 AM.


VBulletin, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2007, Crawlability, Inc.
Copyright ©2006 - 2007, www.java-forums.org