Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8549355
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T13:41:54+00:00 2026-06-11T13:41:54+00:00

I want to have my screen split in two so I used a BorderLayout

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-11T13:41:55+00:00Added an answer on June 11, 2026 at 1:41 pm

    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();
        }
    
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Imagine a full Android device screen, I want it split in to two sections:
I want to have two Emacs windows on the screen: one for Dired and
As according to my project requirement, I want to split the screen into two
i have a screen and it is i want the buttons to put under
I just want to have my anchor in the middle of the screen horizontally,
In my WebView app I want to have a custom onReceivedError screen. Instead I
Screen is amazing, of course, but I don't want to have to think about
I have a few full screen png's where I want to create regions on
I have an iphone app i want that befor moving to next screen it
I have a DIV that I want to touch the bottom of the screen

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.