Reputation: 117
I cant transfer any of the relatet posts to my simple problem. In order to get into GridBagLayout i wrote a simple example:
JFrame frame = new JFrame();
JPanel main =new JPanel();
frame.setContentPane(main);
GridBagLayout gbl=new GridBagLayout();
GridBagConstraints gbc = new GridBagConstraints();
main.setLayout(gbl);
JButton btn1 = new JButton("1");
JButton btn2 = new JButton("2");
gbc.gridx=0;
gbc.gridy=0;
gbc.gridheight=1;
gbc.gridwidth=1;
gbc.fill=GridBagConstraints.WEST;
gbl.setConstraints(btn1, gbc);
gbc.gridx=0;
gbc.gridy=1;
gbl.setConstraints(btn2, gbc);
frame.setSize(new Dimension(200,100));
main.add(btn1);
main.add(btn2);
frame.setVisible(true);
Here i have the Problem that neither .fill nor any other parameter of GBConstrains sems to work. I ever get the two buttons in the middle of the window.
thx in advance
Upvotes: 0
Views: 1007
Reputation: 47608
Your configuration of your GridBagConstraint
are incorrect:
fill
can only take: NONE
, VERTICAL
, HORIZONTAL
or BOTH
.anchor
can use relative location within a "cell"fill
and anchor
almost always require to use weightx/weighty
gridx
and gridy
because they get hard to maintain. Instead use their default value (RELATIVE
) and you can change the value of gridwidth
/gridheight
to set it to 1
(or more if needed) and REMAINDER
to make the layout wrap to next line/column (components are then positionned in the order they were added to the container.Here is a working code of yours (although I am not sure of the exact layout you were targetting):
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
public class TestGBL {
protected void initUI() {
JFrame frame = new JFrame();
GridBagLayout gbl = new GridBagLayout();
JPanel main = new JPanel(gbl);
frame.setContentPane(main);
GridBagConstraints gbc = new GridBagConstraints();
gbc.anchor = GridBagConstraints.WEST;
gbc.weightx = 1.0;
main.setLayout(gbl);
JButton btn1 = new JButton("1");
JButton btn2 = new JButton("2");
gbl.setConstraints(btn1, gbc);
gbl.setConstraints(btn2, gbc);
main.add(btn1);
main.add(btn2);
frame.pack();
frame.setVisible(true);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
new TestGBL().initUI();
}
});
}
}
Upvotes: 2
Reputation: 827
Edit: I made a sample application that demonstrates how to use GridBadLayout that is reusing the constraints:
import java.awt.EventQueue;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import javax.swing.JButton;
import javax.swing.JFrame;
public class Test1 {
private JFrame frame;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
Test1 window = new Test1();
window.frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the application.
*/
public Test1() {
initialize();
}
/**
* Initialize the contents of the frame.
*/
private void initialize() {
frame = new JFrame();
frame.setBounds(100, 100, 450, 300);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
GridBagLayout gridBagLayout = new GridBagLayout();
gridBagLayout.columnWidths = new int[]{0, 0, 0};
gridBagLayout.rowHeights = new int[]{0, 0};
gridBagLayout.columnWeights = new double[]{0.0, 0.0, Double.MIN_VALUE};
gridBagLayout.rowWeights = new double[]{0.0, Double.MIN_VALUE};
frame.getContentPane().setLayout(gridBagLayout);
JButton btnTest = new JButton("Test1");
GridBagConstraints gbc_btnTest = new GridBagConstraints();
gbc_btnTest.insets = new Insets(0, 0, 0, 5);
gbc_btnTest.gridx = 0;
gbc_btnTest.gridy = 0;
frame.getContentPane().add(btnTest, gbc_btnTest);
JButton btnTest_1 = new JButton("Test2");
gbc_btnTest.gridx = 1;
gbc_btnTest.gridy = 0;
frame.getContentPane().add(btnTest_1, gbc_btnTest);
}
}
Upvotes: 1