0

I am relatively new to Java Swing and I am having a little trouble understanding how Grid layouts can do certain things and if they can't, then how the gridbag layout, which is supposedly more powerful can do that.

Here is a program i tried with Grid layout

import javax.swing.*;
import java.awt.*;
//import java.awt.event.*;
public class Swing24
{
public static void main(String[] args)
{
JFrame f1= new JFrame("Grid Layout Test");

f1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f1.setResizable(true);
f1.setLocation(500,200);
f1.setSize(600,600);

JPanel p1 = new JPanel();
p1.setBackground(Color.black);
f1.add(p1);


JButton b1= new JButton("Button 1");
b1.setBackground(Color.white);

JButton b2= new JButton("Button 2");
b2.setBackground(Color.white);

JButton b3= new JButton("Button 3");
b3.setBackground(Color.white);

JLabel lb1=new JLabel(" Label 1");
lb1.setForeground(Color.orange);
//lb1.setOpaque(true);
lb1.setBackground(Color.yellow);

JLabel lb2=new JLabel(" Label 2");
lb2.setBackground(Color.orange);
lb2.setOpaque(true);


GridLayout glm1=new GridLayout(2,3,0,0); 
p1.setLayout(glm1);

p1.add(b1);
p1.add(b2);
p1.add(b3);
p1.add(lb1);
p1.add(lb2);


f1.setVisible(true);

}
}

The above program allows me divide the container into 2 rows and 3 columns. Basically I can divide a container into m rows and n columns with a grid layout. But it adds the components(the butons and labels) serially.

Question 1: How can I directly add a button to the cell(4,3) in a grid of size(10,10)? Question 2: Can a button occupy multiple cells in a grid layout?

If the answer to any of the above is not possible, then how can gridbag layout help solve the problem. I tried using gridbag layout with a button. But it gets placed in the center! How can I, say, place it to the cell(4,3) in a container which can be divided into size(10,10)<10 rows and 10 columns>

user3015246
  • 139
  • 1
  • 2
  • 9

1 Answers1

1

1) You can't add component to specific cell,but inthat question you can find some type of trick for that.

2)Here is another trick with nested lyout inside cells, for merging.

You can do all what you want with help of GridBagLayout. Watch GridBagConstraints it helps you to layout components properly.

See properties of GridBagConstraints:

gridwidth, gridheight, gridx, gridy, anchor.

But you would need some trick with empty spaces around cell(4,3) , if you want to add only one component to your container.

Also read tutorial for GridBagLayout.

EDIT: you can try something like this

public class Form extends JFrame {

    public Form() {
        getContentPane().setLayout(new GridBagLayout());
        GridBagConstraints c = new GridBagConstraints();
        c.gridx = 0;
        c.gridy = 0;
        c.weightx = 1;
        c.weighty = 1;
        for(int i =0;i<10;i++){
            c.gridx = i;
            for(int j =0;j<10;j++){
                c.gridy = j;
                if(i == 3 && j == 2){
                    c.fill = GridBagConstraints.NONE;
                    getContentPane().add(new JButton("btn"),c);
                } else {
                    c.fill = GridBagConstraints.BOTH;
                    JPanel p = new JPanel();
                    p.setBorder(BorderFactory.createLineBorder(Color.red));
                    getContentPane().add(p,c);
                }
            }
        }

        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        pack();
        setLocationRelativeTo(null);
    }


    public static void main(String[] args) throws Exception {
        SwingUtilities.invokeLater(new Runnable() {

            public void run() {
                new Form().setVisible(true);
            }
        });
    }   
}

enter image description here

EDIT2: It's not realy cell(4,3) but in same proportions

public Form() {
    getContentPane().setLayout(new GridBagLayout());
    GridBagConstraints c = new GridBagConstraints();
    c.gridx = 0;
    c.gridy = 0;
    c.weightx = 0.2;
    c.weighty = 0.3;
    c.fill = GridBagConstraints.BOTH;
    getContentPane().add(new JLabel(" "),c);

    c.gridx++;
    c.gridy++;
    c.fill = GridBagConstraints.NONE;
    getContentPane().add(new JButton("btn"),c);

    c.weightx = 0.7;
    c.weighty = 0.6;
    c.gridx++;
    c.gridy++;
    c.fill = GridBagConstraints.BOTH;
    getContentPane().add(new JLabel(" "),c);

    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    pack();
    setLocationRelativeTo(null);
}

or real cell(4,3), but more then 3 components and less then 100:

public Form() {
    getContentPane().setLayout(new GridBagLayout());
    GridBagConstraints c = new GridBagConstraints();
    c.gridx = 0;
    c.gridy = 0;
    c.weightx = 1;
    c.weighty = 1;
    for(int i =0;i<2;i++){
        getContentPane().add(new JLabel(" "),c);
        c.gridx++;
    }

    for(int i =0;i<3;i++){
        getContentPane().add(new JLabel(" "),c);
        c.gridy++;
    }

    c.gridx = 3;
    c.gridy = 4;
    getContentPane().add(new JButton("btn"),c);

    for(int i =0;i<7;i++){
        getContentPane().add(new JLabel(" "),c);
        c.gridx++;
    }

    for(int i =0;i<6;i++){
        getContentPane().add(new JLabel(" "),c);
        c.gridy++;
    }
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    pack();
    setLocationRelativeTo(null);
}
Community
  • 1
  • 1
alex2410
  • 10,904
  • 3
  • 25
  • 41
  • Thanks a lot for your answer. I will come back to grid layout later, but can you explain the grid bag layout in a little more detail?I did read the oracle website tutorial. I only asked because it wasn'tvery clear to me. Perhaps a little feedback in the form of conversation will be better. – user3015246 Nov 25 '13 at 14:01
  • Perhaps an example of a code that uses only one button in gridbag layout and places the button in cell (4,3) of grid size (10,10)? – user3015246 Nov 25 '13 at 14:05
  • Thanks a lot for your edit. I think this program creates a 100 panels and then places a button on the panel at (4,3). But isn't there a way work with just one or 2 components? You mentioned something about spacing trick. The reason I want to work with a small number of components and directly manipulate them is because I want to think from point of view f JVM and understand how exactly c.gridx,c.gridy,c.weightx etc work. And why do they all get placed towards the center. How come your panel(0,0) was able to sit at the top left corner, but my one gets placed at the center even with gridx=0? – user3015246 Nov 25 '13 at 15:25
  • Later I give you an example with 3 components , you can try to do it, by yourself you need to use weightx and weighty properties. Haven't IDE at my phone:) – alex2410 Nov 25 '13 at 15:30
  • OK, thanks and I will wait. Till then, I will keep experimenting. I am having trouble understanding the meaning of weightx and weighty. – user3015246 Nov 25 '13 at 15:43
  • Thanks again for your last edit and sorry for not being able to reply earlier. I checked your code and they seem to work in my IDE. I think I am getting an idea about how the grids are formed - you need at least n components to form an nxn grid- but a lot of things about gridbag layout remains unclear. I may ask another question soon and inform here. – user3015246 Nov 26 '13 at 09:23
  • Empty components need if you want to fill only one cell, if you will construct interface with more components it will be easier – alex2410 Nov 26 '13 at 09:26