1 Attachment(s)
Problem with GridBagLayout.
Hi,
I'm using the GridBagLayout manager and i want to add my first button at the left corener of my frame (0,0).
The button locate him self to the center of my frame, and i dont know how to move it to the left corener.
This is what i want to do :
Attachment 3823
I want to locate the button at the red place.
This is my code :
Code:
import javax.swing.*;
import java.awt.*;
public class Display {
private JFrame frame;
private JPanel panel;
private JList<String> list;
public Display()
{
frame = new JFrame();
panel = new JPanel(new GridBagLayout());
list = new JList<String>();
frame.setVisible(true);
frame.setSize(600,600);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
GridBagConstraints grid = new GridBagConstraints();
//Set the spaces between the components and order them VERTICAL.
grid.fill = GridBagConstraints.VERTICAL;
grid.insets = new Insets(15,15,15,15);
int day_of_week = 7;
for(int i=0;i<28;i++)
{
grid.gridx = i % 7;
grid.gridy = i / 7 ;
list = new JList<String>();
panel.add(list,grid);
}
frame.add(panel);
}
}
How do i use the GridBagLayout to locate the button there?
Thanks alot,
Or.
Re: Problem with GridBagLayout.
You're not setting all the pertinent fields of the GridBagConstraints object. Look at the API and the tutorial. For example you'll want to set the weight constraints among others.