Results 1 to 4 of 4
  1. #1
    Join Date
    Oct 2008
    Posts
    2
    Rep Power
    0

    Default Problems displaying and array of JPanels

    Hello! I'm having a bit of a problem displaying an array of JPanels I've made in my program. The basic idea of it is to read in an arbitrary integral value from a text file and make a corresponding number of JPanels (each with its own components of course). I've taken the liberty of simplifying it, making the "arbitrary integral value" equal to three...since I don't want to bombard you with useless code and since I already know how to read in the data.

    Java Code:
    import java.awt.*;
    import javax.swing.*;
    import java.awt.event.*;
    import java.util.*;
    import java.io.*;
    
    public class Paneler
    {
    	private JFrame theWindow;
    	private PanelType [] thePanels;
    	private Container thePane;
    
    	public static void main(String [] arguments)
    	{
    		new Paneler();
    	}
    
    	public Paneler()
    	{
    		int numPanels = 3;
    		thePanels = new PanelType[numPanels];
    
    		theWindow = new JFrame("UGH");
    		theWindow.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    
    		thePane = theWindow.getContentPane();
    
    		for (int i = 0; i < numPanels; i++) //is this loop correct?  
    		{
    			thePanels[i] = new PanelType();
    			thePane.add(thePanels[i]);
    		}
    
    		theWindow.pack();
    		theWindow.setVisible(true);
    	}
    
    	private class PanelType extends JPanel
    	{
    		private JLabel message;
    
    		public PanelType()
    		{
    			message = new JLabel("What is wrong with this program?!");
    			add(message);
    		}
    	}
    
    }
    The problem is that I'm only getting one JPanel with one JLabel message; id est, the "What is wrong with this program?!" I'm fairly new to Java-based GUI (and to Java itself), but shouldn't I have three panels and, consequently, three JLabel messages?

    Thanks in advance!


    (Bah! I spelled "an" incorrectly!)

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    The key knowledge that you need to study and learn well is to use layout managers. Please read the Sun Swing tutorial on this and it will all become clear. The tutorial is quite well written and very understandable. You can find it here:

    Lesson: Laying Out Components Within a Container (The Java™ Tutorials > Creating a GUI with JFC/Swing)

    When you read this, you'll see that the standard layout used in a contentPane is the BorderLayout. If you add a JComponent to a BorderLayout-using Container without specifying where to put it, then the layout assumes that you want to put it in the BorderLayout.CENTER position. Each component added this way will cover up any previous components added to the CENTER spot, and so you'll see only one panel. To solve this problem above, I'd use the GridLayout set to have one column and many rows. For instance:

    Java Code:
    import java.awt.*;
    import javax.swing.*;
    
    public class Paneler
    {
      private JFrame theWindow;
      private PanelType[] thePanels;
      // private Container thePane;
      private JPanel thePane;  // changed
    
      public static void main(String[] arguments)
      {
        new Paneler();
      }
    
      public Paneler()
      {
        int numPanels = 3;
        thePanels = new PanelType[numPanels];
    
        theWindow = new JFrame("UGH");
        theWindow.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    
        thePane = (JPanel)theWindow.getContentPane();
        thePane.setLayout(new GridLayout(0, 1, 0, 10));
    
        for (int i = 0; i < numPanels; i++) //is this loop correct?  
        {
          thePanels[i] = new PanelType();
          thePane.add(thePanels[i]);
        }
    
        theWindow.pack();
        theWindow.setVisible(true);
      }
    
      private class PanelType extends JPanel
      {
        private JLabel message;
    
        public PanelType()
        {
          message = new JLabel("What is wrong with this program?!");
          add(message);
        }
      }
    
    }
    Last edited by Fubarable; 11-13-2008 at 05:23 AM.

  3. #3
    Join Date
    Oct 2008
    Posts
    2
    Rep Power
    0

    Default

    I never would have thought of that...thank you very much!

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

Similar Threads

  1. array problems need your help ASAP!
    By notherand in forum New To Java
    Replies: 1
    Last Post: 06-29-2008, 09:59 PM
  2. Problem with displaying search results from an array
    By BHCluster in forum New To Java
    Replies: 4
    Last Post: 04-24-2008, 04:34 AM
  3. Problems with JPanels and displaying
    By Mastergeek666 in forum AWT / Swing
    Replies: 1
    Last Post: 01-19-2008, 01:32 AM
  4. Replies: 1
    Last Post: 12-14-2007, 08:25 AM
  5. problems with array index
    By mary in forum New To Java
    Replies: 2
    Last Post: 08-01-2007, 05:30 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
  •