Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java Swing Components does not have correct Size when added to JPanel

It's my first Post here, so forgive me please if i'm doing something wrong.

My Problem is:

I am trying to add Components to a JPanel with defined values for Size etc. But when i add them to the Panel, they do absolutely not have the Size and Location they should have. For example:

public class Console extends JFrame {
    private JPanel mainPanel = new JPanel();
    private JTextArea textField = new JTextArea();
    private JTextArea textField2 = new JTextArea();

    public Console() {
        this.setSize(500,300);

        this.mainPanel.setSize(this.getWidth(),this.getHeight());

        this.textField.setEditable(false);
        this.textField.setSize(this.mainPanel.getWidth(), 100);
        this.textField.setPreferredSize(new Dimension(this.mainPanel.getWidth(),this.mainPanel.getHeight()));
        this.textField.setLocation(0, 0);
        this.textField.setText("some text");
        this.textField.setVisible(true);

        this.textField2.setSize(this.mainPanel.getWidth(),200);
        this.textField2.setPreferredSize(new Dimension(this.getWidth(),this.getHeight()));
        this.textField2.setLocation(0,this.mainPanel.getHeight()-this.textField.getHeight());
        this.textField2.setText("blabla");
        this.textField2.setVisible(true);

        this.mainPanel.add(textField);
        this.mainPanel.add(textField2);
        this.mainPanel.setVisible(true);

        this.add(this.mainPanel);
        // I know you should not call setVisible() in the Constructor, just for making Code more simple here.
        this.setVisible(true);
    }
}

When i start the Application, both JTextArea's are really small and somewhere in the middle (not as set above) while the mainPanel is correct.I tried to call setSize() and setPreferredSize() in different Places in the Code, but it didn't work. I know it is better to use a LayoutManager for doing this as far as i heard but to be honest, i do not get how to use it correctly. I checked it on Oracle Doc's but i would appreciate it if someone could post a clean Solution for this, Thanks in Advance.

like image 456
Furious Gamer Avatar asked Jan 14 '13 14:01

Furious Gamer


People also ask

Can you set the size of a JPanel?

We change the background colour to red, and give the JPanel a size using setSize(width, height). We also give the widget a location using setLocation(x pixels, y pixels). This sets the top left hand corner of the widget to the specified position.

How are components added to a JPanel?

Use add() to place components in a panel. FlowLayout is the default layout manager for JPanel . Use setLayout() only if you want a different layout manager.

How do I change the size of a JFrame in Java?

Change window Size of a JFrame To resize a frame, There is a method JFrame. setSize(int width, int height) which takes two parameters width and height.

What happens if set size is not called on a JFrame?

What happens if setSize is not called on a window? 1) The window is displayed at its preferred size.


1 Answers

You need to set a proper Layout for your Container. You setLayout for a Container like JFrame, JPanel and so on. You don't add other components to layout, but to a container. Then it would layout them accordingly. It is how it works.

With proper layout you would not need to call setLocation(). Also setVisible(true) is excessive, because true is default values for those components in your code.

Better not to extend JFrame, extend JPanel instead and add it to JFrame.

Please, learn about EDT and SwingUtilities.invoketLater() you need to use it.

Also you can save some bytes, not typing this. all the time.

like image 185
Nikolay Kuznetsov Avatar answered Sep 18 '22 19:09

Nikolay Kuznetsov