joval
joval

Reputation: 506

prevent GridBagLayout from resizing columns

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.

Upvotes: 1

Views: 4121

Answers (2)

aterai
aterai

Reputation: 9808

This might work:

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

public class ResizeIssue2 {
  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 ActionListener() {
        @Override
        public void actionPerformed(ActionEvent ae) {
            valueLabel.setText(String.valueOf(--value));
        }
    });

    JButton incButton = new JButton("+");
    incButton.addActionListener(new 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);

    //*
    c.gridy = 1; 
    int w = 32; //incButton.getPreferredSize().width;
    for(c.gridx=0;c.gridx<3;c.gridx++) {
      panel.add(Box.createHorizontalStrut(w), c);
    }
    // */

    frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
    frame.add(panel);
    frame.pack();
    frame.setVisible(true);
  }
}

Upvotes: 1

Devon_C_Miller
Devon_C_Miller

Reputation: 16528

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);

Upvotes: 3

Related Questions