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 8789775
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T22:30:20+00:00 2026-06-13T22:30:20+00:00

this is my first question on here, so please forgive me when i am

  • 0

this is my first question on here, so please forgive me when i am breaking any rules, or not using the right format

I am creating a simple form in java swing which consists of 1 JLabel, 1 JTextField, and 1 Button

|---------------------------|
|                           |
|           JLabel          |
|                           |
|---------------------------|
|    JTextField    | Button |
|---------------------------|

The Button should be in the bottom-right corner, the JTextField left of it, the JLabel on the top, spanning both columns

I want the Button to be a fixed size, the JTextField a fixed height, but using the full width (except for what is in use by the Button), and the JLabel using all other space (with and height)

I am not even sure if i should use a GridBagLayout or another Layout ?

This is probably a very easy question, but got me puzzled for quite some time (too many options with the GridBarLayout i guess)

  • 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-13T22:30:21+00:00Added an answer on June 13, 2026 at 10:30 pm

    OK here is a demo code that should get you going:

    import java.awt.Color;
    import java.awt.GridBagConstraints;
    import java.awt.GridBagLayout;
    
    import javax.swing.BorderFactory;
    import javax.swing.JButton;
    import javax.swing.JFrame;
    import javax.swing.JLabel;
    import javax.swing.JPanel;
    import javax.swing.JTextField;
    import javax.swing.SwingUtilities;
    
    public class TestGridBagLayout {
    
        protected void initUI() {
            JFrame frame = new JFrame(TestGridBagLayout.class.getSimpleName());
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            JButton button = new JButton("A button");
            JTextField textField = new JTextField();
            JLabel label = new JLabel("A cool long nice label that will stretch.");
            label.setHorizontalAlignment(JLabel.CENTER);
            label.setBorder(BorderFactory.createLineBorder(Color.GREEN));
            JPanel panel = new JPanel(new GridBagLayout());
            GridBagConstraints gbc = new GridBagConstraints();
            gbc.fill = GridBagConstraints.BOTH;// Fill the "cell" in both direction
            gbc.weightx = 1.0;// Allocate extra-width to the label
            gbc.weighty = 1.0;// Allocate extra-height to the label
            gbc.gridwidth = GridBagConstraints.REMAINDER;// The label takes all the available width of the "row"
            panel.add(label, gbc);
    
            gbc.weighty = 0; // Don't stretch TF vertically
            gbc.fill = GridBagConstraints.HORIZONTAL; // Fill horizontally
            gbc.gridwidth = GridBagConstraints.RELATIVE;
            panel.add(textField, gbc);
            gbc.weightx = 0; // No extra horizontal space is given to the button
            gbc.fill = GridBagConstraints.NONE; // No fill for the button
            panel.add(button, gbc);
            frame.add(panel);
            frame.pack();
            frame.setVisible(true);
        }
    
        public static void main(String[] args) {
            SwingUtilities.invokeLater(new Runnable() {
                @Override
                public void run() {
                    new TestGridBagLayout().initUI();
                }
            });
        }
    
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This is my first post here so please forgive any protocol errors. My question
First question here, so please forgive me if this seems obvious. I'm having a
this is my first question here :) I know that I should not check
this is my first question on here. The question is simple - # this
This is my first post, so please forgive me if this question has been
First of all I don't know if it this question belongs here if not
(Please forgive me in advance for any formatting errors, as this is the first
This is my first question here and I hope it is simple enough to
This is my first question posting here. I'm novice to Flash builder,using Flash Builder
This is my first question here, so please try to be patient with me

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.