Results 1 to 4 of 4
- 11-13-2008, 05:13 AM #1
Member
- Join Date
- Oct 2008
- Posts
- 2
- Rep Power
- 0
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); } } }
Thanks in advance!
(Bah! I spelled "an" incorrectly!)
-
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.
- 11-13-2008, 06:24 AM #3
Member
- Join Date
- Oct 2008
- Posts
- 2
- Rep Power
- 0
I never would have thought of that...thank you very much!
-
Similar Threads
-
array problems need your help ASAP!
By notherand in forum New To JavaReplies: 1Last Post: 06-29-2008, 09:59 PM -
Problem with displaying search results from an array
By BHCluster in forum New To JavaReplies: 4Last Post: 04-24-2008, 04:34 AM -
Problems with JPanels and displaying
By Mastergeek666 in forum AWT / SwingReplies: 1Last Post: 01-19-2008, 01:32 AM -
problems with asigning elements of an array to a constructor
By rednessc in forum New To JavaReplies: 1Last Post: 12-14-2007, 08:25 AM -
problems with array index
By mary in forum New To JavaReplies: 2Last Post: 08-01-2007, 05:30 PM
Bookmarks