Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 18 of 18

Thread: JLabel/ComboBox Issues

  1. #1
    Member
    Join Date
    Oct 2012
    Posts
    35
    My Mood
    Fine
    Thanks
    5
    Thanked 1 Time in 1 Post

    Post JLabel/ComboBox Issues

    Greetings! I am having some trouble with JLabel.

    This is a Sign In program for my high school's athletic trainer that I am writing. When it first runs, it should display an empty text field titled "Enter Name". Below it, will be a "Select Your Sport: " JComboBox that has an array of sports in it. Below that, will be a "Select Body Part: " JComboBox that has an array of general body parts. When a body part is selected, another JComboBox appears below it that is respective to the selected option in the bodyPart array. If the "General Medical" option is selected, then another JComboBox appears below it with more options. Below all of this is a JButton that when pressed checks to see if everything is filled out/selected, then saves the information into a .txt file at a specified file path.

    What is working:
    • The Formatting of font/color/background
    • Arrays with Names of Treatments/ Potential Selections
    • New JComboBoxes appearing when selected

    What is not working:
    • If Selection 1 was selected, then Selection 2, Selection 1 does not dissapear
    • The arrays, sometimes, get switched with other ones
    • The printing to a .txt file and checking to see if everything was filled out/ selected
    • Formatting the layout of the program (All to the left, and one JComboBox per line
    • The appearing of JComboBoxes off of the General Treatment selection
    • The placement of a textbox at the top

    Here is my code (Compiler Error Free):
    import java.awt.*;
    import javax.swing.*;
    import java.util.*;
    import java.awt.event.*;
     
    public class SignIn extends JFrame implements ActionListener
    {
     
    	FlowLayout flow = new FlowLayout(); 
     
     
    	JLabel label = new JLabel("Select Your Sport:");
    	JLabel label_1 = new JLabel("Body Part:");
    		JLabel label_2a = new JLabel("Upper Extremity");
    		JLabel label_2b = new JLabel("Lower Extremity");
    		JLabel label_2c = new JLabel("Head");
    		JLabel label_2d = new JLabel("Torso"); 
    		JLabel label_2e = new JLabel("General Medical");
    			JLabel label_3a = new JLabel("Side");
    			JLabel label_3b = new JLabel("Injury");
    			JLabel label_3c_a = new JLabel("Treatment");
    			JLabel label_3c_b = new JLabel("Treatment (Optional)");
     
    	final int FRAME_WIDTH = 600; 
    	final int FRAME_HEIGHT = 400; 
    	String[] sports = { "---", "Football", "Boy's Soccer", "Girl's Soccer", "Field Hockey", "Cross Country", "Tennis", "Boy's Basketball", "Girl's Basketball", "Wrestling", "Cheer", "Bowling", "Baseball", "Softball", "Boy's Lacrosse", "Girl's Lacrosse", "Track & Field", "Other"}; 
    	String[] bodyPart = { "---", "Upper Extremity", "Lower Extremity", "Head", "Torso", "General Medical"};
    		String[] upperExtremity = { "---", "Elbow", "Finger", "Forearm", "Hand", "Shoulder", "Thumb", "Upper Arm", "Wrist", "Other"};
    		String[] lowerExtremity = { "---", "Ankle", "Calf", "Foot", "Groin", "Heel", "Hip", "Knee", "Lower Leg", "Shin", "Thigh", "Toes", "Other"};
    		String[] head = { "---", "Brain", "Ear", "Eye", "Face", "Forehead", "Jaw", "Nose", "Scalp", "Skull", "Other"};	
    		String[] torso = { "---", "Abdomen", "Back", "Cervical Spine", "Chest", "Clavicle", "Flank", "Lumber Spine", "Neck", "Rib", "Sacrum (Tailbone)", "Sternum", "Thoracic Spine", "Other"};	
    		String[] generalMedical = { "---", "Side", "Injury", "Treatment", "Other"};
    			String[] side = { "---", "Right", "Left", "Midline", "Front", "Back", "Other"};
    			String[] injury = { "---", "Amputation", "Asphyxiation", "Bite", "Blister", "Burn", "Concussion", "Contusion/ Bruise", "Cut/ Laceration/ Abrasion", "Dislocation", "Fracture", "Hearing Loss", "Heat Illness", "Hypothermia", "Shock", "Shock", "Sore", "Sprain", "Strain", "Vision Loss", "Other" };
    			String[] treatment = { "---", "Bandage", "Brace/ Splint/ Cast", "Combo", "Contrast Bath", "Electrotherapy", "Hi-Volt", "Ice Bag/ Pack", "Ice Massage", "Inferential", "Massage", "Moist Heat Pack", "Muscle Stimulator", "Padding", "Paraffin", "Rehab Excercises", "Russian", "Stretch", "Tape", "TENS", "Ultrasound", "Whirlpool", "Wound Care", "Other"};
    	JComboBox size = new JComboBox(sports);
    	JComboBox size_1 = new JComboBox(bodyPart);
    		JComboBox size_2a = new JComboBox(upperExtremity);
    		JComboBox size_2b = new JComboBox(lowerExtremity);
    		JComboBox size_2c = new JComboBox(head);
    		JComboBox size_2d = new JComboBox(torso);
    		JComboBox size_2e = new JComboBox(generalMedical);
    			JComboBox size_3a = new JComboBox(side);
    			JComboBox size_3b = new JComboBox(injury);
    			JComboBox size_3c_a = new JComboBox(treatment);
    			JComboBox size_3c_b = new JComboBox(treatment);
    	public SignIn() 
    	{ 
    		super("Athletic Trainer Sign In");	
    		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
    		setLayout (new FlowLayout()); 
    		getContentPane().setBackground(new Color(178, 178, 56)); 
     
    		//Sub Level 1 - Sport
    		label.setFont(new Font("Arial", Font.BOLD, 18)); 
    		label.setForeground(Color.white); 
     
    		size.setFont(new Font("Arial", Font.BOLD, 22)); 
    		size.setForeground(Color.white); 
    		size.setBackground(Color.blue);
     
     		add(label); 
    		add(size); 
    		setSize(FRAME_WIDTH, FRAME_HEIGHT); 
     
    		//Sub Level 1 - Body Part
    		label_1.setFont(new Font("Arial", Font.BOLD, 18)); 
    		label_1.setForeground(Color.white); 
     
    		size_1.setFont(new Font("Arial", Font.BOLD, 22)); 
    		size_1.setForeground(Color.white); 
    		size_1.setBackground(Color.blue);  
     
    		add(label_1); 
    		add(size_1); 
    		setSize(FRAME_WIDTH, FRAME_HEIGHT);
    		size_1.addActionListener(this); 
     
    //****************************************************
     
    				//Sub Level 3a - Side
    				label_3a.setFont(new Font("Arial", Font.BOLD, 18)); 
    				label_3a.setForeground(Color.white); 
     
    				size_3a.setFont(new Font("Arial", Font.BOLD, 22)); 
    				size_3a.setForeground(Color.white); 
    				size_3a.setBackground(Color.blue);  
     
    				add(label_3a); 
    				add(size_3a); 
    				setSize(FRAME_WIDTH, FRAME_HEIGHT);
     
    				//Sub Level 3b - Injury
    				label_3b.setFont(new Font("Arial", Font.BOLD, 18)); 
    				label_3b.setForeground(Color.white); 
     
    				size_3b.setFont(new Font("Arial", Font.BOLD, 22)); 
    				size_3b.setForeground(Color.white); 
    				size_3b.setBackground(Color.blue);  
     
    				add(label_3b); 
    				add(size_3b); 
    				setSize(FRAME_WIDTH, FRAME_HEIGHT);
     
    				//Sub Level 3c-a - Treatment
    				label_3c_a.setFont(new Font("Arial", Font.BOLD, 18)); 
    				label_3c_a.setForeground(Color.white); 
     
    				size_3c_a.setFont(new Font("Arial", Font.BOLD, 22)); 
    				size_3c_a.setForeground(Color.white); 
    				size_3c_a.setBackground(Color.blue);  
     
    				add(label_3c_a); 
    				add(size_3c_a); 
    				setSize(FRAME_WIDTH, FRAME_HEIGHT);
     
    				//Sub Level 3c-b - Treatment
    				label_3c_b.setFont(new Font("Arial", Font.BOLD, 18)); 
    				label_3c_b.setForeground(Color.white); 
     
    				size_3c_b.setFont(new Font("Arial", Font.BOLD, 22)); 
    				size_3c_b.setForeground(Color.white); 
    				size_3c_b.setBackground(Color.blue);  
     
    				add(label_3c_b); 
    				add(size_3c_b); 
    				setSize(FRAME_WIDTH, FRAME_HEIGHT);
    	}	
     
    //***********************************************************
     
    	public static void main(String[] args) 
    		{ 
    			SignIn frame = new SignIn(); 
    			frame.setVisible(true); 
      	}
     
      	@Override
        public void actionPerformed(ActionEvent e) {
        	String selectedValue = size_1.getSelectedItem().toString();
            String[] combo2Item = null;
     
            DefaultComboBoxModel model = (DefaultComboBoxModel) size_2a.getModel();      
            model.removeAllElements();
     
            while(selectedValue.equals("Upper Extremity")){
     
            	combo2Item = new String[]{ "---", "Elbow", "Finger", "Forearm", "Hand", "Shoulder", "Thumb", "Upper Arm", "Wrist", "Other"};;
     
                //Sub Level 2a - Upper Extremity
    			label_2a.setFont(new Font("Arial", Font.BOLD, 18)); 
    			label_2a.setForeground(Color.white); 
     
    			size_2a.setFont(new Font("Arial", Font.BOLD, 22)); 
    			size_2a.setForeground(Color.white); 
    			size_2a.setBackground(Color.blue);  
     
    			add(label_2a); 
    			add(size_2a); 
    			setSize(FRAME_WIDTH, FRAME_HEIGHT);
    			break;
     
            } while(selectedValue.equals("Lower Extremity")){
     
                combo2Item = new String[]{ "---", "Ankle", "Calf", "Foot", "Groin", "Heel", "Hip", "Knee", "Lower Leg", "Shin", "Thigh", "Toes", "Other"};
     
                //Sub Level 2b - Lower Extremity
    			label_2b.setFont(new Font("Arial", Font.BOLD, 18)); 
    			label_2b.setForeground(Color.white); 
     
    			size_2b.setFont(new Font("Arial", Font.BOLD, 22)); 
    			size_2b.setForeground(Color.white); 
    			size_2b.setBackground(Color.blue);  
     
    			add(label_2b); 
    			add(size_2b); 
    			setSize(FRAME_WIDTH, FRAME_HEIGHT);
    			break;
     
            } while(selectedValue.equals("Head")){
                combo2Item = new String[]{ "---", "Brain", "Ear", "Eye", "Face", "Forehead", "Jaw", "Nose", "Scalp", "Skull", "Other"};
     
                //Sub Level 2c - Head
    			label_2c.setFont(new Font("Arial", Font.BOLD, 18)); 
    			label_2c.setForeground(Color.white); 
     
    			size_2c.setFont(new Font("Arial", Font.BOLD, 22)); 
    			size_2c.setForeground(Color.white); 
    			size_2c.setBackground(Color.blue);  
     
    			add(label_2c); 
    			add(size_2c); 
    			setSize(FRAME_WIDTH, FRAME_HEIGHT);
    			break;
     
            } 	while(selectedValue.equals("Torso")){
                combo2Item = new String[]{ "---", "Abdomen", "Back", "Cervical Spine", "Chest", "Clavicle", "Flank", "Lumber Spine", "Neck", "Rib", "Sacrum (Tailbone)", "Sternum", "Thoracic Spine", "Other"};
     
                //Sub Level 2d - Torso  
    			label_2d.setFont(new Font("Arial", Font.BOLD, 18)); 
    			label_2d.setForeground(Color.white); 
     
    			size_2d.setFont(new Font("Arial", Font.BOLD, 22)); 
    			size_2d.setForeground(Color.white); 
    			size_2d.setBackground(Color.blue);  
     
    			add(label_2d); 
    			add(size_2d); 
    			setSize(FRAME_WIDTH, FRAME_HEIGHT);
    			break;
     
     
     
            } while(selectedValue.equals("General Medical")){
                combo2Item = new String[]{ "---", "Side", "Injury", "Treatment", "Other"};;
     
                //Sub Level 2e - General Medical 
    			label_2e.setFont(new Font("Arial", Font.BOLD, 18)); 
    			label_2e.setForeground(Color.white); 
     
    			size_2e.setFont(new Font("Arial", Font.BOLD, 22)); 
    			size_2e.setForeground(Color.white); 
    			size_2e.setBackground(Color.blue);  
     
    			add(label_2e); 
    			add(size_2e); 
    			setSize(FRAME_WIDTH, FRAME_HEIGHT);
    			break;
            }
            for(String val : combo2Item){
                model.addElement(val);
        	}	
    	}
    }
    Here is what it looks like when it is running:
    SignIn V.1.jpg
    Please do ask if anything needs to be explained in further detail. Any help will be much appreciated! Thanks for taking the time to read through all of this


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,130
    Thanks
    48
    Thanked 2,156 Times in 2,129 Posts

    Default Re: JLabel/ComboBox Issues

    Starting with the first item in the list:
    If Selection 1 was selected, then Selection 2, Selection 1 does not dissapear
    Can you explain what you do and what the program does etc and what the problem is?
    Please be specific and detailed in your explanation.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Member
    Join Date
    Oct 2012
    Posts
    35
    My Mood
    Fine
    Thanks
    5
    Thanked 1 Time in 1 Post

    Default Re: JLabel/ComboBox Issues

    Okay, when the program runs, there is a JComboBox that contains the bodyPart array. What it is supposed to do is when one of the items in the JComboBox are selected, it makes another JComboBox appear that contains a different array. For example, if "Upper Extremity" is selected in the first JComboBox, then another JComboBox appears below it containing the array, upperExtremity, with items such as elbow, finger forearm, etc. If the user goes back and changes the original selection from "Upper Extremity" to "Lower Extremity", then the upperExtremity array should disappear and be replaced with the lowerExtremity array. This is what I was referring too when I said
    If Selection 1 was selected, then Selection 2, Selection 1 does not dissapear
    This is handled by this block of code:

     
    @Override
        public void actionPerformed(ActionEvent e) {
        	String selectedValue = size_1.getSelectedItem().toString();
            String[] combo2Item = null;
     
            DefaultComboBoxModel model = (DefaultComboBoxModel) size_2a.getModel();      
            model.removeAllElements();
     
            while(selectedValue.equals("Upper Extremity")){
     
            	combo2Item = new String[]{ "---", "Elbow", "Finger", "Forearm", "Hand", "Shoulder", "Thumb", "Upper Arm", "Wrist", "Other"};
     
                //Sub Level 2a - Upper Extremity
    			label_2a.setFont(new Font("Arial", Font.BOLD, 18)); 
    			label_2a.setForeground(Color.white); 
     
    			size_2a.setFont(new Font("Arial", Font.BOLD, 22)); 
    			size_2a.setForeground(Color.white); 
    			size_2a.setBackground(Color.blue);  
     
    			add(label_2a); 
    			add(size_2a); 
    			setSize(FRAME_WIDTH, FRAME_HEIGHT);
    			break;
     
            } while(selectedValue.equals("Lower Extremity")){
     
                combo2Item = new String[]{ "---", "Ankle", "Calf", "Foot", "Groin", "Heel", "Hip", "Knee", "Lower Leg", "Shin", "Thigh", "Toes", "Other"};
     
                //Sub Level 2b - Lower Extremity
    			label_2b.setFont(new Font("Arial", Font.BOLD, 18)); 
    			label_2b.setForeground(Color.white); 
     
    			size_2b.setFont(new Font("Arial", Font.BOLD, 22)); 
    			size_2b.setForeground(Color.white); 
    			size_2b.setBackground(Color.blue);  
     
    			add(label_2b); 
    			add(size_2b); 
    			setSize(FRAME_WIDTH, FRAME_HEIGHT);
    			break;
     
            } while(selectedValue.equals("Head")){
                combo2Item = new String[]{ "---", "Brain", "Ear", "Eye", "Face", "Forehead", "Jaw", "Nose", "Scalp", "Skull", "Other"};
     
                //Sub Level 2c - Head
    			label_2c.setFont(new Font("Arial", Font.BOLD, 18)); 
    			label_2c.setForeground(Color.white); 
     
    			size_2c.setFont(new Font("Arial", Font.BOLD, 22)); 
    			size_2c.setForeground(Color.white); 
    			size_2c.setBackground(Color.blue);  
     
    			add(label_2c); 
    			add(size_2c); 
    			setSize(FRAME_WIDTH, FRAME_HEIGHT);
    			break;
     
            } 	while(selectedValue.equals("Torso")){
                combo2Item = new String[]{ "---", "Abdomen", "Back", "Cervical Spine", "Chest", "Clavicle", "Flank", "Lumber Spine", "Neck", "Rib", "Sacrum (Tailbone)", "Sternum", "Thoracic Spine", "Other"};
     
                //Sub Level 2d - Torso  
    			label_2d.setFont(new Font("Arial", Font.BOLD, 18)); 
    			label_2d.setForeground(Color.white); 
     
    			size_2d.setFont(new Font("Arial", Font.BOLD, 22)); 
    			size_2d.setForeground(Color.white); 
    			size_2d.setBackground(Color.blue);  
     
    			add(label_2d); 
    			add(size_2d); 
    			setSize(FRAME_WIDTH, FRAME_HEIGHT);
    			break;
     
     
     
            } while(selectedValue.equals("General Medical")){
                combo2Item = new String[]{ "---", "Side", "Injury", "Treatment", "Other"};
     
                //Sub Level 2e - General Medical 
    			label_2e.setFont(new Font("Arial", Font.BOLD, 18)); 
    			label_2e.setForeground(Color.white); 
     
    			size_2e.setFont(new Font("Arial", Font.BOLD, 22)); 
    			size_2e.setForeground(Color.white); 
    			size_2e.setBackground(Color.blue);  
     
    			add(label_2e); 
    			add(size_2e); 
    			setSize(FRAME_WIDTH, FRAME_HEIGHT);
    			break;
            }
            for(String val : combo2Item){
                model.addElement(val);
        	}	
    	}

  4. #4
    Super Moderator curmudgeon's Avatar
    Join Date
    Aug 2012
    Posts
    1,130
    My Mood
    Cynical
    Thanks
    64
    Thanked 139 Times in 134 Posts

    Default Re: JLabel/ComboBox Issues

    A while loop in this location is a dangerous thing and unnecessary. Why are you choosing to use this construct? Wouldn't if and if-else blocks be more appropriate for this?

  5. #5
    Member
    Join Date
    Oct 2012
    Posts
    35
    My Mood
    Fine
    Thanks
    5
    Thanked 1 Time in 1 Post

    Default Re: JLabel/ComboBox Issues

    I my thinking was that the initialization of the JComboBoxes are based off of the value of the selectedValue, thus while it is selected, then it is initialized, and when it is not selected, it disappears. Obviously, my initial thoughts were wrong in this sense. I presume that I need a block of code that checks to see if it is no longer the value of the selectedValue, and if it is not, then to have it disappear.

  6. #6
    Super Moderator curmudgeon's Avatar
    Join Date
    Aug 2012
    Posts
    1,130
    My Mood
    Cynical
    Thanks
    64
    Thanked 139 Times in 134 Posts

    Default Re: JLabel/ComboBox Issues

    Also, when you see a lot of redundant code, you will want to get rid of it and simplify your code by creating methods. For instance you could create a method say called void setUpComboBox(JComboBox combobox, int fontSize) that does all your prettification of the JComboBoxes for you. As for your code layout, Google the Java layout manager tutorial and check the layouts that are available. BoxLayout immediately comes to mind if you want to stack components, one on top of the other.

    --- Update ---

    Quote Originally Posted by Blasfemmy View Post
    I my thinking was that the initialization of the JComboBoxes are based off of the value of the selectedValue, thus while it is selected, then it is initialized, and when it is not selected, it disappears. Obviously, my initial thoughts were wrong in this sense. I presume that I need a block of code that checks to see if it is no longer the value of the selectedValue, and if it is not, then to have it disappear.
    Myself, I would show all the JComboBoxes needed from the get-go, but would swap out the combo box's models when necessary so that they only hold the data that I want displayed. This can be done by creating DefaultComboBoxModel objects and then calling setModel(...) on your combo boxes when indicated.

  7. #7
    Member
    Join Date
    Oct 2012
    Posts
    35
    My Mood
    Fine
    Thanks
    5
    Thanked 1 Time in 1 Post

    Default Re: JLabel/ComboBox Issues

    Yeah, I was planning on cleaning up the code a bit and worrying about the layout later, once I got these few program-breaking errors fixed.

    Myself, I would show all the JComboBoxes needed from the get-go, but would swap out the combo box's models when necessary so that they only hold the data that I want displayed. This can be done by creating DefaultComboBoxModel objects and then calling setModel(...) on your combo boxes when indicated.
    Hmm, would I be able to assign something like
     frame.setVisible(true);
    for each one of the JComboBoxes and have all of them turned off, and then just have a boolean variable that toggles it when it is the selectedValue?

  8. #8
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,130
    Thanks
    48
    Thanked 2,156 Times in 2,129 Posts

    Default Re: JLabel/ComboBox Issues

    A quick fix for the layout could be: GridLayout(7,2)
    Later you'll want to fix it up.

    There is a lot of redundant code:
    setSize(FRAME_WIDTH, FRAME_HEIGHT); //<<<<<<< why so many?
    combo2Item = new String[]{ "- <<< the comboboxes already contain the Strings, why change?
    If you don't understand my answer, don't ignore it, ask a question.

  9. #9
    Member
    Join Date
    Oct 2012
    Posts
    35
    My Mood
    Fine
    Thanks
    5
    Thanked 1 Time in 1 Post

    Default Re: JLabel/ComboBox Issues

    Okay, so I cleaned up the code a bit, and was able to make the JComboBox disappear when not selected.

    setSize(FRAME_WIDTH, FRAME_HEIGHT); //<<<<<<< why so many?
    combo2Item = new String[]{ "- <<< the comboboxes already contain the Strings, why change?
    I figured that it couldn't hurt if I added it to prevent a compiler error, because at that point, I didn't know if it needed it or not. I removed the first item mentioned, and for the second, instead of making a new array, it simply references the global array declared at the beginning and works perfectly.

    A quick fix for the layout could be: GridLayout(7,2)
    Later you'll want to fix it up.
    I tried to add it in, and looked at this here: How to Use GridLayout (The Java™ Tutorials > Creating a GUI With JFC/Swing > Laying Out Components Within a Container) to no avail. I will bold this in the new version of the code posted below. Edit: For some reason the bold isn't working so I added an arrow pointing at it.

     
    import java.awt.*;
    import javax.swing.*;
    import java.util.*;
    import java.awt.event.*;
     
    public class SignIn extends JFrame implements ActionListener
    {
     
    	FlowLayout flow = new FlowLayout(); 
     
     
    	JLabel label = new JLabel("Select Your Sport:");
    	JLabel label_1 = new JLabel("Body Part:");
    		JLabel label_2a = new JLabel("Upper Extremity");
    		JLabel label_2b = new JLabel("Lower Extremity");
    		JLabel label_2c = new JLabel("Head");
    		JLabel label_2d = new JLabel("Torso"); 
    		JLabel label_2e = new JLabel("General Medical");
    			JLabel label_3a = new JLabel("Side");
    			JLabel label_3b = new JLabel("Injury");
    			JLabel label_3c_a = new JLabel("Treatment");
    			JLabel label_3c_b = new JLabel("Treatment (Optional)");
     
    	final int FRAME_WIDTH = 600; 
    	final int FRAME_HEIGHT = 400; 
    	String[] sports = { "---", "Football", "Boy's Soccer", "Girl's Soccer", "Field Hockey", "Cross Country", "Tennis", "Boy's Basketball", "Girl's Basketball", "Wrestling", "Cheer", "Bowling", "Baseball", "Softball", "Boy's Lacrosse", "Girl's Lacrosse", "Track & Field", "Other"}; 
    	String[] bodyPart = { "---", "Upper Extremity", "Lower Extremity", "Head", "Torso", "General Medical"};
    		String[] upperExtremity = { "---", "Elbow", "Finger", "Forearm", "Hand", "Shoulder", "Thumb", "Upper Arm", "Wrist", "Other"};
    		String[] lowerExtremity = { "---", "Ankle", "Calf", "Foot", "Groin", "Heel", "Hip", "Knee", "Lower Leg", "Shin", "Thigh", "Toes", "Other"};
    		String[] head = { "---", "Brain", "Ear", "Eye", "Face", "Forehead", "Jaw", "Nose", "Scalp", "Skull", "Other"};	
    		String[] torso = { "---", "Abdomen", "Back", "Cervical Spine", "Chest", "Clavicle", "Flank", "Lumber Spine", "Neck", "Rib", "Sacrum (Tailbone)", "Sternum", "Thoracic Spine", "Other"};	
    		String[] generalMedical = { "---", "Side", "Injury", "Treatment", "Other"};
    			String[] side = { "---", "Right", "Left", "Midline", "Front", "Back", "Other"};
    			String[] injury = { "---", "Amputation", "Asphyxiation", "Bite", "Blister", "Burn", "Concussion", "Contusion/ Bruise", "Cut/ Laceration/ Abrasion", "Dislocation", "Fracture", "Hearing Loss", "Heat Illness", "Hypothermia", "Shock", "Shock", "Sore", "Sprain", "Strain", "Vision Loss", "Other" };
    			String[] treatment = { "---", "Bandage", "Brace/ Splint/ Cast", "Combo", "Contrast Bath", "Electrotherapy", "Hi-Volt", "Ice Bag/ Pack", "Ice Massage", "Inferential", "Massage", "Moist Heat Pack", "Muscle Stimulator", "Padding", "Paraffin", "Rehab Excercises", "Russian", "Stretch", "Tape", "TENS", "Ultrasound", "Whirlpool", "Wound Care", "Other"};
    	JComboBox comboBox = new JComboBox(sports);
    	JComboBox comboBox_1 = new JComboBox(bodyPart);
    		JComboBox comboBox_2a = new JComboBox(upperExtremity);
    		JComboBox comboBox_2b = new JComboBox(lowerExtremity);
    		JComboBox comboBox_2c = new JComboBox(head);
    		JComboBox comboBox_2d = new JComboBox(torso);
    		JComboBox comboBox_2e = new JComboBox(generalMedical);
    			JComboBox comboBox_3a = new JComboBox(side);
    			JComboBox comboBox_3b = new JComboBox(injury);
    			JComboBox comboBox_3c_a = new JComboBox(treatment);
    			JComboBox comboBox_3c_b = new JComboBox(treatment);
    	public SignIn() 
    	{ 
    		super("Athletic Trainer Sign In");	
    		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
    		setLayout (new FlowLayout()); 
    		getContentPane().setBackground(new Color(178, 178, 56));
     
    		GridLayout formatLayout = new GridLayout(7,2); //<---------------HERE
     
     
    		//Sub Level 1 - Sport
    		label.setFont(new Font("Arial", Font.BOLD, 18)); 
    		label.setForeground(Color.white); 
     
    		comboBox.setFont(new Font("Arial", Font.BOLD, 22)); 
    		comboBox.setForeground(Color.white); 
    		comboBox.setBackground(Color.blue);
     
     		add(label); 
    		add(comboBox); 
    		setSize(FRAME_WIDTH, FRAME_HEIGHT); 
     
    		//Sub Level 1 - Body Part
    		label_1.setFont(new Font("Arial", Font.BOLD, 18)); 
    		label_1.setForeground(Color.white); 
     
    		comboBox_1.setFont(new Font("Arial", Font.BOLD, 22)); 
    		comboBox_1.setForeground(Color.white); 
    		comboBox_1.setBackground(Color.blue);  
     
    		add(label_1); 
    		add(comboBox_1); 
     
    		comboBox_1.addActionListener(this); 
     
    //****************************************************
     
    				//Sub Level 3a - Side
    				label_3a.setFont(new Font("Arial", Font.BOLD, 18)); 
    				label_3a.setForeground(Color.white); 
     
    				comboBox_3a.setFont(new Font("Arial", Font.BOLD, 22)); 
    				comboBox_3a.setForeground(Color.white); 
    				comboBox_3a.setBackground(Color.blue);  
     
    				add(label_3a); 
    				add(comboBox_3a); 
     
     
    				//Sub Level 3b - Injury
    				label_3b.setFont(new Font("Arial", Font.BOLD, 18)); 
    				label_3b.setForeground(Color.white); 
     
    				comboBox_3b.setFont(new Font("Arial", Font.BOLD, 22)); 
    				comboBox_3b.setForeground(Color.white); 
    				comboBox_3b.setBackground(Color.blue);  
     
    				add(label_3b); 
    				add(comboBox_3b); 
     
     
    				//Sub Level 3c-a - Treatment
    				label_3c_a.setFont(new Font("Arial", Font.BOLD, 18)); 
    				label_3c_a.setForeground(Color.white); 
     
    				comboBox_3c_a.setFont(new Font("Arial", Font.BOLD, 22)); 
    				comboBox_3c_a.setForeground(Color.white); 
    				comboBox_3c_a.setBackground(Color.blue);  
     
    				add(label_3c_a); 
    				add(comboBox_3c_a); 
     
     
    				//Sub Level 3c-b - Treatment
    				label_3c_b.setFont(new Font("Arial", Font.BOLD, 18)); 
    				label_3c_b.setForeground(Color.white); 
     
    				comboBox_3c_b.setFont(new Font("Arial", Font.BOLD, 22)); 
    				comboBox_3c_b.setForeground(Color.white); 
    				comboBox_3c_b.setBackground(Color.blue);  
     
    				add(label_3c_b); 
    				add(comboBox_3c_b); 
     
    	}	
     
    //***********************************************************
     
    	public static void main(String[] args) 
    		{ 
    			SignIn frame = new SignIn(); 
    			frame.setVisible(true); 
      	}
     
      	@Override
        public void actionPerformed(ActionEvent e) {
        	String selectedValue = comboBox_1.getSelectedItem().toString();
            String[] combo2Item = null;
     
            DefaultComboBoxModel model = (DefaultComboBoxModel) comboBox_2a.getModel();      
            model.removeAllElements();
     
            if(selectedValue.equals("Upper Extremity")){
     
            	label_2a.setVisible(true);
            	comboBox_2a.setVisible(true);
     
            	combo2Item = upperExtremity;
     
                //Sub Level 2a - Upper Extremity
    			label_2a.setFont(new Font("Arial", Font.BOLD, 18)); 
    			label_2a.setForeground(Color.white); 
     
    			comboBox_2a.setFont(new Font("Arial", Font.BOLD, 22)); 
    			comboBox_2a.setForeground(Color.white); 
    			comboBox_2a.setBackground(Color.blue);  
     
    			add(label_2a); 
    			add(comboBox_2a); 
     
     
            } else { 
            	label_2a.setVisible(false);
            	comboBox_2a.setVisible(false);
            	}
            	if(selectedValue.equals("Lower Extremity")){
     
            		label_2b.setVisible(true);
            		comboBox_2b.setVisible(true);
     
              	 	combo2Item = lowerExtremity;
     
             	   //Sub Level 2b - Lower Extremity
    				label_2b.setFont(new Font("Arial", Font.BOLD, 18)); 
    				label_2b.setForeground(Color.white); 
     
    				comboBox_2b.setFont(new Font("Arial", Font.BOLD, 22)); 
    				comboBox_2b.setForeground(Color.white); 
    				comboBox_2b.setBackground(Color.blue);  
     
    				add(label_2b); 
    				add(comboBox_2b); 
     
     
            } else { 
            	label_2b.setVisible(false);
            	comboBox_2b.setVisible(false);
            	}
            	 if(selectedValue.equals("Head")){
     
            	label_2c.setVisible(true);
            	comboBox_2c.setVisible(true);
     
                	combo2Item = head;
     
                	//Sub Level 2c - Head
    				label_2c.setFont(new Font("Arial", Font.BOLD, 18)); 
    				label_2c.setForeground(Color.white); 
     
    				comboBox_2c.setFont(new Font("Arial", Font.BOLD, 22)); 
    				comboBox_2c.setForeground(Color.white); 
    				comboBox_2c.setBackground(Color.blue);  
     
    				add(label_2c); 
    				add(comboBox_2c); 
     
     
            } 
            	else { 
            		label_2c.setVisible(false);
            		comboBox_2c.setVisible(false);
            	}
     
            	if(selectedValue.equals("Torso")){
     
            		label_2d.setVisible(true);
            		comboBox_2d.setVisible(true);
     
              	 	 combo2Item = torso;
     
              	  //Sub Level 2d - Torso  
    				label_2d.setFont(new Font("Arial", Font.BOLD, 18)); 
    				label_2d.setForeground(Color.white); 
     
    				comboBox_2d.setFont(new Font("Arial", Font.BOLD, 22)); 
    				comboBox_2d.setForeground(Color.white); 
    				comboBox_2d.setBackground(Color.blue);  
     
    				add(label_2d); 
    				add(comboBox_2d); 
     
            }
            else { 
            	label_2d.setVisible(false);
            	comboBox_2d.setVisible(false);
            	}
             if(selectedValue.equals("General Medical")){
     
             	label_2e.setVisible(true);
            	comboBox_2e.setVisible(true);
     
                combo2Item = generalMedical;
     
                //Sub Level 2e - General Medical 
    			label_2e.setFont(new Font("Arial", Font.BOLD, 18)); 
    			label_2e.setForeground(Color.white); 
     
    			comboBox_2e.setFont(new Font("Arial", Font.BOLD, 22)); 
    			comboBox_2e.setForeground(Color.white); 
    			comboBox_2e.setBackground(Color.blue);  
     
    			add(label_2e); 
    			add(comboBox_2e); 
     
            }
            else { 
            	label_2e.setVisible(false);
            	comboBox_2e.setVisible(false);
            	}
            for(String val : combo2Item){
                model.addElement(val);
        	}	
    	}
    }

  10. #10
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,130
    Thanks
    48
    Thanked 2,156 Times in 2,129 Posts

    Default Re: JLabel/ComboBox Issues

    What is this statement used for?
    		GridLayout formatLayout = new GridLayout(7,2); //<---------------HERE
    Here is where the layout is set:
    setLayout (new FlowLayout());
    If you don't understand my answer, don't ignore it, ask a question.

  11. #11
    Member
    Join Date
    Oct 2012
    Posts
    35
    My Mood
    Fine
    Thanks
    5
    Thanked 1 Time in 1 Post

    Default Re: JLabel/ComboBox Issues

    Ahh, I see, it is working nicely!

    Here is that updated snippet:
    super("Athletic Trainer Sign In");	
    		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
    		setLayout (new GridLayout(8,2)); 
    		getContentPane().setBackground(new Color(178, 178, 56));
    The issue with this now, is that if a user were to select each of the items in the bodyParts array, then it keeps making more cells. I presume that this is that each individual item in the array is given a unique location, so instead of allowing another item to overwrite the location, it is forming a new cell and adding the data there. My solution to this would be to assign each comboBox and label a specific cell, so that if two are called to the same location, the new one will overwrite it. Is this a logical, or even possible way of going about solving this?

  12. #12
    Super Moderator curmudgeon's Avatar
    Join Date
    Aug 2012
    Posts
    1,130
    My Mood
    Cynical
    Thanks
    64
    Thanked 139 Times in 134 Posts

    Default Re: JLabel/ComboBox Issues

    You could create an array of JPanel and place each JLabel / JComboBox pair in the JPanel, and add and remove combo boxes from the panel based on array index, or use CardLayout to swap the components.

  13. #13
    Member
    Join Date
    Oct 2012
    Posts
    35
    My Mood
    Fine
    Thanks
    5
    Thanked 1 Time in 1 Post

    Default Re: JLabel/ComboBox Issues

    I'm trying to add them to an array index, but I can't seem to figure it out. I am getting an incompatible data types error, thus I presume that I am declaring them incorrectly.

    I added the declaration for the JPanel array here:
     
    public class SignIn extends JFrame implements ActionListener
    {
    	JPanel[][] panel = new JPanel[9][1]; //<--------HERE
     
    	JButton submit = new JButton("Submit"); 
    	JLabel nameLabel = new JLabel("Enter Your First and Last Name:");
    	JLabel label = new JLabel("Select Your Sport:");
    I have my JLabels and JButton declared here as:
     
    JButton submit = new JButton("Submit"); 
    	JLabel nameLabel = new JLabel("Enter Your First and Last Name:");
    	JLabel label = new JLabel("Select Your Sport:");
    	JLabel label_1 = new JLabel("Body Part:");
    		JLabel label_2a = new JLabel("Upper Extremity:");
    		JLabel label_2b = new JLabel("Lower Extremity:");
    		JLabel label_2c = new JLabel("Head:");
    		JLabel label_2d = new JLabel("Torso:"); 
    		JLabel label_2e = new JLabel("General Medical:");
    			JLabel label_3a = new JLabel("Side:");
    			JLabel label_3b = new JLabel("Injury:");
    			JLabel label_3c_a = new JLabel("Treatment:");
    			JLabel label_3c_b = new JLabel("Treatment (Optional):");
    And my ComboBoxes declared here as:
     
    JTextPane enterName = new JTextPane();
    	JComboBox comboBox = new JComboBox(sports);
    	JComboBox comboBox_1 = new JComboBox(bodyPart);
    		JComboBox comboBox_2a = new JComboBox(upperExtremity);
    		JComboBox comboBox_2b = new JComboBox(lowerExtremity);
    		JComboBox comboBox_2c = new JComboBox(head);
    		JComboBox comboBox_2d = new JComboBox(torso);
    		JComboBox comboBox_2e = new JComboBox(generalMedical);
    			JComboBox comboBox_3a = new JComboBox(side);
    			JComboBox comboBox_3b = new JComboBox(injury);
    			JComboBox comboBox_3c_a = new JComboBox(treatment);
    			JComboBox comboBox_3c_b = new JComboBox(treatment);

    My array assignments are here as:
     
    	public SignIn() 
    	{ 
     
    		//JPanel Array Declarations
    			//Name
    			nameLabel = panel[0][0];
    			enterName = panel[0][1];
    			//Sport Selection
    			sports = panel[1][0];
    			comboBox = panel[1][1];
    			//Body Part Selection
    			bodyPart = panel[2][0];
    			comboBox_1 = panel[2][1];
    				//Upper Extremity
    				upperExtremity = panel[3][0];
    				comboBox_2a = panel[3][1];
    				//Lower Extremity
    				lowerExtremity = panel[3][0];
    				comboBox_2b = panel[3][1];
    				//Head
    				head = panel[3][0];
    				comboBox_2c = panel[3][1];
    				//Torso
    				torso = panel[3][0];
    				comboBox_2d = panel[3][1];
    				//General Medical
    				generalMedical = panel[3][0];
    				comboBox_2e = panel[3][1];
    					//Side
    					side = panel[4][0];
    					comboBox_3a = panel[4][1];
    					//Injury
    					side = panel[4][0];
    					comboBox_3b = panel[4][1];
    					//Treatment
    					treatment = panel[4][0];
    					comboBox_3c_a = panel[4][1];
    						//Treatment (Optional)
    						treatment = panel[5][0];
    						comboBox_3c_b = panel[5][1];

    And here is a copy of my complete code as of right now:
     
     
    import java.awt.*;
    import javax.swing.*;
    import java.util.*;
    import java.awt.event.*;
     
     
    public class SignIn extends JFrame implements ActionListener
    {
    	JPanel[][] panel = new JPanel[9][1];
     
    	JButton submit = new JButton("Submit"); 
    	JLabel nameLabel = new JLabel("Enter Your First and Last Name:");
    	JLabel label = new JLabel("Select Your Sport:");
    	JLabel label_1 = new JLabel("Body Part:");
    		JLabel label_2a = new JLabel("Upper Extremity:");
    		JLabel label_2b = new JLabel("Lower Extremity:");
    		JLabel label_2c = new JLabel("Head:");
    		JLabel label_2d = new JLabel("Torso:"); 
    		JLabel label_2e = new JLabel("General Medical:");
    			JLabel label_3a = new JLabel("Side:");
    			JLabel label_3b = new JLabel("Injury:");
    			JLabel label_3c_a = new JLabel("Treatment:");
    			JLabel label_3c_b = new JLabel("Treatment (Optional):");
     
    	final int FRAME_WIDTH = 600; 
    	final int FRAME_HEIGHT = 400; 
    	String[] sports = { "---", "Football", "Boy's Soccer", "Girl's Soccer", "Field Hockey", "Cross Country", "Tennis", "Boy's Basketball", "Girl's Basketball", "Wrestling", "Cheer", "Bowling", "Baseball", "Softball", "Boy's Lacrosse", "Girl's Lacrosse", "Track & Field", "Other"}; 
    	String[] bodyPart = { "---", "Upper Extremity", "Lower Extremity", "Head", "Torso", "General Medical"};
    		String[] upperExtremity = { "---", "Elbow", "Finger", "Forearm", "Hand", "Shoulder", "Thumb", "Upper Arm", "Wrist", "Other"};
    		String[] lowerExtremity = { "---", "Ankle", "Calf", "Foot", "Groin", "Heel", "Hip", "Knee", "Lower Leg", "Shin", "Thigh", "Toes", "Other"};
    		String[] head = { "---", "Brain", "Ear", "Eye", "Face", "Forehead", "Jaw", "Nose", "Scalp", "Skull", "Other"};	
    		String[] torso = { "---", "Abdomen", "Back", "Cervical Spine", "Chest", "Clavicle", "Flank", "Lumber Spine", "Neck", "Rib", "Sacrum (Tailbone)", "Sternum", "Thoracic Spine", "Other"};	
    		String[] generalMedical = { "---", "Side", "Injury", "Treatment", "Other"};
    			String[] side = { "---", "Right", "Left", "Midline", "Front", "Back", "Other"};
    			String[] injury = { "---", "Amputation", "Asphyxiation", "Bite", "Blister", "Burn", "Concussion", "Contusion/ Bruise", "Cut/ Laceration/ Abrasion", "Dislocation", "Fracture", "Hearing Loss", "Heat Illness", "Hypothermia", "Shock", "Shock", "Sore", "Sprain", "Strain", "Vision Loss", "Other" };
    			String[] treatment = { "---", "Bandage", "Brace/ Splint/ Cast", "Combo", "Contrast Bath", "Electrotherapy", "Hi-Volt", "Ice Bag/ Pack", "Ice Massage", "Inferential", "Massage", "Moist Heat Pack", "Muscle Stimulator", "Padding", "Paraffin", "Rehab Excercises", "Russian", "Stretch", "Tape", "TENS", "Ultrasound", "Whirlpool", "Wound Care", "Other"};
    	JTextPane enterName = new JTextPane();
    	JComboBox comboBox = new JComboBox(sports);
    	JComboBox comboBox_1 = new JComboBox(bodyPart);
    		JComboBox comboBox_2a = new JComboBox(upperExtremity);
    		JComboBox comboBox_2b = new JComboBox(lowerExtremity);
    		JComboBox comboBox_2c = new JComboBox(head);
    		JComboBox comboBox_2d = new JComboBox(torso);
    		JComboBox comboBox_2e = new JComboBox(generalMedical);
    			JComboBox comboBox_3a = new JComboBox(side);
    			JComboBox comboBox_3b = new JComboBox(injury);
    			JComboBox comboBox_3c_a = new JComboBox(treatment);
    			JComboBox comboBox_3c_b = new JComboBox(treatment);
    	public SignIn() 
    	{ 
     
    		//JPanel Array Declarations
    			//Name
    			nameLabel = panel[0][0];
    			enterName = panel[0][1];
    			//Sport Selection
    			sports = panel[1][0];
    			comboBox = panel[1][1];
    			//Body Part Selection
    			bodyPart = panel[2][0];
    			comboBox_1 = panel[2][1];
    				//Upper Extremity
    				upperExtremity = panel[3][0];
    				comboBox_2a = panel[3][1];
    				//Lower Extremity
    				lowerExtremity = panel[3][0];
    				comboBox_2b = panel[3][1];
    				//Head
    				head = panel[3][0];
    				comboBox_2c = panel[3][1];
    				//Torso
    				torso = panel[3][0];
    				comboBox_2d = panel[3][1];
    				//General Medical
    				generalMedical = panel[3][0];
    				comboBox_2e = panel[3][1];
    					//Side
    					side = panel[4][0];
    					comboBox_3a = panel[4][1];
    					//Injury
    					side = panel[4][0];
    					comboBox_3b = panel[4][1];
    					//Treatment
    					treatment = panel[4][0];
    					comboBox_3c_a = panel[4][1];
    						//Treatment (Optional)
    						treatment = panel[5][0];
    						comboBox_3c_b = panel[5][1];
     
     
    		super("Athletic Trainer Sign In");	
    		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
    		setLayout (new GridLayout(10,2)); 
    		getContentPane().setBackground(new Color(178, 178, 56));
    		//Sub Level 1 - Enter Name
    		add(nameLabel);
     		add(enterName);
     		nameLabel.setFont(new Font("Arial", Font.BOLD, 18)); 
    		nameLabel.setForeground(Color.white);
     		enterName.setFont(new Font("Arial", Font.BOLD, 22)); 
    		enterName.setForeground(Color.white); 
    		enterName.setBackground(Color.blue);
     
     
    		//Sub Level 1 - Sport
    		label.setFont(new Font("Arial", Font.BOLD, 18)); 
    		label.setForeground(Color.white); 
     
    		comboBox.setFont(new Font("Arial", Font.BOLD, 22)); 
    		comboBox.setForeground(Color.white); 
    		comboBox.setBackground(Color.blue);
     
     		add(label); 
    		add(comboBox); 
    		setSize(FRAME_WIDTH, FRAME_HEIGHT); 
     
    		//Sub Level 1 - Body Part
    		label_1.setFont(new Font("Arial", Font.BOLD, 18)); 
    		label_1.setForeground(Color.white); 
     
    		comboBox_1.setFont(new Font("Arial", Font.BOLD, 22)); 
    		comboBox_1.setForeground(Color.white); 
    		comboBox_1.setBackground(Color.blue);  
     
    		add(label_1); 
    		add(comboBox_1); 
     
    		comboBox_1.addActionListener(this); 
     
    //****************************************************
     
    				//Sub Level 3a - Side
    				label_3a.setFont(new Font("Arial", Font.BOLD, 18)); 
    				label_3a.setForeground(Color.white); 
     
    				comboBox_3a.setFont(new Font("Arial", Font.BOLD, 22)); 
    				comboBox_3a.setForeground(Color.white); 
    				comboBox_3a.setBackground(Color.blue);  
     
    				add(label_3a); 
    				add(comboBox_3a); 
     
     
    				//Sub Level 3b - Injury
    				label_3b.setFont(new Font("Arial", Font.BOLD, 18)); 
    				label_3b.setForeground(Color.white); 
     
    				comboBox_3b.setFont(new Font("Arial", Font.BOLD, 22)); 
    				comboBox_3b.setForeground(Color.white); 
    				comboBox_3b.setBackground(Color.blue);  
     
    				add(label_3b); 
    				add(comboBox_3b); 
     
     
    				//Sub Level 3c-a - Treatment
    				label_3c_a.setFont(new Font("Arial", Font.BOLD, 18)); 
    				label_3c_a.setForeground(Color.white); 
     
    				comboBox_3c_a.setFont(new Font("Arial", Font.BOLD, 22)); 
    				comboBox_3c_a.setForeground(Color.white); 
    				comboBox_3c_a.setBackground(Color.blue);  
     
    				add(label_3c_a); 
    				add(comboBox_3c_a); 
     
     
    				//Sub Level 3c-b - Treatment
    				label_3c_b.setFont(new Font("Arial", Font.BOLD, 18)); 
    				label_3c_b.setForeground(Color.white); 
     
    				comboBox_3c_b.setFont(new Font("Arial", Font.BOLD, 22)); 
    				comboBox_3c_b.setForeground(Color.white); 
    				comboBox_3c_b.setBackground(Color.blue);  
     
    				add(label_3c_b); 
    				add(comboBox_3c_b); 
     
    	}	
     
    //***********************************************************
     
    	public static void main(String[] args) 
    		{ 
    			SignIn frame = new SignIn(); 
    			frame.setVisible(true); 
      	}
     
      	@Override
        public void actionPerformed(ActionEvent e) {
        	String selectedValue = comboBox_1.getSelectedItem().toString();
            String[] combo2Item = null;
     
            DefaultComboBoxModel model = (DefaultComboBoxModel) comboBox_2a.getModel();      
            model.removeAllElements();
     
            if(selectedValue.equals("Upper Extremity")){
     
            	label_2a.setVisible(true);
            	comboBox_2a.setVisible(true);
     
            	combo2Item = upperExtremity;
     
                //Sub Level 2a - Upper Extremity
    			label_2a.setFont(new Font("Arial", Font.BOLD, 18)); 
    			label_2a.setForeground(Color.white); 
     
    			comboBox_2a.setFont(new Font("Arial", Font.BOLD, 22)); 
    			comboBox_2a.setForeground(Color.white); 
    			comboBox_2a.setBackground(Color.blue);  
     
    			add(label_2a); 
    			add(comboBox_2a); 
     
     
            } else { 
            	label_2a.setVisible(false);
            	comboBox_2a.setVisible(false);
            	}
            	if(selectedValue.equals("Lower Extremity")){
     
            		label_2b.setVisible(true);
            		comboBox_2b.setVisible(true);
     
              	 	combo2Item = lowerExtremity;
     
             	   //Sub Level 2b - Lower Extremity
    				label_2b.setFont(new Font("Arial", Font.BOLD, 18)); 
    				label_2b.setForeground(Color.white); 
     
    				comboBox_2b.setFont(new Font("Arial", Font.BOLD, 22)); 
    				comboBox_2b.setForeground(Color.white); 
    				comboBox_2b.setBackground(Color.blue);  
     
    				add(label_2b); 
    				add(comboBox_2b); 
     
     
            } else { 
            	label_2b.setVisible(false);
            	comboBox_2b.setVisible(false);
            	}
            	 if(selectedValue.equals("Head")){
     
            	label_2c.setVisible(true);
            	comboBox_2c.setVisible(true);
     
                	combo2Item = head;
     
                	//Sub Level 2c - Head
    				label_2c.setFont(new Font("Arial", Font.BOLD, 18)); 
    				label_2c.setForeground(Color.white); 
     
    				comboBox_2c.setFont(new Font("Arial", Font.BOLD, 22)); 
    				comboBox_2c.setForeground(Color.white); 
    				comboBox_2c.setBackground(Color.blue);  
     
    				add(label_2c); 
    				add(comboBox_2c); 
     
     
            } 
            	else { 
            		label_2c.setVisible(false);
            		comboBox_2c.setVisible(false);
            	}
     
            	if(selectedValue.equals("Torso")){
     
            		label_2d.setVisible(true);
            		comboBox_2d.setVisible(true);
     
              	 	 combo2Item = torso;
     
              	  //Sub Level 2d - Torso  
    				label_2d.setFont(new Font("Arial", Font.BOLD, 18)); 
    				label_2d.setForeground(Color.white); 
     
    				comboBox_2d.setFont(new Font("Arial", Font.BOLD, 22)); 
    				comboBox_2d.setForeground(Color.white); 
    				comboBox_2d.setBackground(Color.blue);  
     
    				add(label_2d); 
    				add(comboBox_2d); 
     
            }
            else { 
            	label_2d.setVisible(false);
            	comboBox_2d.setVisible(false);
            	}
             if(selectedValue.equals("General Medical")){
     
             	label_2e.setVisible(true);
            	comboBox_2e.setVisible(true);
     
                combo2Item = generalMedical;
     
                //Sub Level 2e - General Medical 
    			label_2e.setFont(new Font("Arial", Font.BOLD, 18)); 
    			label_2e.setForeground(Color.white); 
     
    			comboBox_2e.setFont(new Font("Arial", Font.BOLD, 22)); 
    			comboBox_2e.setForeground(Color.white); 
    			comboBox_2e.setBackground(Color.blue);  
     
    			add(label_2e); 
    			add(comboBox_2e); 
     
            }
            else { 
            	label_2e.setVisible(false);
            	comboBox_2e.setVisible(false);
            	}
            for(String val : combo2Item){
                model.addElement(val);
        	}
        //Submit Button
        //
        //TODO: 
        //		*Takes a Time&Date Stamp, and data filled out, then prints to a .txt file
        //		
        /*	add(submit);
        	submit.setFont(new Font("Arial", Font.BOLD, 22)); 
    			submit.setForeground(Color.white); 
    			submit.setBackground(Color.blue);
    		submit.setVisible(true);
    		submit.addActionListener(this);
    	*/		 	
    	}
    }


    --- Update ---

    I'm trying to add them to an array index, but I can't seem to figure it out. I am getting an incompatible data types error, thus I presume that I am declaring them incorrectly.

    I added the declaration for the JPanel array here:
     
    public class SignIn extends JFrame implements ActionListener
    {
    	JPanel[][] panel = new JPanel[9][1]; //<--------HERE
     
    	JButton submit = new JButton("Submit"); 
    	JLabel nameLabel = new JLabel("Enter Your First and Last Name:");
    	JLabel label = new JLabel("Select Your Sport:");
    I have my JLabels and JButton declared here as:
     
    JButton submit = new JButton("Submit"); 
    	JLabel nameLabel = new JLabel("Enter Your First and Last Name:");
    	JLabel label = new JLabel("Select Your Sport:");
    	JLabel label_1 = new JLabel("Body Part:");
    		JLabel label_2a = new JLabel("Upper Extremity:");
    		JLabel label_2b = new JLabel("Lower Extremity:");
    		JLabel label_2c = new JLabel("Head:");
    		JLabel label_2d = new JLabel("Torso:"); 
    		JLabel label_2e = new JLabel("General Medical:");
    			JLabel label_3a = new JLabel("Side:");
    			JLabel label_3b = new JLabel("Injury:");
    			JLabel label_3c_a = new JLabel("Treatment:");
    			JLabel label_3c_b = new JLabel("Treatment (Optional):");
    And my ComboBoxes declared here as:
     
    JTextPane enterName = new JTextPane();
    	JComboBox comboBox = new JComboBox(sports);
    	JComboBox comboBox_1 = new JComboBox(bodyPart);
    		JComboBox comboBox_2a = new JComboBox(upperExtremity);
    		JComboBox comboBox_2b = new JComboBox(lowerExtremity);
    		JComboBox comboBox_2c = new JComboBox(head);
    		JComboBox comboBox_2d = new JComboBox(torso);
    		JComboBox comboBox_2e = new JComboBox(generalMedical);
    			JComboBox comboBox_3a = new JComboBox(side);
    			JComboBox comboBox_3b = new JComboBox(injury);
    			JComboBox comboBox_3c_a = new JComboBox(treatment);
    			JComboBox comboBox_3c_b = new JComboBox(treatment);

    My array assignments are here as:
     
    	public SignIn() 
    	{ 
     
    		//JPanel Array Declarations
    			//Name
    			nameLabel = panel[0][0];
    			enterName = panel[0][1];
    			//Sport Selection
    			sports = panel[1][0];
    			comboBox = panel[1][1];
    			//Body Part Selection
    			bodyPart = panel[2][0];
    			comboBox_1 = panel[2][1];
    				//Upper Extremity
    				upperExtremity = panel[3][0];
    				comboBox_2a = panel[3][1];
    				//Lower Extremity
    				lowerExtremity = panel[3][0];
    				comboBox_2b = panel[3][1];
    				//Head
    				head = panel[3][0];
    				comboBox_2c = panel[3][1];
    				//Torso
    				torso = panel[3][0];
    				comboBox_2d = panel[3][1];
    				//General Medical
    				generalMedical = panel[3][0];
    				comboBox_2e = panel[3][1];
    					//Side
    					side = panel[4][0];
    					comboBox_3a = panel[4][1];
    					//Injury
    					side = panel[4][0];
    					comboBox_3b = panel[4][1];
    					//Treatment
    					treatment = panel[4][0];
    					comboBox_3c_a = panel[4][1];
    						//Treatment (Optional)
    						treatment = panel[5][0];
    						comboBox_3c_b = panel[5][1];

    And here is a copy of my complete code as of right now:
     
     
    import java.awt.*;
    import javax.swing.*;
    import java.util.*;
    import java.awt.event.*;
     
     
    public class SignIn extends JFrame implements ActionListener
    {
    	JPanel[][] panel = new JPanel[9][1];
     
    	JButton submit = new JButton("Submit"); 
    	JLabel nameLabel = new JLabel("Enter Your First and Last Name:");
    	JLabel label = new JLabel("Select Your Sport:");
    	JLabel label_1 = new JLabel("Body Part:");
    		JLabel label_2a = new JLabel("Upper Extremity:");
    		JLabel label_2b = new JLabel("Lower Extremity:");
    		JLabel label_2c = new JLabel("Head:");
    		JLabel label_2d = new JLabel("Torso:"); 
    		JLabel label_2e = new JLabel("General Medical:");
    			JLabel label_3a = new JLabel("Side:");
    			JLabel label_3b = new JLabel("Injury:");
    			JLabel label_3c_a = new JLabel("Treatment:");
    			JLabel label_3c_b = new JLabel("Treatment (Optional):");
     
    	final int FRAME_WIDTH = 600; 
    	final int FRAME_HEIGHT = 400; 
    	String[] sports = { "---", "Football", "Boy's Soccer", "Girl's Soccer", "Field Hockey", "Cross Country", "Tennis", "Boy's Basketball", "Girl's Basketball", "Wrestling", "Cheer", "Bowling", "Baseball", "Softball", "Boy's Lacrosse", "Girl's Lacrosse", "Track & Field", "Other"}; 
    	String[] bodyPart = { "---", "Upper Extremity", "Lower Extremity", "Head", "Torso", "General Medical"};
    		String[] upperExtremity = { "---", "Elbow", "Finger", "Forearm", "Hand", "Shoulder", "Thumb", "Upper Arm", "Wrist", "Other"};
    		String[] lowerExtremity = { "---", "Ankle", "Calf", "Foot", "Groin", "Heel", "Hip", "Knee", "Lower Leg", "Shin", "Thigh", "Toes", "Other"};
    		String[] head = { "---", "Brain", "Ear", "Eye", "Face", "Forehead", "Jaw", "Nose", "Scalp", "Skull", "Other"};	
    		String[] torso = { "---", "Abdomen", "Back", "Cervical Spine", "Chest", "Clavicle", "Flank", "Lumber Spine", "Neck", "Rib", "Sacrum (Tailbone)", "Sternum", "Thoracic Spine", "Other"};	
    		String[] generalMedical = { "---", "Side", "Injury", "Treatment", "Other"};
    			String[] side = { "---", "Right", "Left", "Midline", "Front", "Back", "Other"};
    			String[] injury = { "---", "Amputation", "Asphyxiation", "Bite", "Blister", "Burn", "Concussion", "Contusion/ Bruise", "Cut/ Laceration/ Abrasion", "Dislocation", "Fracture", "Hearing Loss", "Heat Illness", "Hypothermia", "Shock", "Shock", "Sore", "Sprain", "Strain", "Vision Loss", "Other" };
    			String[] treatment = { "---", "Bandage", "Brace/ Splint/ Cast", "Combo", "Contrast Bath", "Electrotherapy", "Hi-Volt", "Ice Bag/ Pack", "Ice Massage", "Inferential", "Massage", "Moist Heat Pack", "Muscle Stimulator", "Padding", "Paraffin", "Rehab Excercises", "Russian", "Stretch", "Tape", "TENS", "Ultrasound", "Whirlpool", "Wound Care", "Other"};
    	JTextPane enterName = new JTextPane();
    	JComboBox comboBox = new JComboBox(sports);
    	JComboBox comboBox_1 = new JComboBox(bodyPart);
    		JComboBox comboBox_2a = new JComboBox(upperExtremity);
    		JComboBox comboBox_2b = new JComboBox(lowerExtremity);
    		JComboBox comboBox_2c = new JComboBox(head);
    		JComboBox comboBox_2d = new JComboBox(torso);
    		JComboBox comboBox_2e = new JComboBox(generalMedical);
    			JComboBox comboBox_3a = new JComboBox(side);
    			JComboBox comboBox_3b = new JComboBox(injury);
    			JComboBox comboBox_3c_a = new JComboBox(treatment);
    			JComboBox comboBox_3c_b = new JComboBox(treatment);
    	public SignIn() 
    	{ 
     
    		//JPanel Array Declarations
    			//Name
    			nameLabel = panel[0][0];
    			enterName = panel[0][1];
    			//Sport Selection
    			sports = panel[1][0];
    			comboBox = panel[1][1];
    			//Body Part Selection
    			bodyPart = panel[2][0];
    			comboBox_1 = panel[2][1];
    				//Upper Extremity
    				upperExtremity = panel[3][0];
    				comboBox_2a = panel[3][1];
    				//Lower Extremity
    				lowerExtremity = panel[3][0];
    				comboBox_2b = panel[3][1];
    				//Head
    				head = panel[3][0];
    				comboBox_2c = panel[3][1];
    				//Torso
    				torso = panel[3][0];
    				comboBox_2d = panel[3][1];
    				//General Medical
    				generalMedical = panel[3][0];
    				comboBox_2e = panel[3][1];
    					//Side
    					side = panel[4][0];
    					comboBox_3a = panel[4][1];
    					//Injury
    					side = panel[4][0];
    					comboBox_3b = panel[4][1];
    					//Treatment
    					treatment = panel[4][0];
    					comboBox_3c_a = panel[4][1];
    						//Treatment (Optional)
    						treatment = panel[5][0];
    						comboBox_3c_b = panel[5][1];
     
     
    		super("Athletic Trainer Sign In");	
    		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
    		setLayout (new GridLayout(10,2)); 
    		getContentPane().setBackground(new Color(178, 178, 56));
    		//Sub Level 1 - Enter Name
    		add(nameLabel);
     		add(enterName);
     		nameLabel.setFont(new Font("Arial", Font.BOLD, 18)); 
    		nameLabel.setForeground(Color.white);
     		enterName.setFont(new Font("Arial", Font.BOLD, 22)); 
    		enterName.setForeground(Color.white); 
    		enterName.setBackground(Color.blue);
     
     
    		//Sub Level 1 - Sport
    		label.setFont(new Font("Arial", Font.BOLD, 18)); 
    		label.setForeground(Color.white); 
     
    		comboBox.setFont(new Font("Arial", Font.BOLD, 22)); 
    		comboBox.setForeground(Color.white); 
    		comboBox.setBackground(Color.blue);
     
     		add(label); 
    		add(comboBox); 
    		setSize(FRAME_WIDTH, FRAME_HEIGHT); 
     
    		//Sub Level 1 - Body Part
    		label_1.setFont(new Font("Arial", Font.BOLD, 18)); 
    		label_1.setForeground(Color.white); 
     
    		comboBox_1.setFont(new Font("Arial", Font.BOLD, 22)); 
    		comboBox_1.setForeground(Color.white); 
    		comboBox_1.setBackground(Color.blue);  
     
    		add(label_1); 
    		add(comboBox_1); 
     
    		comboBox_1.addActionListener(this); 
     
    //****************************************************
     
    				//Sub Level 3a - Side
    				label_3a.setFont(new Font("Arial", Font.BOLD, 18)); 
    				label_3a.setForeground(Color.white); 
     
    				comboBox_3a.setFont(new Font("Arial", Font.BOLD, 22)); 
    				comboBox_3a.setForeground(Color.white); 
    				comboBox_3a.setBackground(Color.blue);  
     
    				add(label_3a); 
    				add(comboBox_3a); 
     
     
    				//Sub Level 3b - Injury
    				label_3b.setFont(new Font("Arial", Font.BOLD, 18)); 
    				label_3b.setForeground(Color.white); 
     
    				comboBox_3b.setFont(new Font("Arial", Font.BOLD, 22)); 
    				comboBox_3b.setForeground(Color.white); 
    				comboBox_3b.setBackground(Color.blue);  
     
    				add(label_3b); 
    				add(comboBox_3b); 
     
     
    				//Sub Level 3c-a - Treatment
    				label_3c_a.setFont(new Font("Arial", Font.BOLD, 18)); 
    				label_3c_a.setForeground(Color.white); 
     
    				comboBox_3c_a.setFont(new Font("Arial", Font.BOLD, 22)); 
    				comboBox_3c_a.setForeground(Color.white); 
    				comboBox_3c_a.setBackground(Color.blue);  
     
    				add(label_3c_a); 
    				add(comboBox_3c_a); 
     
     
    				//Sub Level 3c-b - Treatment
    				label_3c_b.setFont(new Font("Arial", Font.BOLD, 18)); 
    				label_3c_b.setForeground(Color.white); 
     
    				comboBox_3c_b.setFont(new Font("Arial", Font.BOLD, 22)); 
    				comboBox_3c_b.setForeground(Color.white); 
    				comboBox_3c_b.setBackground(Color.blue);  
     
    				add(label_3c_b); 
    				add(comboBox_3c_b); 
     
    	}	
     
    //***********************************************************
     
    	public static void main(String[] args) 
    		{ 
    			SignIn frame = new SignIn(); 
    			frame.setVisible(true); 
      	}
     
      	@Override
        public void actionPerformed(ActionEvent e) {
        	String selectedValue = comboBox_1.getSelectedItem().toString();
            String[] combo2Item = null;
     
            DefaultComboBoxModel model = (DefaultComboBoxModel) comboBox_2a.getModel();      
            model.removeAllElements();
     
            if(selectedValue.equals("Upper Extremity")){
     
            	label_2a.setVisible(true);
            	comboBox_2a.setVisible(true);
     
            	combo2Item = upperExtremity;
     
                //Sub Level 2a - Upper Extremity
    			label_2a.setFont(new Font("Arial", Font.BOLD, 18)); 
    			label_2a.setForeground(Color.white); 
     
    			comboBox_2a.setFont(new Font("Arial", Font.BOLD, 22)); 
    			comboBox_2a.setForeground(Color.white); 
    			comboBox_2a.setBackground(Color.blue);  
     
    			add(label_2a); 
    			add(comboBox_2a); 
     
     
            } else { 
            	label_2a.setVisible(false);
            	comboBox_2a.setVisible(false);
            	}
            	if(selectedValue.equals("Lower Extremity")){
     
            		label_2b.setVisible(true);
            		comboBox_2b.setVisible(true);
     
              	 	combo2Item = lowerExtremity;
     
             	   //Sub Level 2b - Lower Extremity
    				label_2b.setFont(new Font("Arial", Font.BOLD, 18)); 
    				label_2b.setForeground(Color.white); 
     
    				comboBox_2b.setFont(new Font("Arial", Font.BOLD, 22)); 
    				comboBox_2b.setForeground(Color.white); 
    				comboBox_2b.setBackground(Color.blue);  
     
    				add(label_2b); 
    				add(comboBox_2b); 
     
     
            } else { 
            	label_2b.setVisible(false);
            	comboBox_2b.setVisible(false);
            	}
            	 if(selectedValue.equals("Head")){
     
            	label_2c.setVisible(true);
            	comboBox_2c.setVisible(true);
     
                	combo2Item = head;
     
                	//Sub Level 2c - Head
    				label_2c.setFont(new Font("Arial", Font.BOLD, 18)); 
    				label_2c.setForeground(Color.white); 
     
    				comboBox_2c.setFont(new Font("Arial", Font.BOLD, 22)); 
    				comboBox_2c.setForeground(Color.white); 
    				comboBox_2c.setBackground(Color.blue);  
     
    				add(label_2c); 
    				add(comboBox_2c); 
     
     
            } 
            	else { 
            		label_2c.setVisible(false);
            		comboBox_2c.setVisible(false);
            	}
     
            	if(selectedValue.equals("Torso")){
     
            		label_2d.setVisible(true);
            		comboBox_2d.setVisible(true);
     
              	 	 combo2Item = torso;
     
              	  //Sub Level 2d - Torso  
    				label_2d.setFont(new Font("Arial", Font.BOLD, 18)); 
    				label_2d.setForeground(Color.white); 
     
    				comboBox_2d.setFont(new Font("Arial", Font.BOLD, 22)); 
    				comboBox_2d.setForeground(Color.white); 
    				comboBox_2d.setBackground(Color.blue);  
     
    				add(label_2d); 
    				add(comboBox_2d); 
     
            }
            else { 
            	label_2d.setVisible(false);
            	comboBox_2d.setVisible(false);
            	}
             if(selectedValue.equals("General Medical")){
     
             	label_2e.setVisible(true);
            	comboBox_2e.setVisible(true);
     
                combo2Item = generalMedical;
     
                //Sub Level 2e - General Medical 
    			label_2e.setFont(new Font("Arial", Font.BOLD, 18)); 
    			label_2e.setForeground(Color.white); 
     
    			comboBox_2e.setFont(new Font("Arial", Font.BOLD, 22)); 
    			comboBox_2e.setForeground(Color.white); 
    			comboBox_2e.setBackground(Color.blue);  
     
    			add(label_2e); 
    			add(comboBox_2e); 
     
            }
            else { 
            	label_2e.setVisible(false);
            	comboBox_2e.setVisible(false);
            	}
            for(String val : combo2Item){
                model.addElement(val);
        	}
        //Submit Button
        //
        //TODO: 
        //		*Takes a Time&Date Stamp, and data filled out, then prints to a .txt file
        //		
        /*	add(submit);
        	submit.setFont(new Font("Arial", Font.BOLD, 22)); 
    			submit.setForeground(Color.white); 
    			submit.setBackground(Color.blue);
    		submit.setVisible(true);
    		submit.addActionListener(this);
    	*/		 	
    	}
    }

  14. #14
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,130
    Thanks
    48
    Thanked 2,156 Times in 2,129 Posts

    Default Re: JLabel/ComboBox Issues

    I suggest that you go back to the previous version. When when making changes, do them in small steps and compile often to see so you won't get so many errors.

    One thing I see that does not make sense is the changing of the contents of comboBox_2a's model when it is not the combobox that is being shown.
    Another problem is the continual adding of components. The layout manager keeps all the components that were already added and puts the newly added ones at the end of the list that is has been keeping.
    If you don't understand my answer, don't ignore it, ask a question.

  15. #15
    Member
    Join Date
    Oct 2012
    Posts
    35
    My Mood
    Fine
    Thanks
    5
    Thanked 1 Time in 1 Post

    Default Re: JLabel/ComboBox Issues

    Quote Originally Posted by Norm View Post
    The layout manager keeps all the components that were already added and puts the newly added ones at the end of the list that is has been keeping.
    This is where my problem was originating from though. I don't want it to add them to them bottom, but to overwrite them, to have them put in the same spot where the previous one was. For example, if the user first selects "General Medical" then goes back and changes it to "Upper Extremity", there will be a blank space where the "General Medical" comboBox was previously at, and the "Upper Extremity" comboBox will be displayed below the invisible "General Medical" comboBox. I want it so that the "Upper Extremity" comboBox is placed where the "General Medical" comboBox was previously at.

  16. #16
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,130
    Thanks
    48
    Thanked 2,156 Times in 2,129 Posts

    Default Re: JLabel/ComboBox Issues

    but to overwrite them
    Why not just reuse one that is already there.
    You can change the text of a label and the list of items in a combobox.
    If you don't understand my answer, don't ignore it, ask a question.

  17. #17
    Member
    Join Date
    Oct 2012
    Posts
    35
    My Mood
    Fine
    Thanks
    5
    Thanked 1 Time in 1 Post

    Default Re: JLabel/ComboBox Issues

    Oh, I see! I didn't even think of that! So instead of having one comboBox/Label for each array, have one for each position and modify the contents as needed.

  18. #18
    Super Moderator curmudgeon's Avatar
    Join Date
    Aug 2012
    Posts
    1,130
    My Mood
    Cynical
    Thanks
    64
    Thanked 139 Times in 134 Posts

    Default Re: JLabel/ComboBox Issues

    I would work on this aspect of the program in isolation, in its own small program that does nothing but swapping components based on the selection of a combo box. Take out all other bits of code unrelated to this problem and work on it alone. Then it will be much easier for you to figure out, and easier for us to help you with this code if it still doesn't work.

    For example, one small bit of code that uses a Map<String, JPanel> to swap JLabels in and out:

    import java.awt.Color;
    import java.awt.GridLayout;
    import java.awt.event.ItemEvent;
    import java.awt.event.ItemListener;
    import java.util.HashMap;
    import java.util.Map;
     
    import javax.swing.*;
     
    public class PanelGridTest extends JPanel {
       private static final String[] ITEMS = {"One", "Two", "Three", "Four"};
       private JComboBox combo = new JComboBox(ITEMS);
       private Map<String, JPanel> panelMap = new HashMap<String, JPanel>();
     
       public PanelGridTest() {
          setLayout(new GridLayout(0, 1)); // 1 column, variable number of rows
          JPanel comboPanel = new JPanel();
          comboPanel.add(combo);
     
          add(comboPanel);
          for (String item : ITEMS) {
             JPanel panel = new JPanel();
             panel.setBorder(BorderFactory.createLineBorder(Color.black));
             panelMap.put(item, panel);
             add(panel);
          }
          combo.setSelectedIndex(-1);
     
          combo.addItemListener(new ItemListener() {
     
             @Override
             public void itemStateChanged(ItemEvent e) {
                // first clear all panels
                for (String itemKey : ITEMS) {
                   JPanel panel = panelMap.get(itemKey);
                   panel.removeAll();
                   panel.revalidate();
                   panel.repaint();
                }
     
                // get the selected item from the combobox
                String itemKey = e.getItem().toString();
     
                // use it to get the selected JPanel
                JPanel panel = panelMap.get(itemKey);
     
                // add component to JPanel
                panel.add(new JLabel(itemKey));
     
                // revalidate to lay out component and repaint to show
                panel.revalidate();
                panel.repaint();
             }
          });
       }
     
       private static void createAndShowGui() {
          PanelGridTest mainPanel = new PanelGridTest();
     
          JFrame frame = new JFrame("PanelGridTest");
          frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
          frame.getContentPane().add(mainPanel);
          frame.pack();
          frame.setLocationByPlatform(true);
          frame.setVisible(true);
       }
     
       public static void main(String[] args) {
          SwingUtilities.invokeLater(new Runnable() {
             public void run() {
                createAndShowGui();
             }
          });
       }
    }

    You obviously can't use this code since your requirements are different -- to swap a selection of JComboBoxes from one JPanel based on the selection of another JComboBox, but it should give you the idea of this small program that I recommend.

    --- Update ---

    Quote Originally Posted by Blasfemmy View Post
    Oh, I see! I didn't even think of that! So instead of having one comboBox/Label for each array, have one for each position and modify the contents as needed.
    Yes, this is a much better idea, one that I suggested yesterday in post #6. Please re-read it.

    --- Update ---

    Quote Originally Posted by Blasfemmy View Post
    Oh, I see! I didn't even think of that! So instead of having one comboBox/Label for each array, have one for each position and modify the contents as needed.
    Yes, this is a much better idea, one that I suggested yesterday in post #6. Please re-read it.

Similar Threads

  1. Loop through JLabel and change JLabel
    By JoeBrown in forum What's Wrong With My Code?
    Replies: 2
    Last Post: April 11th, 2012, 12:52 PM
  2. Adding JLabel on other Jlabel
    By mike416 in forum AWT / Java Swing
    Replies: 3
    Last Post: March 29th, 2012, 11:50 AM
  3. Jlabel issues
    By Wadashe in forum Java Theory & Questions
    Replies: 2
    Last Post: February 27th, 2012, 01:39 PM
  4. [SOLVED] Populating second combobox from a combobox help!
    By ComputerSaysNo in forum AWT / Java Swing
    Replies: 7
    Last Post: October 18th, 2011, 09:01 AM
  5. JLabel placement issues
    By fride360 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: March 11th, 2011, 12:20 PM

Tags for this Thread