Creating a GridBagLayout
A gridbag layout is the most sophisticated layout manager in Java's UI
toolkit. This example demonstrates how to create and set a gridbag
layout and how to set gridbag constraints on components added to the
layout. See other examples for code that demonstrates the usage of
available gridbag constraints.
JFrame frame = new JFrame();
Container container = frame.getContentPane();
// Create the layout
GridBagLayout gbl = new GridBagLayout();
// Set layout on container
container.setLayout(gbl);
// Place a component at cell location (1,1)
GridBagConstraints gbc = new GridBagConstraints();
gbc.gridx = 1;
gbc.gridy = 1;
// Add other gridbag constraints here
// Associate the gridbag constraints with the component
gbl.setConstraints(component, gbc);
// Add the component to the container
container.add(component);
// Show the frame
frame.pack();
frame.setVisible(true);
Post a comment