views:

359

answers:

3

I put my buttons in a JPane with GridLayout. Then I put JPanel into another JPanel with BoxLayout.Y_AXIS. I want buttons in the GridLayout to be square. I use tmp.setSize(30,30) and it does not work. I also try to use new GridLayout(X, Y, 4, 4) but I cannot figure out what X and Y are. So, what is the correct way to do this stuff?

ADDED:

I still cannot solve the problem. Here is the code of what I am trying to do:

import javax.swing.*;
import java.awt.*;

public class PanelModel {
    public static void main(String[] args) {
        JFrame frame = new JFrame("Colored Trails");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        JPanel mainPanel = new JPanel();
        mainPanel.setLayout(new BoxLayout(mainPanel, BoxLayout.Y_AXIS));

        JPanel firstPanel = new JPanel();
        firstPanel.setLayout(new GridLayout(4, 4));
        JButton btn;
        for (int i=1; i<=4; i++) {
            for (int j=1; j<=4; j++) {
                btn = new JButton();
                btn.setPreferredSize(new Dimension(100, 100));
                firstPanel.add(btn);
            }
        }

        JPanel secondPanel = new JPanel();
        secondPanel.setLayout(new GridLayout(5, 13));
        for (int i=1; i<=5; i++) {
            for (int j=1; j<=13; j++) {
                btn = new JButton();
                btn.setPreferredSize(new Dimension(40, 40));
                secondPanel.add(btn);
            }
        }

        mainPanel.add(firstPanel);
        mainPanel.add(secondPanel);
        frame.add(mainPanel);

        frame.setSize(400,600);
        frame.setVisible(true);
    }
}

The problem is that Java tries to make width of the firstPanel and secondPanel equal! Moreover, Java tries to to fill all height of the window. How can I remove this behavior?

+2  A: 

Try with setPreferredSize instead of setSize.

UPDATE: GridLayout take up all space in its container, and BoxLayout seams to take up all the width in its container, so I added some glue-panels that are invisible and just take up space when the user stretches the window. I have just done this horizontally, and not vertically, but you could implement that in the same way if you want it.

Since GridLayout make all cells in the same size, it doesn't matter if they have a specified size. You have to specify a size for its container instead, as I have done.

import javax.swing.*;
import java.awt.*;

public class PanelModel {
    public static void main(String[] args) {
        JFrame frame = new JFrame("Colored Trails");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        JPanel mainPanel = new JPanel();
        mainPanel.setLayout(new BoxLayout(mainPanel, BoxLayout.Y_AXIS));

        JPanel firstPanel = new JPanel(new GridLayout(4, 4));
        firstPanel.setPreferredSize(new Dimension(4*100, 4*100));
        for (int i=1; i<=4; i++) {
            for (int j=1; j<=4; j++) {
                firstPanel.add(new JButton());
            }
        }

        JPanel firstGluePanel = new JPanel(new BorderLayout());
        firstGluePanel.add(firstPanel, BorderLayout.WEST);
        firstGluePanel.add(Box.createHorizontalGlue(), BorderLayout.CENTER);
        firstGluePanel.add(Box.createVerticalGlue(), BorderLayout.SOUTH);

        JPanel secondPanel = new JPanel(new GridLayout(13, 5));
        secondPanel.setPreferredSize(new Dimension(5*40, 13*40));
        for (int i=1; i<=5; i++) {
            for (int j=1; j<=13; j++) {
                secondPanel.add(new JButton());
            }
        }

        JPanel secondGluePanel = new JPanel(new BorderLayout());
        secondGluePanel.add(secondPanel, BorderLayout.WEST);
        secondGluePanel.add(Box.createHorizontalGlue(), BorderLayout.CENTER);
        secondGluePanel.add(Box.createVerticalGlue(), BorderLayout.SOUTH);

        mainPanel.add(firstGluePanel);
        mainPanel.add(secondGluePanel);
        frame.getContentPane().add(mainPanel);

        //frame.setSize(400,600);
        frame.pack();
        frame.setVisible(true);
    }
}
Jonas
Compiler writes that it cannot find method called `setPreferredSize`.
Roman
+2  A: 

The following bit of code does what you ask for. Just make sure that you assign enough space so that the text on the button becomes visible

    JFrame frame = new JFrame("test");
    frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
    JPanel panel = new JPanel(new GridLayout(4,4,4,4));

    for(int i=0 ; i<16 ; i++){
        JButton btn = new JButton(String.valueOf(i));
        btn.setPreferredSize(new Dimension(40, 40));
        panel.add(btn);
    }
    frame.setContentPane(panel);
    frame.pack();
    frame.setVisible(true);

The X and Y (two first parameters of the GridLayout constructor) specify the number of rows and columns in the grid (respectively). You may leave one of them as 0 if you want that value to be unbounded.

Edit

I've modified the provided code and I believe it now conforms to what is desired:

    JFrame frame = new JFrame("Colored Trails");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    JPanel mainPanel = new JPanel();
    mainPanel.setLayout(new BoxLayout(mainPanel, BoxLayout.Y_AXIS));

    JPanel firstPanel = new JPanel();
    firstPanel.setLayout(new GridLayout(4, 4));
    firstPanel.setMaximumSize(new Dimension(400, 400));
    JButton btn;
    for (int i=1; i<=4; i++) {
        for (int j=1; j<=4; j++) {
            btn = new JButton();
            btn.setPreferredSize(new Dimension(100, 100));
            firstPanel.add(btn);
        }
    }

    JPanel secondPanel = new JPanel();
    secondPanel.setLayout(new GridLayout(5, 13));
    secondPanel.setMaximumSize(new Dimension(520, 200));
    for (int i=1; i<=5; i++) {
        for (int j=1; j<=13; j++) {
            btn = new JButton();
            btn.setPreferredSize(new Dimension(40, 40));
            secondPanel.add(btn);
        }
    }

    mainPanel.add(firstPanel);
    mainPanel.add(secondPanel);
    frame.setContentPane(mainPanel);

    frame.setSize(520,600);
    frame.setMinimumSize(new Dimension(520,600));
    frame.setVisible(true);

Basically I now set the preferred size of the panels and a minimum size for the frame.

Kris
I tried this code. It works. But if I try to implement the same idea in my program, it does not work. I think that the reason is that I include my JPanel (with the GridLayout) to another JPanel (with BoxLayout.Y_AXIS).
Roman
@Roman If you want more specific help you'll need to post some sample code!
Kris
@Kris, as requested, I posted the code.
Roman
+2  A: 

GridLayout is often not the best choice for buttons, although it might be for your application. A good reference is the tutorial on using Layout Managers. If you look at the GridLayout example, you'll see the buttons look a little silly -- way too big.

A better idea might be to use a FlowLayout for your buttons, or if you know exactly what you want, perhaps a GroupLayout. (Sun/Oracle recommend that GroupLayout or GridBag layout are better than GridLayout when hand-coding.)

John