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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T04:01:22+00:00 2026-06-15T04:01:22+00:00

Hi i am beginner to java and doing small GUI using GridBagLayout. See the

  • 0

Hi i am beginner to java and doing small GUI using GridBagLayout. See the attached code and also the output . what i want is to place the JButtons on top left corner as per the position assigned in gridx and gridy . But it placing components in center instead of top left as expected , if i use Insets , gridx /gridy all that is working but not from proper coordinates so please see attached code and image and guide me about it

public  rect()
{     

      JPanel panel = new JPanel( new GridBagLayout());
      GridBagConstraints gbc = new GridBagConstraints();
      JButton nb1= new JButton("Button1  ");
      JButton nb2= new JButton("Button2  ");

      gbc.gridx=0;
      gbc.gridy=0 ;
      panel.add(nb1, gbc);
      gbc.gridx=1;
      gbc.gridy=1; 
      panel.add(nb2, gbc);
      panel.setVisible(true);
      JFrame  frame = new JFrame("Address Book "); 
      frame.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);
      frame.setSize(300, 300 );
      frame.add(panel);

      frame.setVisible(true); 


}

OUTPUT : want these buttons on top left please guide me

enter image description here

  • 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-15T04:01:23+00:00Added an answer on June 15, 2026 at 4:01 am

    I think the problem is more the use of setSize(..), you should rather use appropriate LayoutManager and call pack() on JFrame instance after adding all components to JFrame and before setting JFrame visible, also no need for panel.setVisible(..):

    enter image description here

    public static void main(String[] args) {
    
        SwingUtilities.invokeLater(new Runnable() {
            @Override
    
            public void run() {
                JPanel panel = new JPanel(new GridBagLayout());
    
                JButton nb1 = new JButton("Button1  ");
                JButton nb2 = new JButton("Button2  ");
    
                GridBagConstraints gbc = new GridBagConstraints();
    
                gbc.gridx = 0;
                gbc.gridy = 0;
                panel.add(nb1, gbc);
    
                gbc.gridx = 1;
                gbc.gridy = 1;
                panel.add(nb2, gbc);
    
                JFrame frame = new JFrame("Address Book ");
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    
                frame.add(panel);
    
                frame.pack();
                frame.setVisible(true);
            }
        });
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

i am doing a small Gui in java. i am using setBounds methods to
Am Beginner to java.I have a small doubt while i am developing an application
I am a beginner in Java and am doing an exercise from a book.
I am beginner to java and was trying out this code puzzle from the
hi guys im beginner in java :) so what im doing is that im
I'm Java beginner, but I thought that when using try-catch-finally I don't have to
A school assignment (in beginner Java) is asking me to create a small contact
I am Java beginner and I am trying to code something. Currently I am
I am beginner with Java, and I would like to write some code like
I'm a beginner in java programming. Now I'm doing a game called mastermind. It

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.