|
|
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.
|
|

05-13-2008, 01:27 PM
|
|
Member
|
|
Join Date: May 2008
Posts: 9
|
|
|
[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.
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:
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
|
|

05-13-2008, 01:49 PM
|
 |
Senior Member
|
|
Join Date: Jan 2008
Location: Cebu City, Philippines
Posts: 527
|
|
|
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.
|
|

05-13-2008, 02:29 PM
|
|
Member
|
|
Join Date: May 2008
Posts: 9
|
|
Ah I am quite new at this Sorry, im not sure i Follow. Here is the code for my project so far:
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
|
|

05-14-2008, 04:20 AM
|
 |
Senior Member
|
|
Join Date: Jan 2008
Location: Cebu City, Philippines
Posts: 527
|
|
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.
|
|

05-14-2008, 05:46 AM
|
|
Senior Member
|
|
Join Date: May 2008
Location: Makati, Philippines
Posts: 228
|
|
Here matey. I made a simple example for you using Action Listener.
Main Class
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
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.
|
|

05-14-2008, 08:36 AM
|
|
Member
|
|
Join Date: May 2008
Posts: 9
|
|
Thanks alot their Eku, really appreciate it, it works fine and does exactly what I need it too do.
Cheers 
|
|

05-14-2008, 09:22 AM
|
|
Senior Member
|
|
Join Date: May 2008
Location: Makati, Philippines
Posts: 228
|
|
|
Your Welcome Matey. =) Just mark this thread as [SOLVED] ^_^
|
|

05-14-2008, 10:35 AM
|
|
Member
|
|
Join Date: May 2008
Posts: 9
|
|
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:
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:
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 
|
|

05-14-2008, 11:34 AM
|
|
Senior Member
|
|
Join Date: May 2008
Location: Makati, Philippines
Posts: 228
|
|
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:
String Temp = (String)jcombo2.getSelectedItem();
//OR
String Temp2 = jcombo2.getSelectedItem().toString();
|
|

05-14-2008, 02:14 PM
|
|
Member
|
|
Join Date: May 2008
Posts: 9
|
|
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, 04:33 AM
|
|
Senior Member
|
|
Join Date: May 2008
Location: Makati, Philippines
Posts: 228
|
|
|
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, 05:44 AM
|
 |
Senior Member
|
|
Join Date: Jan 2008
Location: Cebu City, Philippines
Posts: 527
|
|
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.
|
|

05-15-2008, 07:25 AM
|
|
Senior Member
|
|
Join Date: May 2008
Location: Makati, Philippines
Posts: 228
|
|
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, 03:21 PM
|
|
Member
|
|
Join Date: May 2008
Posts: 1
|
|
|
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).
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.
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
|
|

10-01-2008, 11:07 PM
|
|
Member
|
|
Join Date: Oct 2008
Location: Latvia
Posts: 1
|
|
|
Hi from Latvia
Hi
This is a terrific forum, I been lurking here for a while.
Now I decieded to register.
theshyzirty
|
|
| Thread Tools |
|
|
| Display Modes |
Linear Mode
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
|