1

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);
}

}

BackSlash
  • 21,927
  • 22
  • 96
  • 136
Angelo
  • 65
  • 1
  • 1
  • 10
  • that is the frame not the panel. resize the frame – Saher Ahwal Dec 08 '14 at 18:12
  • if you have enough time and have patience...the one approach is you use absolute layout(to very small project...) ...it has more time of you...but you can arrange every component at absolute position and size that you want..! :-) – mehdi Dec 08 '14 at 19:00

2 Answers2

4

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);
            }
        });
    }
}
Community
  • 1
  • 1
tenorsax
  • 21,123
  • 9
  • 60
  • 107
  • One more question : if I want the frame's size to be 350 x 300 in only shows the first panel :-( – Angelo Dec 08 '14 at 19:03
  • @Angelo how do you set 350x300 size? – tenorsax Dec 08 '14 at 19:05
  • Yes . In main , I want " frame.setSize (350,300) – Angelo Dec 08 '14 at 19:19
  • @Angelo I cannot really reproduce the case when only one panel is shown. In any case, specify the desired number of rows and columns for text components. Also override `getPreferredSize()` for the `panel` (the one that is added to the frame's content) to return (350,300). Then `pack()` the frame. You may also want to put `panel1` in the `CENTER` and `panel2` in `SOUTH` so the text area occupies more space. – tenorsax Dec 08 '14 at 19:30
  • I dont really understand how is that done :-?? I just want the frame's size to be 350 x 300 – Angelo Dec 08 '14 at 20:47
  • @Angelo I added an example of `getPreferredSize()` in the last edit. It is just a slightly modified version of your code. – tenorsax Dec 08 '14 at 23:05
  • Thanks . Thats great , but why didnt it worked before placing panel1 in center and panel2 south like you said ? As I wrote first time ( panel1 north and panel2 center) , it only showed the first panel – Angelo Dec 08 '14 at 23:17
  • No problem :) As for the missing panel, I am not sure, may be there are other changes in your code :) – tenorsax Dec 08 '14 at 23:25
  • Whats this doing ? " SwingUtilities.invokeLater(new Runnable() { public void run() " – Angelo Dec 09 '14 at 12:00
  • @Angelo see [Initial Threads](https://docs.oracle.com/javase/tutorial/uiswing/concurrency/initial.html) for details. – tenorsax Dec 09 '14 at 19:55
0

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);
brso05
  • 13,142
  • 2
  • 21
  • 40