Search code examples
javajpaneljappletborder-layout

Adding a JPanel into a BorderLayout of a JApplet


I'm having trouble adding this JPanel into the Center block of the pane. Essentially, this main window is a BorderLayout that has centerPanel in the Center and the west and east blocks will be separate BorderLayouts. I've googled the problem and looked through example code from my professor and here on stackoverflow, but I can't find the problem in my code.

I do all of my coding in Eclipse, so I use the integrated AppletViewer. The only thing that comes up is an empty gray box where I expect to see the centerPanel which includes the JLabels and JTextAreas.

import java.awt.*;
import java.awt.event.*;

import javax.swing.*;

public class Lab7 extends JApplet implements ItemListener, ActionListener{

//variables
private JLabel currentOrder, total;
private JTextArea descTop, currentOrderTA, totalTA;
private JRadioButton sizeSmall, sizeMedium, sizeLarge, typeDeep, typePan, typeHand;
private JCheckBox pepperoni, bacon, extra_cheese, mushroom, pepper, sausage, tomato, olive;
private JButton orderButton, resetButton;
private ButtonGroup sizeBG, typeBG;
private BorderLayout borderLayoutMain, borderLayoutWest, borderLayoutEast;
private JPanel westPanel, centerPanel, eastPanel;

public void init(){       
    Container pane = getContentPane();
    pane.setLayout(borderLayoutMain);

    //borderLayoutMain centerPanel
    centerPanel = new JPanel();
    centerPanel.setLayout(null);

    currentOrder.setSize(200, 25);
    currentOrder.setLocation(100, 25);
    currentOrderTA.setSize(600, 400);
    currentOrderTA.setLocation(100, 50);
    currentOrderTA.setEditable(false);
    total.setSize(200, 25);
    totalTA.setLocation(100, 450);
    totalTA.setEditable(false);
    orderButton.setSize(100, 50);
    orderButton.setLocation(100, 500);
    resetButton.setSize(100, 50);
    resetButton.setLocation(400, 500);

    centerPanel.add(currentOrder);
    centerPanel.add(currentOrderTA);
    centerPanel.add(total);
    centerPanel.add(totalTA);
    centerPanel.add(orderButton);
    centerPanel.add(resetButton);

    pane.add(centerPanel, BorderLayout.CENTER);
}

Solution

  • Since you've done away with the layout manager (centerPanel.setLayout(null);) the BorderLayout no longer has any idea what the size of your panel should be (as the value for the preferred size of the container is ascertained by it's layout manager)

    Consider using a layout manager on centerPanel, see Laying Out Components Within a Container for more details.

    Avoid using null layouts, pixel perfect layouts are an illusion within modern ui design. There are too many factors which affect the individual size of components, none of which you can control. Swing was designed to work with layout managers at the core, discarding these will lead to no end of issues and problems that you will spend more and more time trying to rectify

    See Why is it frowned upon to use a null layout in SWING? for more details

    Updated...

    When you call

    pane.setLayout(borderLayoutMain);
    

    borderLayoutMain is null, so you now have a null layout at the top level container.

    Try using...

    pane.setLayout(new BorderLayout());
    

    instead...