0

I want to make the frame resizable but keep the initial size of components in a GridLayout. How do I do this?

Initial Size. This is the size I want to keep:

enter image description here

When the frame is resized, the size of the components changes. I don't want this:

enter image description here

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
David
  • 13
  • 3
  • 2
    Put the container inside a second container, use a `GridBagLayout` to layout the second container onto the parent, without constraints. `GridBagLayout` will continue to layout the components to their preferred size and centre the second container within the parent when more space is avaliable – MadProgrammer Apr 14 '19 at 01:29
  • 1
    Alternatively, this [example](https://stackoverflow.com/a/8504753/230513) uses `GroupLayout` to get useful resizing. – trashgod Apr 14 '19 at 01:35
  • I have to use GridLayout because it's a requirement – David Apr 14 '19 at 01:50
  • @MadProgrammer Could you show me an example? I don't understand – David Apr 14 '19 at 01:52
  • @David Create a new JPanel, set its layout as a GridBagLayoit, add your existing panel to it, then add the new panel to your parent container – MadProgrammer Apr 14 '19 at 02:40
  • JPanel panel1 = new JPanel(); frame.getContentPane().add(panel1); panel1.setLayout(new GridLayout(2, 2)); JPanel panel2 = new JPanel(); panel2.add(panel1); frame.getContentPane().add(panel2); – David Apr 14 '19 at 03:03
  • `frame.getContentPane().add(panel2);` No. If following the suggestion of @MadProgrammer, that should be `panel1.add(panel2);` – Andrew Thompson Apr 14 '19 at 03:55
  • 1
    "Could you show me an example?" - post an [mcve]. This makes helping much easier. – c0der Apr 14 '19 at 05:06

1 Answers1

1

The following is an mcve that demonstrates a solution using GridBagLayout as proposed by MadProgrammer.
Including such mcve in the question makes helping much easier and your chances to get good answers higher.

import java.awt.GridBagLayout;
import java.awt.GridLayout;
import javax.swing.BorderFactory;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.border.Border;

public class SwingMain {

    public static void main(String[] args) {

        JFrame frame = new JFrame();
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setLocationRelativeTo(null);
        frame.setLayout(new GridBagLayout()); //place desired content in a GridbagLayout
        frame.add(new TestPane());
        frame.pack();
        frame.setVisible(true);
    }
}

class TestPane extends JPanel{

    TestPane() {

        Border padding = BorderFactory.createEmptyBorder(10, 10, 10, 10);
        setBorder(padding);
        setLayout(new GridLayout(2, 2, 10, 10));
        add(new JLabel("Celsius"));
        add(new JTextField(10));
        add(new JLabel("Fahrenheit"));
        add(new JTextField(10));
    }
}

enter image description here

c0der
  • 18,467
  • 6
  • 33
  • 65