0

I am wondering why I am not able to see the topPanel in my controlPanel

Here is my code:

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.FlowLayout;
import java.awt.GridLayout;

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

public class GUI {

private JFrame frame;
private JTextArea textArea;
private JPanel topPanel;
private JPanel controlPanel;
private JLabel topLabel;

void createScreen() {

    frame = new JFrame("Hello");
    frame.setSize(600,600);
    frame.setLayout(new GridLayout(3,1)); 
    controlPanel = new JPanel();
    controlPanel.setLayout(new FlowLayout());
    controlPanel.setBackground(Color.GREEN);
    topLabel = new JLabel("WELCOME TO MY TRAINING", JLabel.CENTER);

    frame.add(topLabel);
    frame.add(controlPanel);

    topPanel = new JPanel();

    BorderLayout borderLayout = new BorderLayout();

    borderLayout.setHgap(10);
    borderLayout.setVgap(10);

    topPanel.setLayout(borderLayout);
    topPanel.setBackground(Color.BLUE);
    topPanel.setSize(75,300);
    textArea = new JTextArea();
    textArea.setSize(25, 25);

    topPanel.add(textArea, BorderLayout.CENTER);

    controlPanel.add(topPanel);

    frame.setVisible(true);

}

public static void main(String[] args) {
     GUI gui = new GUI();
     gui.createScreen();
}

}
isaace
  • 3,336
  • 1
  • 9
  • 22
  • My mistake, I thought topLabel was a panel as well. Comment deleted. – arkdevelopment Dec 26 '17 at 21:38
  • 1
    `frame.pack()`? – sprinter Dec 26 '17 at 21:40
  • It's always a good information if you analyse the Swing Layout information from debug output. Therefore, press [Ctrl]+[Shift]+[F1] in your java console, and you get the layout output. For this and other debugging hints, see https://stackoverflow.com/questions/6671021/how-to-debug-java-swing-layouts Another approach for debugging to set some fancy colors to your panels. The color allows you to see which panel is shown and which one is missing. – Christoph Bimminger Dec 26 '17 at 21:50

1 Answers1

1

FlowLayout uses components preferred size and not the actual size set to it. To fix your issue set preferred size to topPanel instead of its size.

topPanel.setPreferredSize( new Dimension(75,300) );

But my advice is to avoid setting size like this but instead let the TextArea determine the size by specifying its number of rows and columns like this:

topPanel.setLayout(borderLayout);
topPanel.setBackground(Color.BLUE);
textArea = new JTextArea(10, 15);
tsolakp
  • 5,858
  • 1
  • 22
  • 28
  • Thank you for your answer. I am still confused a bit, it seems to me that the text area is always the entire size of the topPanel. Is that because I am using flowlayout? – isaace Dec 26 '17 at 22:05
  • Yes. If you set `BorderLayout` to `controlPanel` it will take the whole `controlPanel`. – tsolakp Dec 26 '17 at 22:07
  • @isaace: you will want to read the layout manager tutorials which you can find here: [Layout Manager Tutorial](http://docs.oracle.com/javase/tutorial/uiswing/layout/index.html), and you can find links to the Swing tutorials and to other Swing resources here: [Swing Info](http://stackoverflow.com/questions/tagged/swing). – Hovercraft Full Of Eels Dec 26 '17 at 22:46