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

Thread: Array of JPanels not showing.

  1. #1
    Member
    Join Date
    May 2013
    Posts
    106
    My Mood
    Amused
    Thanks
    16
    Thanked 9 Times in 9 Posts

    Default Array of JPanels not showing.

    I'm trying to add 5 identical JPanels to a frame. Each panel has 2 radio buttons and a slider bar. The individual panels seem to render okay, but if I add them to the frame, only the last one is shown. It looks like the others were added--you can see a big space for 4 panels--but they seem to be invisible or their individual components are missing.




    The panels are in an array, and I added components to them using a for loop.

     
    	private void makeControlFrame()
    	{
    		JFrame controlPanel = new JFrame("Fan spinner control panel.");
    		JPanel componentControlPanel[] = new JPanel[5];
     
    		ButtonGroup grpDirection = new ButtonGroup();
    		JRadioButton jrbClockWise = new JRadioButton("CLOCKWISE");
    		JRadioButton jrbCounterClockWise = new JRadioButton("COUNTERCLOCKWISE");
     
    		JSlider jslSpeed = new JSlider(JSlider.HORIZONTAL,1,2000,100);
     
    		controlPanel.setLayout(new GridLayout(5,1));
     
    		grpDirection.add(jrbClockWise);
    		grpDirection.add(jrbCounterClockWise);
     
    		for(int i = 0; i < 5; i++)
    		{
    			componentControlPanel[i] = new JPanel();
    			componentControlPanel[i].setLayout(new GridLayout (3,1));
    			componentControlPanel[i].add(jrbClockWise);
    			componentControlPanel[i].add(jrbCounterClockWise);
    			componentControlPanel[i].add(jslSpeed);
    			controlPanel.add(componentControlPanel[i]);
     
    		}	
     
    		controlPanel.setVisible(true);
    		controlPanel.setLocationRelativeTo(null);
    		controlPanel.pack();
     
    }


  2. #2
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,318
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: Array of JPanels not showing.

    Every loop iteration you are adding the same components to the newly created JPanel. A Component can only have a single parent, thus will only show up within the last Container it has been added to. You will need to create new Components (JRadioButton and JSlider) for each component you wish to add them to.

  3. The Following User Says Thank You to copeg For This Useful Post:

    mstabosz (September 10th, 2013)

  4. #3
    Member
    Join Date
    May 2013
    Posts
    106
    My Mood
    Amused
    Thanks
    16
    Thanked 9 Times in 9 Posts

    Default Re: Array of JPanels not showing.

    Oh hmm. That's interesting. I didn't realize that. I might have to do a few more arrays then.... or maybe arrays are more trouble than they're worth in this instance. Thanks for the insight copeg.

  5. #4
    Member
    Join Date
    May 2013
    Posts
    106
    My Mood
    Amused
    Thanks
    16
    Thanked 9 Times in 9 Posts

    Default Re: Array of JPanels not showing.

    Okay, got this working. That thing about needing 1 of each swing element for each panel is one of those things that seems super obvious in hindsight. Here's what I ended up with:

     
    	//Method makes the control panel frame.
    	private void makeControlFrame()
    	{
     
    		//Primary frame.
    		JFrame controlPanel = new JFrame("CONTROL PANEL");
     
    		//Five control panels each to hold controls for one fan or star.
    		JPanel componentControlPanel[] = new JPanel[5];
     
    		//Labels for control panel elements.
    		String[] labels = {"North Fan" , "East Fan", "South Fan", "West Fan", "Center Star"};		
     
    		controlPanel.setLayout(new GridLayout(5,1));
    		controlPanel.addWindowListener(new FrameListener());
     
    		//Add elements to sub panels, then add to the control panel frame.
    		for(int i = 0; i < 5; i++)
    		{
     
    			//Create the basic border.
    			Border subPanelBorder = BorderFactory.createLineBorder(Color.BLACK);
    			//Modify it to add the name of the current element.
    			subPanelBorder = BorderFactory.createTitledBorder(subPanelBorder, labels[i]);
     
    			//Create GUI elements for this sub panel.
    			componentControlPanel[i] = new JPanel();
    			//componentControlPanel[i].add(new JLabel(labels[i] +" Direction"));
    			grpDirection[i] = new ButtonGroup();
    			jslSpeed[i] = new JSlider(JSlider.HORIZONTAL,1,1000,100);
    			jrbClockWise[i] = new JRadioButton("Clockwise");
    			jrbCounterClockWise[i] = new JRadioButton("Counterclockwise");			
     
    			//Set attributes of speed slider bar.
    			jslSpeed[i].setMinorTickSpacing(100);
    			jslSpeed[i].setPaintTicks(true);
    			jslSpeed[i].setPaintLabels(true);
     
    			//Group radio buttons.
    			grpDirection[i].add(jrbClockWise[i]);
    			grpDirection[i].add(jrbCounterClockWise[i]);
     
    			//Set layout manager for the sub panel.
    			componentControlPanel[i].setLayout(new FlowLayout(FlowLayout.LEFT));
     
    			//Another sub panel to group the radio buttons.
    			JPanel radioSubPanel = new JPanel();
    			radioSubPanel.setLayout(new GridLayout(3,1));
    			radioSubPanel.add(new JLabel("Direction"));
    			radioSubPanel.add(jrbClockWise[i]);
    			radioSubPanel.add(jrbCounterClockWise[i]);
     
    			//Another sub panel to group the speed slider and label.
    			JPanel sliderSubPanel = new JPanel();
    			sliderSubPanel.setLayout(new GridLayout(3,1));
    			sliderSubPanel.add(new JLabel("Speed"));
    			sliderSubPanel.add(new JLabel("FAST -------------------------------  SLOW"));
    			sliderSubPanel.add(jslSpeed[i]);
     
    			//Add radio and slider sub panels to control panel.
    			componentControlPanel[i].add(sliderSubPanel);
    			componentControlPanel[i].add(radioSubPanel);
     
    			//Register listeners.
    			jslSpeed[i].addChangeListener(new HandleSliders(i));
    			jrbClockWise[i].addActionListener(new HandleRadioButtons(i));
    			jrbCounterClockWise[i].addActionListener(new HandleRadioButtons(i));
     
    			//Set the title border for the control panel.
    			componentControlPanel[i].setBorder(subPanelBorder);
     
    			//Add this panel to the main frame.
    			controlPanel.add(componentControlPanel[i]);
    		}	
    		//Speed slider bar for the star has different minimum and maximum values.
    		jslSpeed[4].setValue(500);
    		jslSpeed[4].setMinimum(100);
    		jslSpeed[4].setMaximum(2000);
    		jslSpeed[4].setMinorTickSpacing(200);
     
    		//Set attributes of frame.
    		controlPanel.setVisible(true);
    		controlPanel.setResizable(false);
    		controlPanel.pack();
     
    	}

Similar Threads

  1. Changing JPanels
    By Damian3395 in forum What's Wrong With My Code?
    Replies: 2
    Last Post: July 6th, 2013, 09:01 PM
  2. CardLayout which between JPanels
    By ICEPower in forum AWT / Java Swing
    Replies: 3
    Last Post: April 5th, 2013, 03:28 PM
  3. Replies: 3
    Last Post: March 6th, 2012, 03:50 AM
  4. 1 JFrame, more JPanels
    By jknwhz in forum AWT / Java Swing
    Replies: 3
    Last Post: October 24th, 2011, 11:18 PM
  5. JComboBox selection not showing item from object array
    By oper125 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: November 24th, 2010, 06:31 AM