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 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 load data into my grid using a WCF service. When a user clicks
I'm working on drawing an SVG grid using Javascript. I've managed to get the
I'm trying to produce sheets of photographs with captions arranged in a grid using
What is the best breakdown of responsibility when using a virtual grid and the
Is it possible to show multiple lines of text in grid cells using the
I'm using a couple of Grid s to format multiple GridViewColumn.CellTemplate s: <ListView SharedSizeScope=true>
I'm using a ListView in C# to make a grid. I would like to
If you are using ASP.NET MVC how are you doing grid display? Rolled your
I am using ASP.NET 2.0 and VS 2005. I need to populate a grid
We have a requirement to increase the functionality of a grid we are using

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.