Paul
Paul

Reputation: 1381

Java Swing BorderLayout resize difficulties

I want to have my screen split in two so I used a BorderLayout with East and West sections. I had problems resizing and here I eventually found out that width is not changed in the East and West panels and height is not changed in the North and South panels and both are changed in the Center panel.

However, I want both width and height to be changed upon resize, and have two panels side by side. I have tried various levels of nesting to try getting it to work but I do not think it will work with BorderLayout.

It seems like this should be easy for the default layout manager but maybe I should try a different layout (e.g. BoxLayout) to achieve what I want.

Also here is some code which replicates the problem I am talking about (try resizing the window):

import java.awt.BorderLayout;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;

public class Main extends JFrame {

    public static void main(String[] args) {
        JFrame window = new Main();
        window.setVisible(true);
    }

    public Main() {
        JButton east = new JButton("East");
        JButton west = new JButton("West");

        JPanel content = new JPanel();
        content.setLayout(new BorderLayout());

        content.add(east, BorderLayout.EAST);
        content.add(west, BorderLayout.WEST);

        setContentPane(content);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        pack();
    }

}

Edit: I do not want the two sides to be equal, roughly 2:1 is the ratio which I want.

Upvotes: 5

Views: 23668

Answers (4)

RobB
RobB

Reputation: 29

Sorry about replying to an old post. My fix is to still use BorderLayout but to throw in the following line after the Component is resized

getLayout().layoutContainer(this);

Upvotes: 0

David Faitelson
David Faitelson

Reputation: 211

If you want to keep your BorderLayout you can use something like the following object:

public class ResizablePanel extends JPanel {

  public ResizablePanel(JComponent body) {

    setLayout(new BorderLayout());
    JButton resize = new JButton();
    resize.setPreferredSize(new Dimension(Integer.MAX_VALUE, 4));
    resize.addMouseMotionListener(new MouseAdapter() {
        public void mouseDragged(MouseEvent e) {
            Dimension preferredSize = ResizablePanel.this.getPreferredSize();
            ResizablePanel.this.setPreferredSize(new Dimension(preferredSize.width, preferredSize.height-e.getY()));
            ResizablePanel.this.revalidate();
        }
    });             
    add(resize, BorderLayout.PAGE_START);
    add(body, BorderLayout.CENTER);
  }
}

Now wrap the part you want to resize with an instance of ResizablePanel and you'll be able to resize it by dragging the thin button.

Note that this is code is for resizing the height of a panel that you put at the bottom (PAGE_END) part of a border layout, but it should be fairly straightforward to change it for resizing the width.

Upvotes: 1

Branislav Lazic
Branislav Lazic

Reputation: 14806

Why don't you try with JSplitPane:

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

public class AppDemo {

    public static void main(String[] args) {

        SwingUtilities.invokeLater(() -> {
            JFrame frame = new JFrame();
            JButton eastButton = new JButton("East");
            JButton westButton = new JButton("West");
            JSplitPane splitPane = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT, eastButton, westButton);

            JPanel content = new JPanel();
            content.setLayout(new BorderLayout());
            content.add(splitPane, BorderLayout.CENTER);

            frame.setContentPane(content);
            frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
            frame.setPreferredSize(new Dimension(500, 400));
            frame.pack();
            frame.setVisible(true);
        });

    }
}

You will get this:

enter image description here

Upvotes: 8

nIcE cOw
nIcE cOw

Reputation: 24616

What you can use in your case is GridLayout, here two JButtons will resize themselves as the JFrame resizes.

import java.awt.GridLayout;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;

public class Main extends JFrame {

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable()
        {
            @Override
            public void run()
            {
                JFrame window = new Main();
                window.setVisible(true);
            }
        });        
    }

    public Main() {
        JButton east = new JButton("East");
        JButton west = new JButton("West");

        JPanel content = new JPanel();
        content.setLayout(new GridLayout(1, 2));

        content.add(east);
        content.add(west);

        setContentPane(content);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        pack();
    }

}

Moreover, it's always best to run your GUI related code from the EDT - Event Dispatch Thread, and not from the Main Thread. Do read Concurrency in Swing, for more info on the topic.

LATEST EDIT : As per requested comment

Use GridBagLayout to specify the size that you want to give

import java.awt.Color;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;

public class Main extends JFrame {

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable()
        {
            @Override
            public void run()
            {
                JFrame window = new Main();
                window.setVisible(true);
            }
        });        
    }

    public Main() {
        JPanel east = new JPanel();
        east.setOpaque(true);
        east.setBackground(Color.WHITE);
        JPanel west = new JPanel();
        west.setOpaque(true);
        west.setBackground(Color.BLUE);

        JPanel content = new JPanel();
        content.setLayout(new GridBagLayout());

        GridBagConstraints gbc = new GridBagConstraints();
        gbc.anchor = GridBagConstraints.FIRST_LINE_START;
        gbc.fill = GridBagConstraints.BOTH;
        gbc.weightx = 0.3;
        gbc.weighty = 1.0;
        gbc.gridx = 0;
        gbc.gridy = 0;

        content.add(east, gbc);
        gbc.weightx = 0.7;
        gbc.gridx = 1;
        content.add(west, gbc);

        setContentPane(content);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        pack();
    }

}

Upvotes: 8

Related Questions