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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T21:24:27+00:00 2026-05-18T21:24:27+00:00

I am using grid bag layout for my Java application, but the problem is,

  • 0

I am using grid bag layout for my Java application, but the problem is, it is not placing the components in the page start. Here is the code I am using:

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

public class Trial extends JFrame {

  JLabel banner;
  Container c;
  GridBagConstraints gbc = new GridBagConstraints();
  GridBagLayout gbl;
  public Trial()
  {
    setTitle("Attendence Manager");
    setIconImage(Toolkit.getDefaultToolkit().getImage("images/icon.png"));
    Dimension dim= Toolkit.getDefaultToolkit().getScreenSize();
    setSize(new Dimension(dim.width-20,dim.height-100));
    c= getContentPane();
    gbl= new GridBagLayout();
    setLayout(gbl);
    banner = new JLabel(new ImageIcon("images/banner.jpg"));
    gbc.anchor=GridBagConstraints.PAGE_START;
    gbc.gridx=0;
    gbc.gridy=0;
    gbc.gridwidth=GridBagConstraints.REMAINDER;
    c.add(banner,gbc);
    this.setVisible(true);
    addWindowListener(new MyWindowAdapter());
  }

  public static void main(String[] args) {
    Trial t = new Trial();
  }

}
class MyWindowAdapter extends WindowAdapter
{
  //LoginPage sp;
  public MyWindowAdapter()
  {
  }

  @Override
  public void windowClosing(WindowEvent we)
  {
    System.exit(0);
  }
}

I have also tried

gbc.anchor = GridBagConstraints.FIRST_LINE_START;

even that didn’t work. This is the output I am getting:

Image

  • 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-05-18T21:24:28+00:00Added an answer on May 18, 2026 at 9:24 pm

    First you need to set

    gbc.fill = GridBagConstraints.BOTH;
    gbc.weightx = 1.0;
    gbc.weighty = 1.0;
    

    but this will only make the JLabel to fill the whole space not the icon inside the JLabel. If you want to your image to scale and use up the whole space. I would suggest you read the image into a BufferedImage and then override the paintComponent() method to draw a scaled instance of the BufferedImage. Like this:

    public Trail() {
        setTitle("Attendence Manager");
        setIconImage(Toolkit.getDefaultToolkit().getImage("images/icon.png"));
        Dimension dim = Toolkit.getDefaultToolkit().getScreenSize();
        setSize(new Dimension(dim.width - 20, dim.height - 100));
        c = getContentPane();
        gbl = new GridBagLayout();
        setLayout(gbl);
    
        try {
            final BufferedImage image = ImageIO.read(new File("images/sample.jpg"));
            banner = new JLabel(){
                public void paintComponent(Graphics g) {
                    g.drawImage(image, 0, 0, getWidth(), getHeight(), null);
                }
            };
            gbc.fill = GridBagConstraints.BOTH;
            gbc.weightx = 1.0;
            gbc.weighty = 1.0;
            c.add(banner, gbc);
        }
        catch (IOException ex) {
            Logger.getLogger(Trail.class.getName()).log(Level.SEVERE, null, ex);
        }
    
        this.setVisible(true);
        addWindowListener(new MyWindowAdapter());
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

i am using grid view in vb.net. here is code... If Not DataGridView1.SelectedRows.Count =
I'm learning databases using Java, and here is the simple code for that: import
I am using a grid and a class of java to render the data
I'm not using a Grid, just using the MvcContrib Pager. I have a partial
I'm using gridbaglayout for layout manager. And its size 6*10. But when i change
I am using Grid view control to insert data into oracle database but when
I'm not exactly new to java (I've been using it for a year now)
I've been tasked with doing refactoring to a Java Swing application we have here
I'm generating some JComponents in code and using the GridBag layout to arrange them.
I'm creating an easy GUI using GridBagLayout and almost everything goes right, but there

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.