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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T22:50:02+00:00 2026-06-12T22:50:02+00:00

I have this layout using GridBagLayout : public class Example extends JFrame { public

  • 0

I have this layout using GridBagLayout:

public class Example extends JFrame {
    public Example() {
        Border outline = BorderFactory.createLineBorder(Color.black);
        GridBagLayout gbl = new GridBagLayout();
        GridBagConstraints gbc = new GridBagConstraints();
        JPanel pane = new JPanel(gbl);

        gbc.weighty = 1.0;
        gbc.weightx = 1.0;

        JLabel unitLbl = new JLabel("Unit");
        unitLbl.setBorder(outline);
        gbc.gridx = 0;
        gbc.gridy = 0;
        gbc.ipadx = 30;
        gbc.ipady = 10;
        gbl.setConstraints(unitLbl, gbc);
        pane.add(unitLbl);

        JLabel typeLbl = new JLabel("Type");
        typeLbl.setBorder(outline);
        gbc.gridx = 0;
        gbc.gridy = 1;
        gbc.ipadx = 30;
        gbc.ipady = 10;
        gbl.setConstraints(typeLbl, gbc);
        pane.add(typeLbl);

        JTextField unitField = new JTextField();
        typeLbl.setBorder(outline);
        gbc.gridx = 1;
        gbc.gridy = 0;
        gbc.ipadx = 30;
        gbc.ipady = 10;
        gbl.setConstraints(unitField, gbc);
        pane.add(unitField);

        String[] type = {"All", "Verb", "Noun", "Adjective"};
        JComboBox<String> comboBox = new JComboBox<String>(type);
        gbc.gridx = 1;
        gbc.gridy = 1;
        gbc.ipadx = 30;
        gbc.ipady = 10;
        gbl.setConstraints(comboBox, gbc);
        pane.add(comboBox);


        add(pane, BorderLayout.CENTER);
        setSize(new Dimension(400, 300));
        getContentPane().setBackground(Color.WHITE);
        setVisible(true);
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                new Example();
            }
        });
    }
}

In this example, when run, It seems that every component is at the center of the frame. But what I want is :

  1. Two JLabel (unitLbl and typelbl) will be on the left of frame
  2. JTextField and JComboBox will be on the right of two JLabel, respectively with a small distance between.
  3. Moreover, I want to add a new JButton at location (3,0) of the grid, but the height of this location sum of two JLabel height. It means, this button height is on “two line”.

How can I fix this code to achieve this goal ? Please help me.

Thanks 🙂

  • 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-12T22:50:03+00:00Added an answer on June 12, 2026 at 10:50 pm

    You want to use GridBagConsraints#anchor to define the position within the cell that you want to align the component to.

    To allow a component to span over number of cells, you want to use GridBagConstraints#gridwidth and GridBagConstraints#gridheight (the default is 1)

    enter image description here

    public class TestLayout09 {
    
        public static void main(String[] args) {
            new TestLayout09();
        }
    
        public TestLayout09() {
            EventQueue.invokeLater(new Runnable() {
                @Override
                public void run() {
                    try {
                        UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                    } catch (ClassNotFoundException ex) {
                    } catch (InstantiationException ex) {
                    } catch (IllegalAccessException ex) {
                    } catch (UnsupportedLookAndFeelException ex) {
                    }
    
                    JFrame frame = new JFrame();
                    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                    frame.setLayout(new BorderLayout());
                    frame.add(new LayoutPane());
                    frame.setBackground(Color.WHITE);
                    frame.pack();
                    frame.setLocationRelativeTo(null);
                    frame.setVisible(true);
                }
    
            });
        }
    
        public class LayoutPane extends JPanel {
    
            public LayoutPane() {
                Border outline = BorderFactory.createLineBorder(Color.black);
                setLayout(new GridBagLayout());
                GridBagConstraints gbc = new GridBagConstraints();
    
                // I'm not sure this really is what you want, but I may be mistaken
    //            gbc.weighty = 1.0;
    //            gbc.weightx = 1.0;
    
                JLabel unitLbl = new JLabel("Unit");
                unitLbl.setBorder(outline);
                gbc.gridx = 0;
                gbc.gridy = 0;
                gbc.ipadx = 30;
                gbc.ipady = 10;
                gbc.anchor = GridBagConstraints.WEST;
                add(unitLbl, gbc);
    
                JLabel typeLbl = new JLabel("Type");
                typeLbl.setBorder(outline);
                gbc.gridx = 0;
                gbc.gridy = 1;
                gbc.ipadx = 30;
                gbc.ipady = 10;
                add(typeLbl, gbc);
    
                JTextField unitField = new JTextField();
                typeLbl.setBorder(outline);
                gbc.gridx = 1;
                gbc.gridy = 0;
                gbc.ipadx = 30;
                gbc.ipady = 10;
                gbc.anchor = GridBagConstraints.EAST;
                add(unitField, gbc);
    
                String[] type = {"All", "Verb", "Noun", "Adjective"};
                JComboBox<String> comboBox = new JComboBox<String>(type);
                gbc.gridx = 1;
                gbc.gridy = 1;
                gbc.ipadx = 30;
                gbc.ipady = 10;
                add(comboBox, gbc);
    
                JButton btn = new JButton("Test");
                gbc.gridx = 3;
                gbc.gridy = 0;
                gbc.fill = GridBagConstraints.BOTH;
                gbc.gridheight = 2;
                add(btn, gbc);
            }
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have designed the screen using this relative layout. <?xml version=1.0 encoding=utf-8?> <RelativeLayout xmlns:android=http://schemas.android.com/apk/res/android
I have an item layout like this, and set the background by using item
I'm using an sqlite database in my app. I have this dbhelper class in
alt text http://img705.yfrog.com/img705/1337/layoutxt.jpg I already have this layout created using CSS, but its plagued
I have a regular layout that looks that this: This layout is done using
I have this layout: <div id=sectors> <h1>Sectors</h1> <div id=s7-1103 class=alpha></div> <div id=s8-1104 class=alpha></div> <div
I am using ActionBarSherlock, in the ActionBar i have this custom layout to appear
I have this JSON layout: [ { tag: div, attr: [ [id, sortSideStyleIlluminated], [class,
So I have this layout and I'm planning on using javascript/jquery to change the
I have read this blog entry about using relative layout to optimize layout in

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.