1

I am trying to make the interface of a program using Java Swing. I have a container where I add 3 JPanels with some components. The problem is that when I expand the frame all those 3 JPanels come on the first row one next to another.

Here is my code:

import java.awt.Color;
import java.awt.Container;
import java.awt.FlowLayout;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.SpringLayout;
import javax.swing.border.LineBorder;

public class GraphicRefact extends JFrame {
    private Container container;

    private JButton button2;
    private JButton button1;
    private JTextField textField01;
    private JLabel label01;

    private JButton button03;
    private JButton button04;
    private JTextField textField03;
    private JLabel label03;

    private JButton button02;
    private JButton button01;
    private JTextField textField02;
    private JLabel label02;

    public static void main(String[] args) {
        new GraphicRefact();
    }

    public GraphicRefact() {
        initializeComponents();

        setTitle("Title");
        setSize(500, 150);
        setVisible(true);
        setDefaultCloseOperation(EXIT_ON_CLOSE);

        container = new JPanel();


        JPanel panel01 = new JPanel();

        panel01.add(label01);
        panel01.add(textField01);
        panel01.add(button2);
        panel01.add(button1);
        panel01.setBorder(new LineBorder(Color.BLACK, 3));
        container.add(panel01);



        JPanel panel02 = new JPanel();

        panel02.add(label02);
        panel02.add(textField02);
        panel02.add(button02);
        panel02.add(button01);
        container.add(panel02);

        JPanel panel03 = new JPanel();

        panel03.add(label03);
        panel03.add(textField03);
        panel03.add(button03);
        panel03.add(button04);
        container.add(panel03);

        add(container);

    }

    private void initializeComponents() {

        button1 = new JButton("Open");
        button2 = new JButton("Close");
        textField01 = new JTextField("Choose the path...");
        label01 = new JLabel("Choose File: ");

        button01 = new JButton("Button01");
        button02 = new JButton("Button02");
        textField02 = new JTextField("Choose the path...");
        label02 = new JLabel("Choose Dir:");

        button03 = new JButton("Button03");
        button04 = new JButton("Button03");
        textField03 = new JTextField("Choose the path...");
        label03 = new JLabel("Choose Dir:");
    }

}

Here is how the program looks before and after I expand the frame.

Before:

enter image description here

After:

enter image description here

So, even after I expand the frame, I want the program to leave those 3 JPanels on the middle of cotainer.

Thank you!

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
John R.
  • 420
  • 4
  • 14
  • 1
    Try something like .. `container = new JPanel(new GridLayout(0,1));` – Andrew Thompson Dec 27 '18 at 14:16
  • 1
    Don't use JPanel's default FlowLayout but rather nest JPanels each using a more powerful layout. You can find the layout manager tutorial 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 27 '18 at 14:17

4 Answers4

1

You have to use a proper LayoutManager. Look at the examples there. BoxLayout seems to be what you want.

Dorian Gray
  • 2,913
  • 1
  • 9
  • 25
1

You can set the gridLayout and put those elements inside.

JPanel jp = new JPanel();
GridLayout gl = new GridLayout(3,4); //3 rows, 4 columns
jp.setLayout(gl);

After you do this, just put your elements inside layout, by order.

jp.add(label1);
jp.add(button1);
//etc...
SwissCodeMen
  • 4,222
  • 8
  • 24
  • 34
0

I can't see where you have defined any positions for anything, if you need to read how that's done you can start here

Positioning a JPanel in a JFrame at specific position

wraig
  • 71
  • 4
0

The inbuilt layout manger of java swing are not that useful. You should use this

frame.setLayout(null);
component.setLocation(x, y);
frame.add(component);
  • 4
    Hi there, welcome to SO :) Please note absolute components positioning is actually not recommended. As already mentioned, Swing is designed to be used with [LayoutManagers](https://docs.oracle.com/javase/tutorial/uiswing/layout/index.html) which can manage components sizing and positioning in a gracefull way and independently of screen's size and resolution. – dic19 Dec 27 '18 at 14:46