3
votes

i was coding Address Book GUI using GridBagLayout . problem is that when i set gridx and gridy that does not work it looks as there is no layout used and components fall in center of the Frame . i am not pasting whole code just a small piece of code for 1 JButton to let you see what is happening .. please see the code and gudide me how to fix it , right now button is falling in center of the JFrame

I am just beginner so please dont mind if foolish style code or any problem like that

public class rect  
{
 JFrame frame ; 
 JPanel panel1 ; 
 JButton b1;
 GridBagConstraints gbc; 

 public  rect()
{
    panel1 = new JPanel();  
    panel1.setSize(300,300);
    panel1.setLayout(new GridBagLayout());
    gbc = new GridBagConstraints();

    frame= new JFrame("Address ?ooK "); 
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setSize(300, 300);


    b1= new JButton () ;
    b1.setText("Hello ");
    gbc.gridx=1; 
    gbc.gridy=0; 

    panel1.add(b1, gbc) ; 

    frame.add(panel1);
    panel1.setVisible(true);
    frame.setVisible(true);

}
3
Where is it supposed to be? Try to set weightx/weight to 1.0 and change anchor to GridBagLayout.NORTH... and see what happens ;-) I recommend to carefully read GBL documentation and tutorial.Guillaume Polet

3 Answers

1
votes

When setting

gbc.gridx = 1;
gbc.gridy = 0;

for GridBagConstraints, you are not setting absolute co-ordinates like you would if you using absolute positioning. Instead, you are setting the grid cell where the component will reside. In this sample application above, there is only one component so there is only one grid cell so the JButton will be center positioned by default.

To highlight this fact we could add set the anchor to NORTH and add another button, creating 2 grid cells:

gbc.anchor = GridBagConstraints.NORTH;
gbc.weightx = 1.0;
gbc.weighty = 1.0;

gbc.gridx = 1;
gbc.gridy = 0;

panel1.add(new JButton("Hello"), gbc);

gbc.gridx = 2; // now a 2nd cell exists
panel1.add(new JButton("Test"), gbc);

Notice we also set weightx and weighty so that the button could take their positions at the top of the JFrame:

GridBagLayout with 2 NORTH-Anchored Components

0
votes

There are some details missing from what your end goal is, but:

Your frame doesn't have a layout manager so your JPanel is going to end up in the middle of your frame. Whatever contents (e.g. JButtons) you add to the panel is going to be layed out according to GridBagLayout (but your frame doesn't know how to position your pane because your frame doesn't have a lay out manager). Think of your frame as a container, and your pane as another container.

Set a GridBagLayout for your frame similar to how you have it set up for your pane.


Also, your frame and your panel are set to exactly the same size -

panel1.setSize(300,300);
...
frame.setSize(300, 300);

You probably want to make your frame bigger to actually see the effects of panel1's positioning change according to the frame's layout manager.

0
votes

I would recommend using a different layout such as BorderLayout. For instance you can then

panel1 = new JPanel();  
panel1.setSize(300,300);
panel1.setLayout(new BorderLayout());

frame= new JFrame("Address ?ooK "); 
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(300, 300);
b1= new JButton ("Hello") ;

panel1.add(gbc) ; 

frame.add(panel1,BorderLayout.CENTER));
frame.add(b1,BorderLayout.PAGE_END));
panel1.setVisible(true);
frame.setVisible(true);