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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T19:56:38+00:00 2026-06-16T19:56:38+00:00

I am currently designing GUI for an application using Swing. If you take a

  • 0

I am currently designing GUI for an application using Swing. If you take a look at the picture, you see a yellow-colored JPanel, which is placed inside a BorderLayout. This panel uses a GridBagLayout to place components inside it. As you can see, one row of components consists of a label, a text field and a button. At the moment, there are two rows of components, vertically centered. I would like to them to be placed from top of the panel, though. I tried setting frame.setAlignmentY(TOP_ALIGNMENT) as well as gridBagConstraints.anchor property, but with no results. I think my approach to this problem is wrong, and instead of struggling for hours, I turn to good people of stackoverflow.

So the question is, how to force the grid of components to be vertically not centered, but on top? Thank you for any kind of advice pointing in me in the right direction.

Frame

EDIT (code):

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;

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

/**
 * 
 * @author mt
 */
public class MainView extends JFrame {

    public MainView() {
        setPreferredSize(new Dimension(500, 500));
        getContentPane().setLayout(new BorderLayout());

        JPanel panel = new JPanel(new GridBagLayout());
        panel.setBackground(Color.YELLOW);
        GridBagConstraints c = new GridBagConstraints();
        c.insets = new Insets(4, 4, 4, 4);

        c.gridx = 0;
        c.gridy = 0;
        panel.add(new JLabel("PokerStars"), c);

        c.gridx = 1;
        c.gridy = 0;
        panel.add(new JTextField(20), c);

        c.gridx = 2;
        c.gridy = 0;
        panel.add(new JButton("btn"), c);

        c.gridx = 0;
        c.gridy = 1;
        panel.add(new JLabel("Full Tilt Poker"), c);

        c.gridx = 1;
        c.gridy = 1;
        panel.add(new JTextField(20), c);

        c.gridx = 2;
        c.gridy = 1;
        panel.add(new JButton("btn"), c);

        add(panel, BorderLayout.LINE_START);

        pack();
        setVisible(true);
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                new MainView();
            }
        });
    }
}
  • 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-16T19:56:40+00:00Added an answer on June 16, 2026 at 7:56 pm

    OK, one way to solve your issue is to add an additional ‘invisible’ component at the end of your panel and make it take all the extra available vertical space. Here is how you can do that:

    import java.awt.BorderLayout;
    import java.awt.Color;
    import java.awt.Dimension;
    import java.awt.GridBagConstraints;
    import java.awt.GridBagLayout;
    import java.awt.Insets;
    
    import javax.swing.Box;
    import javax.swing.JButton;
    import javax.swing.JFrame;
    import javax.swing.JLabel;
    import javax.swing.JPanel;
    import javax.swing.JTextField;
    import javax.swing.SwingUtilities;
    
    /**
     * 
     * @author mt
     */
    public class MainView extends JFrame {
    
        public MainView() {
            setPreferredSize(new Dimension(500, 500));
            getContentPane().setLayout(new BorderLayout());
            JPanel panel = new JPanel(new GridBagLayout());
            panel.setBackground(Color.YELLOW);
            GridBagConstraints c = new GridBagConstraints();
            c.insets = new Insets(4, 4, 4, 4);
            panel.add(new JLabel("PokerStars"), c);
    
            panel.add(new JTextField(20), c);
    
            c.gridwidth = GridBagConstraints.REMAINDER;
            panel.add(new JButton("btn"), c);
            c.gridwidth = 1;
    
            panel.add(new JLabel("Full Tilt Poker"), c);
    
            panel.add(new JTextField(20), c);
    
            c.gridwidth = GridBagConstraints.REMAINDER;
            panel.add(new JButton("btn"), c);
            c.weighty = 1.0;
            panel.add(Box.createGlue(), c);
    
            add(panel, BorderLayout.LINE_START);
    
            pack();
            setVisible(true);
        }
    
        public static void main(String[] args) {
            SwingUtilities.invokeLater(new Runnable() {
                @Override
                public void run() {
                    new MainView();
                }
            });
        }
    }
    

    While this is not my favourite option, it will solve your issues in an acceptable manner. There is a probably a more interesting way to solve this but it requires to know what kind of UI you are targetting and how you expect the various component to behave when the window is resized.

    In most cases, you should try not to set gridx/gridy and use their default value (RELATIVE), it makes your code shorter and it is a lot easier to maintain if you have to insert one or more components.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm currently designing a web application using php, javascript, and MySQL. I'm considering two
I'm currently designing an application which I will ultimately want to move to Windows
I'm currently designing my first ever GUI for Windows. I'm using MFC and Visual
I'm currently designing a web application which may be viewed by people in all
We are currently designing a GIS intranet application using GWT and ESRI ArcGIS. We
I'm currently designing an Android application, which will be published in Play for free.
I'm currently designing a small application in which I have: - 1 menu (graphic
I'm currently designing a database for a small bookmarking application (using MySQL) and I'd
I am currently designing a GUI to control some software. I am using fltk,
I'm currently designing a windows mobile application using compact framework 3.5 and I need

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.