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

  • Home
  • SEARCH
  • 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 9189319
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T20:12:36+00:00 2026-06-17T20:12:36+00:00

I am trying to build a set of two labels and a button (label-button-label)

  • 0

I am trying to build a set of two labels and a button (label-button-label) that looks something like the top row of this:

enter image description here.

I am struggling to do this with Java Swing. I’ve tried BorderLayout, with BorderLayout.WEST, BorderLayout.CENTER, BorderLayout.EAST but that makes the button fill the space:
enter image description here

here’s the code I used for that:

panel = new JPanel(new BorderLayout());
l1 = new JLabel("l1");
button = new JButton("B");
l2 = new JLabel("l2");
panel.add(l1, BorderLayout.WEST);
panel.add(button, BorderLayout.CENTER);
panel.add(l2, BorderLayout.EAST);

I’ve also tried GridBagLayout, and the closest I’ve come there is to have them spaced out, but not hugging the sides:

enter image description here

Code for that:

panel = new JPanel(new GridBagLayout());
GridBagConstraints c = new GridBagConstraints();
c.fill = GridBagConstraints.VERTICAL;
l1 = new JLabel("L1");
l2 = new JLabel("L2");
button = new JButton("B");
c.weightx = Integer.MAX_VALUE;
c.gridx = 0;
panel.add(l1, c);
c.weightx = 1;
c.gridx = 1;
panel.add(button, c);
c.weightx = Integer.MAX_VALUE;
c.gridx = 2;
panel.add(l2, c);

Any ideas?

  • 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-17T20:12:37+00:00Added an answer on June 17, 2026 at 8:12 pm

    Here are two ways to doit. Once with a BorderLayout and once with a GridBagLayout:

    import java.awt.BorderLayout;
    import java.awt.FlowLayout;
    import java.awt.GridBagConstraints;
    import java.awt.GridBagLayout;
    
    import javax.swing.JButton;
    import javax.swing.JFrame;
    import javax.swing.JLabel;
    import javax.swing.JPanel;
    import javax.swing.SwingUtilities;
    
    public class TestGridBagLayout {
    
        protected void initUI1() {
            final JFrame frame = new JFrame("Grid bag layout");
            frame.setTitle(TestGridBagLayout.class.getSimpleName());
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            JLabel l1 = new JLabel("L1");
            JLabel l2 = new JLabel("L2");
            JButton b = new JButton("B");
            JPanel panel = new JPanel(new GridBagLayout());
            GridBagConstraints gbc = new GridBagConstraints();
            panel.add(l1, gbc);
            gbc.weightx = 1.0;
            gbc.anchor = GridBagConstraints.CENTER;
            panel.add(b, gbc);
            gbc.weightx = 0;
            panel.add(l2, gbc);
            frame.add(panel);
            frame.setSize(800, 100);
            frame.setVisible(true);
        }
    
        protected void initUI2() {
            final JFrame frame = new JFrame("Border layout");
            frame.setTitle(TestGridBagLayout.class.getSimpleName());
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            JPanel panel = new JPanel(new BorderLayout());
            JLabel l1 = new JLabel("L1");
            JLabel l2 = new JLabel("L2");
            JButton b = new JButton("B");
            JPanel wrappingPanel = new JPanel(new FlowLayout());
            wrappingPanel.add(b);
            panel.add(l1, BorderLayout.WEST);
            panel.add(l2, BorderLayout.EAST);
            panel.add(wrappingPanel);
            frame.add(panel);
            frame.setLocation(0, 125);
            frame.setSize(800, 100);
            frame.setVisible(true);
        }
    
        public static void main(String[] args) {
            SwingUtilities.invokeLater(new Runnable() {
                @Override
                public void run() {
                    TestGridBagLayout test = new TestGridBagLayout();
                    test.initUI1();
                    test.initUI2();
                }
            });
        }
    
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to build something that will randomly pick two objects from a list
I am trying to build set of reusable components for the ASP.Net MVC3 application.
I am trying to build a secure of set of tests with Symfony2, Doctrine
I'm currently trying to build a more or less complete set of unit tests
I am trying to get a build process set up in TeamCity 5, and
I am trying to set up a Build Server using CC.NET and TFS. In
I am trying to set up an automated build system on Windows using Cygwin.
I'm trying to set up Jenkins (v1.47) to build a project using the ClearCase
I'm trying to automate InstallShield from my build process, and I need to set
I am trying to write an MVC webpage that has two drop down lists.

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.