Swing doesn't respect my definition of GridLayout x Columns. How to fix?

I tried to solve this without success. How to make JPanel p

compliance certain GridLayout

? All I get is the first line containing 3 panels, but not 4 as I told Java to do.

What is the voodoo (or knowledge of my ignorance) required 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();

        }   
    }

      

Since this is my first StackOverflow question, I try to follow the rules strictly:

Be specific : I want to GridLayout

consider defining the columns of row x

What is my ultimate goal . Show 4 panels on the first line and subsequent panels on the second line.

Make it relevant to others . This is why this code is didactic, repeating all the panel declarations so that newbies (and myself) can understand the whole code and focus solely on the problem GridLayout

.

+3


source to share


1 answer


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

      

Specifying documents for constructor :

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

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

+10


source







All Articles