0

So, I'm starting with the basic visual Java components, and have trouble to use them accurately.

Here is my new problem: I'm trying to implement a 3 parts panel: east, center, west, but can't have a proper display of the center panel. Here is my piece of code:

Basically 'panelUpMiddle' isn't visible, so I wonder why?...

public class TestCode_Web {


public static void main(String[] args) {

    JFrame window = new JFrame("Test");
    window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    window.setSize(400, 400);

    JPanel innerPanel = new JPanel(new BorderLayout());     
    JPanel panelUp = new JPanel(new BorderLayout());

    JPanel panelUpLeft = new JPanel();
    JPanel panelUpMiddle = new JPanel();

    window.add(innerPanel, BorderLayout.NORTH);     
    innerPanel.add(panelUp, BorderLayout.NORTH);

    panelUp.add(panelUpLeft, BorderLayout.WEST);
    panelUp.add(panelUpMiddle, BorderLayout.CENTER);

    JLabel label1 = new JLabel("Label 1");

    JLabel label11 = new JLabel("Label 11");
    JLabel label12 = new JLabel("Label 12");

    panelUp.add(label1);
    panelUpLeft.add(label11);       
    panelUpMiddle.add(label12);     

    panelUp.setBackground(new Color(200, 240, 200));

    panelUpLeft.setBackground(new Color(200, 240, 0));
    panelUpMiddle.setBackground(new Color(100, 240, 200));

    panelUp.setPreferredSize(new Dimension(window.getWidth(), 160));        
    panelUpLeft.setPreferredSize(new Dimension(160, 120));

    window.setVisible(true);
}

}
  • 3
    See [*Should I avoid the use of set(Preferred|Maximum|Minimum)Size methods in Java Swing*](http://stackoverflow.com/q/7229226/230513) and [*Initial Threads*](http://docs.oracle.com/javase/tutorial/uiswing/concurrency/initial.html). – trashgod Jun 18 '13 at 09:25
  • 2
    When you wrote this `panelUp.add(label1);`, it simply replaced the previously added component at the CENTER position i.e.`panelUp.add(panelUpMiddle, BorderLayout.CENTER);`, hence you won't be able to see `label12`/`panelUpMiddle`. Since only one component can be added to any location at any given instance of time. Have a relook at your code – nIcE cOw Jun 18 '13 at 15:22
  • @nIcE cOw: that's exactly the problem, I should have paid better attention to that point! Thanx a lot, but I can't consider that question answered as you didn't open a new _post answer_. – Matei Focseneanu Jun 19 '13 at 07:08
  • @MateiFocseneanu : You're MOST WELCOME and KEEP SMILING :-). Well you can mark Mackie's answer as being appropriate, as the same has been provided by `him/her`. We are all here to share the knowledge with each other and do pay genuine attention to what `trashgod` has to say on using the `setXxX()` methods. VERY GOOD points in that post related to this approach :-) – nIcE cOw Jun 19 '13 at 07:22

3 Answers3

3

Try this code. I updated the naming of the panels and the labels so it is more clear.

    JFrame window = new JFrame("Test");
    window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    window.setSize(400, 400);

    JPanel centerPanel = new JPanel(new BorderLayout());     
    JPanel eastPanel = new JPanel(new BorderLayout());
    JPanel westPanel = new JPanel(new BorderLayout());

    window.add(centerPanel, BorderLayout.CENTER);     
    window.add(eastPanel, BorderLayout.EAST);
    window.add(westPanel, BorderLayout.WEST);

    JLabel centerLabel = new JLabel("Center");
    JLabel eastLabel = new JLabel("East");
    JLabel westLabel = new JLabel("West");

    eastPanel.add(eastLabel);
    westPanel.add(westLabel);       
    centerPanel.add(centerLabel);

    centerPanel.setPreferredSize(new Dimension(200, 400));
    eastPanel.setPreferredSize(new Dimension(100, 400));
    westPanel.setPreferredSize(new Dimension(100, 400));
    eastPanel.setBackground(new Color(200, 240, 200));
    westPanel.setBackground(new Color(200, 240, 0));

    window.setVisible(true);
darijan
  • 9,725
  • 25
  • 38
2

A third panel is never created and nested within the other panel.

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;

import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;

public class TestCode_Web {


public static void main(String[] args) {

    JFrame window = new JFrame("Test");
    window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    window.setSize(400, 400);

    JPanel innerPanel = new JPanel(new BorderLayout());     
    JPanel panelUp = new JPanel(new BorderLayout());

    JPanel panelUpLeft = new JPanel();
    JPanel panelUpMiddle = new JPanel();
    JPanel panelUpRight = new JPanel();  //Create third panel

    window.add(innerPanel, BorderLayout.NORTH);     
    innerPanel.add(panelUp, BorderLayout.NORTH);

    panelUp.add(panelUpRight, BorderLayout.EAST); //Add Third Panel
    panelUp.add(panelUpLeft, BorderLayout.WEST);
    panelUp.add(panelUpMiddle, BorderLayout.CENTER);

    JLabel label1 = new JLabel("Label 1");

    JLabel label11 = new JLabel("Label 11");
    JLabel label12 = new JLabel("Label 12");

    panelUpRight.add(label1); //Add label for third panel
    panelUpLeft.add(label11);       
    panelUpMiddle.add(label12);     

    panelUp.setBackground(new Color(200, 240, 200));

    panelUpLeft.setBackground(new Color(200, 240, 0));
    panelUpMiddle.setBackground(new Color(100, 240, 200));

    panelUp.setPreferredSize(new Dimension(window.getWidth(), 160));        
    panelUpLeft.setPreferredSize(new Dimension(160, 120));

    window.setVisible(true);
}

}
Kevin Bowersox
  • 93,289
  • 19
  • 159
  • 189
  • Well it doesn't work. Apart from the fact it's not what I asked (I mean in this case I don't need another instance of JPanel, it just doesn't work as I still don't see any 'panelUpMiddle' (or 'label12' in the executble app.) – Matei Focseneanu Jun 18 '13 at 10:29
1

Actually, you don't need the innerPanel:

public class TestCode_Web
{
    public static void main(String[] args) {

    JFrame window = new JFrame("Test");
    window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    window.setLayout(new BorderLayout());
    window.setSize(400, 400);

    JPanel panelUp = new JPanel();
    JPanel panelUpLeft = new JPanel();
    JPanel panelUpMiddle = new JPanel();

    window.add(panelUp, BorderLayout.NORTH);     
    window.add(panelUpLeft, BorderLayout.WEST);
    window.add(panelUpMiddle, BorderLayout.CENTER);

    JLabel label1 = new JLabel("Label 1");
    JLabel label11 = new JLabel("Label 11");
    JLabel label12 = new JLabel("Label 12");

    panelUp.add(label1);
    panelUpLeft.add(label11);       
    panelUpMiddle.add(label12);     

    panelUp.setBackground(new Color(200, 240, 200));
    panelUpLeft.setBackground(new Color(200, 240, 0));
    panelUpMiddle.setBackground(new Color(100, 240, 200));

    panelUp.setPreferredSize(new Dimension(window.getWidth(), 160));        
    panelUpLeft.setPreferredSize(new Dimension(160, 120));

    window.setVisible(true);
}
}

NiceCow gave the answer why yours is not working. This should work fine.

Mackie
  • 186
  • 9