Results 1 to 15 of 15
  1. #1
    Join Date
    May 2008
    Posts
    9
    Rep Power
    0

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

    Java 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:

    Java 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

  2. #2
    sukatoa's Avatar
    sukatoa is offline Senior Member
    Join Date
    Jan 2008
    Location
    Cebu City, Philippines
    Posts
    556
    Rep Power
    7

    Default

    copy the value returned from getSelectedIndex() method.

    and use it for accessing the other array....
    freedom exists in the world of ideas

  3. #3
    Join Date
    May 2008
    Posts
    9
    Rep Power
    0

    Default

    Ah I am quite new at this Sorry, im not sure i Follow. Here is the code for my project so far:

    Java 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

  4. #4
    sukatoa's Avatar
    sukatoa is offline Senior Member
    Join Date
    Jan 2008
    Location
    Cebu City, Philippines
    Posts
    556
    Rep Power
    7

    Default

    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.....
    freedom exists in the world of ideas

  5. #5
    Eku
    Eku is offline Senior Member
    Join Date
    May 2008
    Location
    Makati, Philippines
    Posts
    234
    Rep Power
    7

    Default

    Here matey. I made a simple example for you using Action Listener.

    Main Class
    Java 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
    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);
        }
    }
    I hope that helps.
    Last edited by Eku; 05-14-2008 at 04:48 AM.

  6. #6
    Join Date
    May 2008
    Posts
    9
    Rep Power
    0

    Default

    Thanks alot their Eku, really appreciate it, it works fine and does exactly what I need it too do.

    Cheers :)

  7. #7
    Eku
    Eku is offline Senior Member
    Join Date
    May 2008
    Location
    Makati, Philippines
    Posts
    234
    Rep Power
    7

    Default

    Your Welcome Matey. =) Just mark this thread as [SOLVED] ^_^

  8. #8
    Join Date
    May 2008
    Posts
    9
    Rep Power
    0

    Default

    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:

    Java 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:

    Java 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 :mad:

  9. #9
    Eku
    Eku is offline Senior Member
    Join Date
    May 2008
    Location
    Makati, Philippines
    Posts
    234
    Rep Power
    7

    Default

    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();

  10. #10
    Join Date
    May 2008
    Posts
    9
    Rep Power
    0

    Default

    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 :)

  11. #11
    Eku
    Eku is offline Senior Member
    Join Date
    May 2008
    Location
    Makati, Philippines
    Posts
    234
    Rep Power
    7

    Default

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

  12. #12
    sukatoa's Avatar
    sukatoa is offline Senior Member
    Join Date
    Jan 2008
    Location
    Cebu City, Philippines
    Posts
    556
    Rep Power
    7

    Default

    Sometimes the most difficult error is just a simple typo error. =)
    and that should be avoided.... :)
    freedom exists in the world of ideas

  13. #13
    Eku
    Eku is offline Senior Member
    Join Date
    May 2008
    Location
    Makati, Philippines
    Posts
    234
    Rep Power
    7

    Default

    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.

  14. #14
    Taz_84 is offline Member
    Join Date
    May 2008
    Posts
    7
    Rep Power
    0

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

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

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

  15. #15
    Tresurimase is offline Member
    Join Date
    Oct 2008
    Location
    Latvia
    Posts
    1
    Rep Power
    0

    Default Hi from Latvia

    Hi

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


    theshyzirty

Similar Threads

  1. HashMap and ComboBox
    By banie in forum AWT / Swing
    Replies: 2
    Last Post: 03-26-2008, 12:58 AM
  2. Need help with T/F and Multiple Choice
    By sayso36 in forum Advanced Java
    Replies: 0
    Last Post: 03-12-2008, 05:39 PM
  3. Using java.awt.Choice
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 01-02-2008, 07:33 PM
  4. ComboBox with database options
    By Goldy in forum Advanced Java
    Replies: 0
    Last Post: 12-01-2007, 10:43 PM
  5. i need assistance with a string triggered loop please!
    By Phobos0001 in forum New To Java
    Replies: 9
    Last Post: 11-14-2007, 03:44 PM

Posting Permissions

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