AlexQuezada
AlexQuezada

Reputation: 143

Why does the last item added to a panel occupy the whole panel?

I'm adding a quantity of JTextField to a panel, and all of them are added but, the last one added takes the whole panel and seems all other text boxes added on the last one..... here is the code

public JPanel crearCartonFormulario() {
    panel = new JPanel();
    panel.setLayout(new BorderLayout());
    JTextField[] textBoxes = new JTextField[25];
    int cont = 0;
    int posX = 10;
    int posY = 0;
    llenarArreglo();
    while (cont <= 4) {
        for (int i = 0; i <= 4; i++) {

            if (cont == 2 && i == 2) {
                textBoxes[i] = new JTextField("");
            } else {
                textBoxes[i] = new JTextField(String.valueOf(numeros[cont][i]));
            }
            textBoxes[i].setBounds(i + posX, 15 + posY, 40, 40);
            textBoxes[i].setEditable(false);
            panel.add(textBoxes[i]);
            posX += 50;
        }
        posY += 50;
        posX = 10;
        cont++;
    }
    return panel;
}

This is returned at a panel where I keep multiple panels of this one, it works but in this one the last JTextField takes the whole panel space....

The new JFrame that contains the panels created by the method, adopt the last JTextField size and that text box doesn't take the bounds indicated by the method, but all the other text boxes still inside and correctly added.

Upvotes: 1

Views: 122

Answers (3)

Roan
Roan

Reputation: 1206

This is because you are using BorderLayout and BorderLaout Always requires a parameter like BorderLayout.CENTER, BorderLayout.WEST, BorderLayout.EAST, BorderLayout.NORTH and BorderLayout.SOUTH.

So basically BorderLayout only has 5 position where a component can go. And if you do not specify where when adding a component it defaults to BorderLayout.CENTER. And as there can only be one component at a time in the BorderLayout.CENTER position it only really adds the last one. So I'd suggest an other layout manager like GridLayout( if you want all the components to be equally sized).

I hope this helps :).

P.S. If you want me to give some explination on GridLayout just ask.

Upvotes: 0

camickr
camickr

Reputation: 324128

panel.setLayout(new BorderLayout());

You are using a BorderLayout.

panel.add(textBoxes[i]);

When you use the add() method the default is to add the component to the CENTER of the BorderLayout. However, only a single component can be added to the center so the layout manager will only manage the size/location of the last component added. The rules of the BorderLayout is to make the component take up all the available space.

However, you have also used the setBounds() methods for the other text fields which is causing a problem. You should NOT attempt to use a layout manager and manage the bounds of the components yourself.

The solution is to just use a layout manager and let the layout manager do its job. Read the section from the Swing tutorial on Using Layout Managers for more information and use a more appropriate layout manager.

Update:

its a bingo table

Then maybe you shouldn't even be using JTextFields. Maybe a JTable would be a better component to use. The tutorial also has a section on How to Use Tables.

Upvotes: 3

Hovercraft Full Of Eels
Hovercraft Full Of Eels

Reputation: 285405

Your problem is here:

panel.setLayout(new BorderLayout());

You set the layout to BorderLayout and yet add components to the JPanel as if it were a GridLayout. Understand that when you add components to a BorderLayout-using container in a default way, the components get added in the BorderLayout.CENTER position which fills this position, covering anything added prevsiously.

Perhaps you wish to use a GridLayout instead? You will want to read the layout manager tutorial for more.

Upvotes: 1

Related Questions