-1

At the moment I've got a 9x9 grid of text fields using a GridBayLayout. Because the grid makes each text field slighter longer than it is tall (even when I set a specific height for the text field), the grid is a rectangle, rather than a square.

How can I either make the entire grid a specific size, or each text field inside the grid a specific size?

EDIT: I need the 9 x 9 grid of text fields to be square, rather than rectangular.

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433

1 Answers1

2

The problem is with sizing hints, methods [get/set][Minimum/Maximum/Preferred]Size are telling the layout manager what size the component wants to be and layout managers sometimes ignore that and sometimes they don't. GridBagLayout respects that and your JTextFields are tellimg him that they want to be rectangular. How JTextFields figure out what size they want to be is all kinds of difficult so let's leave that out.

If you want to make sure that all your JTextFields are of the same size, you should override these methods. Subclass JTextField and make something like this:

final class SquareJTextField extends JTextField {
    private static Dimension maxDimension = new Dimension(0, 0);

    @Override
    public Dimension getPreferredSize() {
        Dimension d = super.getPreferredSize();
        // take the larger value
        int max = d.width > d.height ? d.width : d.height;
        // compare it against our static dimension
        // height je rovnaky ako width
        if (max > maxDimension.width)
            maxDimension = new Dimension(max, max);
        // return copy so no one can change the private one
        return new Dimension(maxDimension);
    }

    @Override
    public Dimension getMinimumSize() {
        Dimension d = super.getPreferredSize();
        int max = d.width > d.height ? d.width : d.height;
        if (max > maxDimension.width)
            maxDimension = new Dimension(max, max);
        return new Dimension(maxDimension);
    }

    @Override
    public Dimension getMaximumSize() {
        Dimension d = super.getPreferredSize();
        int max = d.width > d.height ? d.width : d.height;
        if (max > maxDimension.width)
            maxDimension = new Dimension(max, max);
        return new Dimension(maxDimension);
    }
}

All objects of this class should be the same dimension and should be square.

Replace JtextFields in your grid with SquareJTextField

MatheM
  • 801
  • 7
  • 17
  • If GridBagLayout sespects sizing hints, wouldn't it be easier to set it instead of subclass ? Just wondering... – Betlista Feb 05 '18 at 15:00
  • I think in this case the JTextFields are telling the Layout what size they are, which means while the GridBag could presume a certain size, the JTextFields will still remain the size they want to be, and may end up overlapping or going off the end or something. – Perry Monschau Feb 05 '18 at 16:20
  • 1
    @Betlista It is generally a bad idea to set the sizing hints with the setters. [Here](https://stackoverflow.com/questions/7229226/should-i-avoid-the-use-of-setpreferredmaximumminimumsize-methods-in-java-swi) is the reason why I am avoiding it. – MatheM Feb 05 '18 at 19:17