Swing doesn't respect my GridLayout Rows x Columns definition. How to fix?
Asked Answered
Q

1

5

I have been trying to resolve this without success. How to make JPanel p respect the defined GridLayout? All I get is the first row containing 3 panels but not 4 as I've told Java to perform.

What's the necessary voodoo (or knowledge to my ignorance), to make it work?

package temp2;

import java.awt.Dimension;
import java.awt.GridLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;


public class Temp2 {

    public static void main(String[] args) {

        JFrame f = new JFrame();
        JPanel p = new JPanel();  
    //  GridLayout flow = new GridLayout(1, 5); --> With this I get a row with 5 jpanels
        GridLayout flow = new GridLayout(2, 4); // --> Why it doenst work? I want 4 
                                                // jpanels in the first row and 
                                                // subsequent one on the next row
                                                // --> Why it doesnt respect my code?
        p.setLayout(flow);
      // p.setSize(800,800); // --> This doesnt make any difference on final result either
      //  p.setPreferredSize(new Dimension(800,800));  // --> This doesnt make any
                                                      // difference on final result either
        p.setMinimumSize(new Dimension(800,800));

        JPanel x1 = new JPanel();     
        x1.setPreferredSize(new Dimension(50,30)); // --> It doesnt respect these preferences
        x1.setMaximumSize(new Dimension(50,30)); // --> It doesnt respect this maximum
        x1.setBorder(javax.swing.BorderFactory.createEtchedBorder());

        JPanel x2 = new JPanel();       
        x2.setPreferredSize(new Dimension(50,30)); // --> It doesnt respect these preferences
        x2.setMaximumSize(new Dimension(50,30)); // --> It doesnt respect this maximum
        x2.setBorder(javax.swing.BorderFactory.createEtchedBorder());

        JPanel x3 = new JPanel();      
        x3.setPreferredSize(new Dimension(50,30)); // --> It doesnt respect these preferences
        x3.setMaximumSize(new Dimension(50,30)); // --> It doesnt respect this maximum
        x3.setBorder(javax.swing.BorderFactory.createEtchedBorder());

        JPanel x4 = new JPanel();      
        x4.setPreferredSize(new Dimension(50,30)); // --> It doesnt respect these preferences
        x4.setMaximumSize(new Dimension(50,30));  // --> It doesnt respect this maximum      
        x4.setBorder(javax.swing.BorderFactory.createEtchedBorder());

        JPanel x5 = new JPanel();       
        x4.setMaximumSize(new Dimension(50,30)); // --> It doesnt respect these preferences
        x5.setPreferredSize(new Dimension(50,30)); // --> It doesnt respect this maximum 
        x5.setBorder(javax.swing.BorderFactory.createEtchedBorder());

         p.add(x1);
         p.add(x2);
         p.add(x3);
         p.add(x4);
         p.add(x5);

         f.getContentPane().add(p, "Center");

         f.setSize(800, 800);
         f.setVisible(true);
         p.updateUI();

        }   
    }

As this is my first StackOverflow question I'm trying to follow the rules strictly:

Be specific: I want GridLayout to respect row x columns definition

What my final purpose: Show 4 panels in the first row and subsequent one on the second row.

Make it relevant to others: That's why this code is didactic, repeating all the panel declarations so newcomers (and me) can understand all the code and focuses exclusively on the GridLayout issue.

Queston answered 17/4, 2015 at 22:2 Comment(3)
See Should I avoid the use of set(Preferred|Maximum|Minimum)Size methods in Java Swing? (Yes.) BTW - GridLayout will stretch each component in the layout to the width of the widest one, and the height of the tallest. Perhaps look to GridBagLayout that will reserve enough space for each component in its column and row, but (depending on GridBagConstraints) not stretch (& instead use the preferred size of) components.Urinary
Is be very, very careful with setting the min size of the containerSabotage
Thanks for the inputs. I will take a look about GridBagLayout to see if I can get what I'm actually trying (the same 4 jpanels on 1st row). Well I removed all code about Preferred|Maximum|Minimum Sizes and GridLayout still doenst respect my grid definitions. My guess is that the GridLayout definition is not the most important but how Java thinks what's the most usefull way to distribute these components inside jPanel? Im bit lost ...Queston
U
14
    GridLayout flow = new GridLayout(2, 4); // --> Why it doenst work? I want 4 
                                            // jpanels in the first row and 
                                            // subsequent one on the next row
                                            // --> Why it doesnt respect my code?

Like this?

enter image description here

Use instead:

    GridLayout flow = new GridLayout(0,4);

Quoting the docs for the constructor:

Creates a grid layout with the specified number of rows and columns. All components in the layout are given equal size.

One, but not both, of rows and cols can be zero, which means that any number of objects can be placed in a row or in a column.

Urinary answered 17/4, 2015 at 22:19 Comment(1)
Many thanks Andrew. You deserves +1 but I cannot do it as I need more reputation. In fact a clear answer were just there in the docs, my fault. Interesting that the zero definition overrides Java's "smartness" on this one. At least they left that option opened!Queston

© 2022 - 2024 — McMap. All rights reserved.