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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T21:14:10+00:00 2026-06-04T21:14:10+00:00

Another problem with swing. How can I stop GridBagLayout from respacing components if one

  • 0

Another problem with swing. How can I stop GridBagLayout from respacing components if one of them changes size? For example, I have few columns, in one of which there is a JLabel with text “text”. When I change it to “texttext” layout manager resizes the whole column. I don’t want it to do that. Is there any way to prevent it?

Example:

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

public class ResizeIssue {

 static int value = 99;

 public static void main(String[] args) {

    JFrame frame = new JFrame();
    final JLabel valueLabel = new JLabel(String.valueOf(value));
    JButton decButton = new JButton("-");
    decButton.addActionListener(new java.awt.event.ActionListener() {

        @Override
        public void actionPerformed(ActionEvent ae) {
            valueLabel.setText(String.valueOf(--value));
        }
    });

    JButton incButton = new JButton("+");
    incButton.addActionListener(new java.awt.event.ActionListener() {

        @Override
        public void actionPerformed(ActionEvent ae) {
            valueLabel.setText(String.valueOf(++value));
        }
    });

  JPanel panel = new JPanel();
  panel.setLayout(new GridBagLayout());
  GridBagConstraints c = new GridBagConstraints();
  c.weightx = 1;
  c.gridx = 0;
  c.gridy = 0;
  panel.add(decButton, c);
  c.gridx = 1;
  panel.add(valueLabel, c);
  c.gridx = 2;
  panel.add(incButton, c);

  frame.add(panel);
  frame.pack();
  frame.setVisible(true);
  }
 }

It is visible while 9 -> 10 or anytime text changes width.

  • 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-04T21:14:12+00:00Added an answer on June 4, 2026 at 9:14 pm

    GridBagLayout ignores maximumWidth/Height. There’s not an easy way to set the maximum size of the JLabel.

    But, what I think you really want is the layout to not shift when the text in the JLabel changes.

    That can be done by making the JLabel wide enough to hold the largest value it needs to display. For example:

        jLabel1.setFont(new Font("monospace", Font.PLAIN, 12));
        FontMetrics fm = jLabel1.getFontMetrics(jLabel1.getFont());
        int w = fm.stringWidth("0000");
        int h = fm.getHeight();
        Dimension size = new Dimension(w, h);
        jLabel1.setMinimumSize(size);
        jLabel1.setPreferredSize(size);
    

    Update:

    To center the label text, just add:

        jLabel1.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Following on from a question I posted yesterday about GUIs, I have another problem
I have a problem with a JPanel inside another one. I don't know why,
I've run into another problem in Qt, I can't seem to figure out how
After getting a helpful answer here , I have run into yet another problem:
I'm working on a swing program to display several pictures. And one can rotate
Following my previous post in this link I have another problem . Given the
In my application, I have two swing components on top of each other, that
having another problem with my javascript for my table. Basically, if you look at
I've run into another problem while fixing up my game to use this library.
I've got another problem in the same code... I'm getting this error: initialization method

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.