1

How do I use setRows and setColumns to change the amount of rows and columns in my window?

Here's a copy of my code and where I tried to implement the set rows and columns:

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;

import java.awt.Dimension;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Toolkit;
import java.awt.*;
@SuppressWarnings("unused")

public class GUI
{

    public static void main(String[] args)
    {
        new GUI();
    }


    public GUI()
    {

        JFrame AG = new JFrame("Adventure Game");
        AG.setExtendedState(JFrame.MAXIMIZED_BOTH);
        AG.setResizable(true);
        AG.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        JPanel p=new JPanel();

        p.setLayout (new GridBagLayout());

        AG.add(p);
        GridBagConstraints GBC = new GridBagConstraints();
        GridLayout.setRows(100);//heres the set rows<<<<<<<<<<<<<<<<<<<
        GridLayout.setColumns(100);//heres the set columns<<<<<<<<<<<<<<<<<<<
        AG.getContentPane().add(p, BorderLayout.NORTH);


        JButton saveButton =new JButton("Save");
        JButton loadButton =new JButton("Load");
        JButton optionsButton = new JButton("Options");

        JLabel textBox= new JLabel("Story line will go here.");

        JLabel label11 = new JLabel("Test 1");
        GBC.gridx = 0;
        GBC.gridy = 1;

        p.add(label11,GBC);
        JLabel label12 = new JLabel("Test 2");
        GBC.gridx = 0;
        GBC.gridy = 2;
        p.add(label12,GBC);
        JLabel label13 = new JLabel("Test 3");
        GBC.gridx = 0;
        GBC.gridy = 3;
        p.add(label13,GBC);
        JLabel label14 = new JLabel("Test 4");
        GBC.gridx = 0;
        GBC.gridy = 5;
        p.add(label14,GBC);

        AG.setVisible(true);
    }
}
3
  • 1
    What do you try to achive? You need to manipulate rows/columns with help of GridBagConstraints: gridx,gridy,gridwidth,gridheight
    – alex2410
    Nov 21, 2014 at 14:51
  • yeah I'm trying to work out how to lay out my buttons because i want for example to be able to have blank cells so i could have a button at 0,1 0,2 0,3 then miss one and 0,5
    – Tom T
    Nov 21, 2014 at 14:53
  • 1
    In that case I recommend you to add dummy label(new JLabel(" ");) to 0.4.
    – alex2410
    Nov 21, 2014 at 14:57

2 Answers 2

2

GridBagConstraints work with the GridBagLayout, not the GridLayout. Your call to GridLayout doesn't do anything.

To set the rows and columns with GridBagConstraints, you need to use GBC.gridx = ...; and GBC.gridy = ...;

2
  • thats what i have used but the amount of columns and rows is relative to the amount of objects so i want to predefine how many cells in the x and y axis
    – Tom T
    Nov 21, 2014 at 17:20
  • you cannot predefine row / column count in GridBagLayout... It makes no sense! Either you've got a component in the row/column or you don't, and if you don't GridBagLayout won't lay it out (ie 0 height & width) Nov 21, 2014 at 17:26
0

If you are trying to place gaps between your buttons, empty GridBag cells are not the way to do it; use the insets of your GridBagConstraints for that.

To make a component in a GridBagLayout disappear without the other components shifting around, place the button in a JPanel with a CardLayout, and make sure you add that JPanel to your GridBagLayout instead of the button:

private JButton optionalButton;

private void buildWindow() {
    // ...

    optionalButton = new JButton("Optional Action");

    JPanel optionalButtonPanel = new JPanel(new CardLayout());
    optionalButtonPanel.add(optionalButton, "BUTTON");
    optionalButtonPanel.add(new JLabel(), "BLANK");

    // ...
}

private void setOptionalButtonVisible(boolean visible) {
    Container optionalButtonPanel = optionalButton.getParent();
    CardLayout layout = (CardLayout) optionalButtonPanel.getLayout();
    layout.show(optionalButtonPanel, visible ? "BUTTON" : "BLANK");
}

CardLayout always displays one and only one of its components, but it sizes its associated container to fit all the components, including the invisible ones.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.