Angelo
Angelo

Reputation: 65

How to resize a JPanel

I am trying to resize the JPanels but there is a space under it . Here is a link to show :

enter image description here

  And this is the code :


import java.awt.*;
import javax.swing.*;

public class Ex1 extends JFrame{
private JTextArea textarea = new JTextArea ();
private JTextField field = new JTextField ();``
private JButton buton = new JButton ("Trimite");

public Ex1(){
    JPanel panel = new JPanel (new BorderLayout(2,2));
    JPanel panel1 = new JPanel (new BorderLayout(2,2));
    JPanel panel2 = new JPanel (new BorderLayout(2,2));
    JLabel label1 = new JLabel ("Mesaje");
    JLabel label2 = new JLabel ("Scrieti un mesaj");
    panel1.setPreferredSize(new Dimension(350,100));
    panel2.setPreferredSize(new Dimension(350,25));
    panel1.add(label1, BorderLayout.NORTH);
    panel1.add(textarea, BorderLayout.CENTER);
    panel2.add(label2, BorderLayout.WEST);
    panel2.add(field, BorderLayout.CENTER);
    panel2.add(buton, BorderLayout.EAST);
    setLayout(new GridLayout(2,1,1,1));
    panel.add(panel1, BorderLayout.NORTH);
    panel.add(panel2, BorderLayout.CENTER);
    add(panel);

}

public static void main(String[] args) {
    JFrame frame = new Ex1();
    frame.pack();
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setLocationRelativeTo(null);
    frame.setVisible(true);
}

}

Upvotes: 1

Views: 23669

Answers (2)

tenorsax
tenorsax

Reputation: 21233

You are setting a layout for a frame to GridLayout in which all components are given equal size. You have two rows, add(panel) adds the panel to the first row of the grid. The second row is left empty. See How to Use GridLayout.

Comment out setLayout(new GridLayout(2,1,1,1)); and the extra space should go away. When you comment this line the layout of frame's content pane will be BorderLayout. The default layout of the JFrame is BorderLayout. So add(panel); will add the panel to the center of the frame's content pane. As a result the panel should occupy all the available space.

As a side note, avoid setPreferredSize(), usually it is not necessary, see Should I avoid the use of set(Preferred|Maximum|Minimum)Size methods in Java Swing for details.

You can specify the number of rows and columns for a text area and wrap it in the scroll pane, ie:

textArea = new JTextArea(5, 20);
JScrollPane scrollPane = new JScrollPane(textArea);

For more details see How to Use Text Areas

EDIT: example of getPreferredSize()

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

import javax.swing.*;

public class Ex1 extends JPanel{
    private JTextArea textarea = new JTextArea ();
    private JTextField field = new JTextField ();
    private JButton buton = new JButton ("Trimite");

    public Ex1() {
        setLayout(new BorderLayout());

        JPanel panel1 = new JPanel (new BorderLayout(2,2));
        JPanel panel2 = new JPanel (new BorderLayout(2,2));

        JLabel label1 = new JLabel ("Mesaje");
        JLabel label2 = new JLabel ("Scrieti un mesaj");

        panel1.add(label1, BorderLayout.NORTH);
        panel1.add(new JScrollPane(textarea), BorderLayout.CENTER);

        panel2.add(label2, BorderLayout.WEST);
        panel2.add(field, BorderLayout.CENTER);
        panel2.add(buton, BorderLayout.EAST);

        add(panel1, BorderLayout.CENTER);
        add(panel2, BorderLayout.SOUTH);
    }

    @Override
    public Dimension getPreferredSize() {
        return new Dimension(350, 300);
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {   
            public void run() {   
                JFrame frame = new JFrame("Test");

                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.setLocationByPlatform(true);

                Ex1 panel = new Ex1();

                frame.add(panel);
                frame.pack();
                frame.setVisible(true);
            }
        });
    }
}

Upvotes: 4

brso05
brso05

Reputation: 13232

You need to resize the JFrame not the JPanel. Try:

this.setPreferredSize(new Dimension(350, 25);// in Ex1

Or in your main method:

frame.setPreferredSize(new Dimension(350, 25);

Upvotes: 0

Related Questions