Peter
Peter

Reputation: 67

Java Swing | The last JTextField doesn't sets to custom size

I'm creating a Word Puzzle Solver. I created for loops to create a JTextField "grid".

enter image description here

When the last field in the lower right corner is created it does not resize to the size I entered. It is resizing to the size of frame.

My code:

public class SolverGUI {

    int sizeOfRect = 50;
    static int height = 5;
    static int width = 5;
    static JTextField[][] textFields = new JTextField[width][height];
    static JFrame frame = new JFrame("Word Puzzle Solver");

    public void createAndShowGUI() {
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setSize(800, 800);
        createWordPuzzleArea();
        frame.setVisible(true);
    }
    public void createWordPuzzleArea() {
        for (int i = 0; i < height; i++) {
            for (int i2 = 0; i2 < width; i2++) {
                textFields[i2][i] = new JTextField();
                frame.add(textFields[i2][i]);
                textFields[i2][i].setBounds(10 + (i2 * sizeOfRect), 10 + (i * sizeOfRect), sizeOfRect, sizeOfRect);
            }
        }
    }
}

Upvotes: 1

Views: 74

Answers (1)

Frakcool
Frakcool

Reputation: 11143

Instead of making the grid yourself, you could make use of GridLayout.

Making use of setBounds makes me think you're using null-layout which is not right, it might seem like the best and easiest way to make a complex UI, but it isn't, if you don't believe me check this example. Swing has to deal with multiple PLAFs, OS, screen sizes and resolutions and thus pixel-perfect UIs aren't good, as you can see in the example everything breaks, and thus using layout managers like GridLayout solve these issues.

You can instead make it like this, and you're avoiding making the calculation of each cell yourself, and let the layout manager do that for you :)

import java.awt.Dimension;
import java.awt.GridLayout;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.SwingUtilities;

public class GridOfTextFields {
    private JFrame frame;
    private JPanel pane;
    private JTextField[][] fields;
    
    private static final int ROWS = 5;
    private static final int COLS = 5;
    
    public static void main(String[] args) {
        SwingUtilities.invokeLater(new GridOfTextFields()::createAndShowGUI);
    }
    
    private void createAndShowGUI() {
        frame = new JFrame(getClass().getSimpleName());
        pane = new JPanel() {
            @Override
            public Dimension getPreferredSize() {
                return new Dimension(300, 300);
            }
        };
        
        pane.setLayout(new GridLayout(ROWS, COLS, 1, 1));
        
        fields = new JTextField[ROWS][COLS];
        
        for (int i = 0; i < ROWS; i++) {
            for (int j = 0; j < COLS; j++) {
                fields[i][j] = new JTextField();
                pane.add(fields[i][j]);
            }
        }
        
        frame.add(pane);
        
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.pack();
        frame.setVisible(true);
    }
}

enter image description here


And just for the sake of explaining why your code isn't working is that you're fighting the layout manager, I'm getting this output in my computer when running your code:

enter image description here

The way to make it work is to call frame.setLayout(null) which again IS NOT RECOMMENDED, see the second paragraph of this post, if you insist on keep doing it with null-layout you'll just find yourself into more and more troubles as you keep adding features to your program.

After removing the layout manager:

enter image description here

Upvotes: 3

Related Questions