Results 1 to 15 of 15
  1. #1
    Josep_16's Avatar
    Josep_16 is offline Member
    Join Date
    Mar 2011
    Posts
    40
    Rep Power
    0

    Default Can't I add containers into containers? What's wrong here?

    Hey again. I am trying to add 3 JPanels inside another, which one is added to another one, which is added to the Applet. The code is the next one:

    Java Code:
    setLayout(null);
    	container = new JPanel();
    	add(container);
    	container.setLocation(xContainer, yContainer);
    	container1 = new JPanel(new GridLayout(0,3,5,0));
    	container2 = new JPanel(new GridLayout(5,2,0,0));
    	container3 = new JPanel(new GridLayout(6,2,0,0));
    	container4 = new JPanel(new GridLayout(3,2,0,0));
    	container.add(container1);
    	container1.add(container2, 0);
    	container1.add(container3, 1);
    	container1.add(container4, 2);

    Then, I add components to container2, 3 and 4. The problem is that I can't see anything on the screen related to the JPanels. Is there anything ill-posed ? Thanks.

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

    Default

    Get rid of your use of null layout as it's messing you up and for the most part should almost never be used. Better to use the flexible layout mangers as you're using for some of your JPanels.

  3. #3
    Josep_16's Avatar
    Josep_16 is offline Member
    Join Date
    Mar 2011
    Posts
    40
    Rep Power
    0

    Default

    Quote Originally Posted by Fubarable View Post
    Get rid of your use of null layout as it's messing you up and for the most part should almost never be used. Better to use the flexible layout mangers as you're using for some of your JPanels.
    Yes, that's a great idea. But now I have another problem. When I try to add a new Button or JButton to the Applet, outside the "container" JPanel, as it uses the BorderLayout, and that layout specifies that if a component does not fill all the Applet site, another one must do (in brief that the layout must fill all the Applet), it gets expended till it covers all the Applet, without respect whatever you can sey him with .setBounds or .setLocation . I'd just like a method from Component or JComponent that could perform the Button to avoid the BorderLayout. Thanks.

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

    Default

    You'll likely want to still use the BorderLayout, but just create a JPanel/container that uses another layout and place that JPanel in the contentPane's BorderLayout.CENTER position. Then you can add more containers or components into that JPanel. You can continue nesting JPanels/containers each using whatever layout works best of its contents to your heart's content.

  5. #5
    Josep_16's Avatar
    Josep_16 is offline Member
    Join Date
    Mar 2011
    Posts
    40
    Rep Power
    0

    Default

    Quote Originally Posted by Fubarable View Post
    You'll likely want to still use the BorderLayout, but just create a JPanel/container that uses another layout and place that JPanel in the contentPane's BorderLayout.CENTER position. Then you can add more containers or components into that JPanel. You can continue nesting JPanels/containers each using whatever layout works best of its contents to your heart's content.
    Thank you. Btw, another question that has nothing to do with the other ones. Is there any reason why I can't see the JLabels inside the JPanel (I just see the ones without anything wrote on them , (""), )? The JLabels are made like this:
    Java Code:
    JLabel labelEsfera;
    init {
    labelEsfera = new JLabel("Esfera");
    labelEsfera.setFont(fuenteTitulos);
    container3.add(labelEsfera,0);
    }
    Some other don't use a specific font, just some ones.

    Thanks.

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

    Default

    Quote Originally Posted by Josep_16 View Post
    Thank you. Btw, another question that has nothing to do with the other ones. Is there any reason why I can't see the JLabels inside the JPanel (I just see the ones without anything wrote on them , (""), )? The JLabels are made like this:
    Java Code:
    JLabel labelEsfera;
    init {
    labelEsfera = new JLabel("Esfera");
    labelEsfera.setFont(fuenteTitulos);
    container3.add(labelEsfera,0);
    }
    Some other don't use a specific font, just some ones.

    Thanks.
    No idea without the context. Since this is a new question, perhaps it would be best if you create a new question / thread here. If you do, your best bet is to create a small compilable program, an SSCCE, that demonstrates your problem, that we can all run and modify and better help you solve.

  7. #7
    nagatron's Avatar
    nagatron is offline Java Program Blog
    Join Date
    Jul 2011
    Posts
    2
    Rep Power
    0

    Default

    What I am focus here in your problem is that you can't see anything on the screen related to your JPanels. You can't really see a JPanel unless you put a border on it. . To add border on your JPanel so you can see it, here is the code:

    Java Code:
    //Constructing JPanel  
            JPanel panel = new JPanel();  
            panel.setLayout(new GridLayout(3,1)); //Setting layout on JPanel  with 3 rows and 1 column
            panel.setBorder(BorderFactory.createTitledBorder("Select an Image")); //Create a titled border on JPanel

  8. #8
    Josep_16's Avatar
    Josep_16 is offline Member
    Join Date
    Mar 2011
    Posts
    40
    Rep Power
    0

    Default

    Quote Originally Posted by nagatron View Post
    What I am focus here in your problem is that you can't see anything on the screen related to your JPanels. You can't really see a JPanel unless you put a border on it. . To add border on your JPanel so you can see it, here is the code:

    Java Code:
    //Constructing JPanel  
            JPanel panel = new JPanel();  
            panel.setLayout(new GridLayout(3,1)); //Setting layout on JPanel  with 3 rows and 1 column
            panel.setBorder(BorderFactory.createTitledBorder("Select an Image")); //Create a titled border on JPanel
    Thanks for the help, but the answer on in the deleted thread was correct. This problem happens basically when you add components to the applet or the component, when it was alredy visible (painted). So, the solution is just to add the components at the start of the init() method, nowhere else.

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

    Default

    Quote Originally Posted by Josep_16 View Post
    Thanks for the help, but the answer on in the deleted thread was correct. This problem happens basically when you add components to the applet or the component, when it was alredy visible (painted). So, the solution is just to add the components at the start of the init() method, nowhere else.
    Sooner or later, you will eventually have to add components onto a container while the program is running. When you do that, you will need to call revalidate() and repaint() on the container after adding the component.

  10. #10
    Josep_16's Avatar
    Josep_16 is offline Member
    Join Date
    Mar 2011
    Posts
    40
    Rep Power
    0

    Default

    For god sake, it was as simple as change the JPanels for Panels. Don't know where's the difference, but the problem is solved...

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

    Default

    Quote Originally Posted by Josep_16 View Post
    For god sake, it was as simple as change the JPanels for Panels. Don't know where's the difference, but the problem is solved...
    No, if this is a Swing application, this doesn't sound right. Can you show more of your code so we can take a look at it?

    Much Luck!

  12. #12
    Josep_16's Avatar
    Josep_16 is offline Member
    Join Date
    Mar 2011
    Posts
    40
    Rep Power
    0

    Default

    Quote Originally Posted by Fubarable View Post
    No, if this is a Swing application, this doesn't sound right. Can you show more of your code so we can take a look at it?

    Much Luck!
    I'll pass you the example that I used to solve it, that simply represents the problem in a shorter way; it adds some swing components inside containers and then paints something.

    With this code I have problems with swing components (using JPanels):
    Java Code:
    /**
     * Applet Prueba
     *
     * <APPLET CODE="Prueba.class" WIDTH="500" HEIGHT="500"></APPLET>
     */
    
    import java.applet.*;
    import java.awt.*;
    import javax.swing.*;
    import javax.swing.JLabel;
    import javax.swing.JPanel;
    
    public class Prueba extends JApplet{
    
      JButton button;
      JButton button2;
      JButton button3;
      JPanel panel;
      JPanel panel2;
      JPanel panel3;
      JComboBox combo;
    
      public void init() {
    	setLayout(null);
    	button = new JButton("HelloWorld");
    	button2 = new JButton("HolaMundo");
    	button3 = new JButton("HolaMundo");
    	String[] fluidoArray = {"Aire", "Aigua", "Glicerina"};
    	combo = new JComboBox(fluidoArray);
    	panel = new JPanel(new GridLayout(1,2,5,5));
    	panel2 = new JPanel(new GridLayout(2,1));
    	panel3 = new JPanel(new GridLayout(2,1));
    	
    	add(panel);
    	panel.setBounds(100, 100, 100, 100);
    	panel.add(panel2,0);
    	panel.add(panel3,1);
    	panel2.add(button3,0);
    	panel2.add(button2,1);
    	panel3.add(button,0);
    	panel3.add(combo,1);
    	
      }
    
      public void paint(Graphics g) {
    	g.drawString("Hello", 50, 30);
      }
    
    
    }
    With this one, the problem is solved (with Panels):
    Java Code:
    /**
     * Applet Prueba
     *
     * <APPLET CODE="Prueba.class" WIDTH="500" HEIGHT="500"></APPLET>
     */
    
    import java.applet.*;
    import java.awt.*;
    import javax.swing.*;
    import javax.swing.JLabel;
    import javax.swing.JPanel;
    
    public class Prueba extends JApplet{
    
      JButton button;
      JButton button2;
      JButton button3;
      Panel panel;
      Panel panel2;
      Panel panel3;
      JComboBox combo;
    
      public void init() {
    	setLayout(null);
    	button = new JButton("HelloWorld");
    	button2 = new JButton("HolaMundo");
    	button3 = new JButton("HolaMundo");
    	String[] fluidoArray = {"Aire", "Aigua", "Glicerina"};
    	combo = new JComboBox(fluidoArray);
    	panel = new Panel(new GridLayout(1,2,5,5));
    	panel2 = new Panel(new GridLayout(2,1));
    	panel3 = new Panel(new GridLayout(2,1));
    	
    	add(panel);
    	panel.setBounds(100, 100, 100, 100);
    	panel.add(panel2,0);
    	panel.add(panel3,1);
    	panel2.add(button3,0);
    	panel2.add(button2,1);
    	panel3.add(button,0);
    	panel3.add(combo,1);
    	
      }
    
      public void paint(Graphics g) {
    	g.drawString("Hello", 50, 30);
      }
    
    
    }

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

    Default

    No, this is a Swing application, and you most definitely do not want to use Panel in place of JPanel. What behavior is the second bit of code showing that the first bit is not? And again, why are you still using null layout when this is likely contributing to your problem (as noted in my first post above)?

    Edit: Also consider uploading an image of just what you want your GUI to look like.
    Last edited by Fubarable; 08-21-2011 at 05:50 PM.

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

    Default

    Edit: one major problem I see is that you're drawing directly on the JApplet and that won't work. Better to draw in a paintComponent method of a JPanel and then add that JPanel to the applet.

    For example:

    Java Code:
    import java.awt.*;
    import javax.swing.*;
    
    public class Prueba extends JApplet {
    
       JButton button;
       JButton button2;
       JButton button3;
       JPanel panel;
       JPanel panel2;
       JPanel panel3;
       JComboBox combo;
    
       public void init() {
          JPanel mainPanel = new JPanel() {
             @Override
             protected void paintComponent(Graphics g) {
                super.paintComponent(g);
                g.drawString("Hello", 50, 30);
             }
          };
          
          getContentPane().add(mainPanel);
          
          button = new JButton("HelloWorld");
          button2 = new JButton("HolaMundo");
          button3 = new JButton("HolaMundo");
          String[] fluidoArray = {"Aire", "Aigua", "Glicerina"};
          combo = new JComboBox(fluidoArray);
          panel = new JPanel(new GridLayout(1, 2, 5, 5));
          panel2 = new JPanel(new GridLayout(2, 1));
          panel3 = new JPanel(new GridLayout(2, 1));
    
          panel.add(panel2, 0);
          panel.add(panel3, 1);
          panel2.add(button3, 0);
          panel2.add(button2, 1);
          panel3.add(button, 0);
          panel3.add(combo, 1);
    
          mainPanel.setLayout(new BorderLayout());
          
          JPanel bottomPanel = new JPanel();
          bottomPanel.setLayout(new BoxLayout(bottomPanel, BoxLayout.LINE_AXIS));
          bottomPanel.add(Box.createHorizontalGlue());
          bottomPanel.add(panel);
          
          mainPanel.add(bottomPanel, BorderLayout.SOUTH);
       }
    
    }

  15. #15
    Josep_16's Avatar
    Josep_16 is offline Member
    Join Date
    Mar 2011
    Posts
    40
    Rep Power
    0

    Default

    Quote Originally Posted by Fubarable View Post
    Edit: one major problem I see is that you're drawing directly on the JApplet and that won't work. Better to draw in a paintComponent method of a JPanel and then add that JPanel to the applet.

    For example:

    Java Code:
    import java.awt.*;
    import javax.swing.*;
    
    public class Prueba extends JApplet {
    
       JButton button;
       JButton button2;
       JButton button3;
       JPanel panel;
       JPanel panel2;
       JPanel panel3;
       JComboBox combo;
    
       public void init() {
          JPanel mainPanel = new JPanel() {
             @Override
             protected void paintComponent(Graphics g) {
                super.paintComponent(g);
                g.drawString("Hello", 50, 30);
             }
          };
          
          getContentPane().add(mainPanel);
          
          button = new JButton("HelloWorld");
          button2 = new JButton("HolaMundo");
          button3 = new JButton("HolaMundo");
          String[] fluidoArray = {"Aire", "Aigua", "Glicerina"};
          combo = new JComboBox(fluidoArray);
          panel = new JPanel(new GridLayout(1, 2, 5, 5));
          panel2 = new JPanel(new GridLayout(2, 1));
          panel3 = new JPanel(new GridLayout(2, 1));
    
          panel.add(panel2, 0);
          panel.add(panel3, 1);
          panel2.add(button3, 0);
          panel2.add(button2, 1);
          panel3.add(button, 0);
          panel3.add(combo, 1);
    
          mainPanel.setLayout(new BorderLayout());
          
          JPanel bottomPanel = new JPanel();
          bottomPanel.setLayout(new BoxLayout(bottomPanel, BoxLayout.LINE_AXIS));
          bottomPanel.add(Box.createHorizontalGlue());
          bottomPanel.add(panel);
          
          mainPanel.add(bottomPanel, BorderLayout.SOUTH);
       }
    
    }
    THank you very much!

Similar Threads

  1. transparant containers
    By Dipke in forum New To Java
    Replies: 5
    Last Post: 03-27-2011, 03:41 PM
  2. Replies: 2
    Last Post: 12-19-2010, 03:24 PM
  3. Handle Resizing of Swing Containers
    By simlei in forum AWT / Swing
    Replies: 5
    Last Post: 02-23-2010, 03:20 AM
  4. First Java-game: Containers
    By eastviolence in forum New To Java
    Replies: 0
    Last Post: 04-04-2008, 07:09 PM
  5. Swing containers
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 12-07-2007, 01:06 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
  •