



























Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
Community
Ask the community for help and clear up your study doubts
Discover the best universities in your country according to Docsity users
Free resources
Download our free guides on studying techniques, anxiety management strategies, and thesis advice from Docsity tutors
An overview of gridbaglayout, a powerful and flexible layout manager in java for creating complex guis. It explains the concept of gridbaglayout, the use of gridbagconstraints, and how to arrange components using gridx and gridy parameters. The document also covers the concept of preferred sizes, fill property, and anchor property, and how to make components span multiple cells using gridwidth and gridheight. Additionally, it discusses the importance of minimum, preferred, and maximum sizes, and how to make components fill the entire grid cell. Lastly, it touches upon the use of insets for adding space around components and combining different layout managers in a single window.
Typology: Quizzes
1 / 35
This page cannot be seen from the preview
Don't miss anything!
java.awt.GridBagLayout
is the most powerful and
fl exible layout manager that comes with Java (but there are
GridBagLayout many third-party layout managers available.)
gets its name from its use of a rectangular
GridBagLayout multiple grid squares.not have to be the same size, and the cells can take upgrid of cells. The “bag” part means that the grid squares do
uses a helper class
java.awt.GridBagConstraints
which holds the 11 (!)
parameters that control how
GridBagLayout
sets up a
component.
The 11 parameters are:
gridx
gridy
gridwidth
gridheight
weightx
weighty
anchor
fill
insets
ipadx
, and
ipady
The rest of the lecture will explain what these mean.
manager isrows. If we set up the window with a content panel whose layoutIt has a natural resemblance to a table with 2 columns and 4
GridBagLayout
, we can use these row and column
They are thenumbers to cause it to arrange the components like the above.
gridx
and
gridy
parameters of
GridBagConstraints
Rectangle Info
// ...p.add(yTextField, gbc);gbc.gridx = 2;JTextField yTextField = new JTextField(10);p.add(yCaption, gbc);gbc.gridy = 2;gbc.gridx = 1;JLabel yCaption = new JLabel("Y:");p.add(xTextField, gbc); // Reusing gbc ok because fields copied by layoutgbc.gridx = 2; // Keeping gridy from aboveJTextField xTextField = new JTextField(10);p.add(xCaption, gbc);gbc.gridy = 1;gbc.gridx = 1;JLabel xCaption = new JLabel("X:");JPanel p = new JPanel(gbl);GridBagConstraints gbc = new GridBagConstraints();GridBagLayout gbl = new GridBagLayout();
To
fi nd the grid, we look for the boundary lines bet ween
components. Each column and each row in
GridBagLayout
boundary lines cut through other components. extends the entire height or width of the panel. Some of the
Rectangle Info
quite what we wanted. By putting the widthWhen we display it on the screen, it looks like this, which isn't
JTextField
in cell
(2,2)
, we left cells
(3,2)
and
(4,2)
open. Fortunately,
GridBagLayout
allows a component to span multiple cells using
the
gridwidth
and
gridheight
parameters.
Rectangle Info
Now we get something like this. What's happening?
Rectangle Info
actually use.The maximum size is the most space the component canlayout.manager will usually use the preferred size to do thewhen it isn't being stretched or compressed. A layoutThe preferred size is the “normal” size for the componentcan be compressed and still function properly.The minimum size indicates the smallest the component
JComponent
has methods for getting/setting these:
being used and the conpreferred, and maximum sizes based on the look-and-feel Most components will supply their own values for minimum,
fi guration of the component.
JTextField
uses the number of columns parameter you
passed to the constructor to determine its preferred width.
fi Because we used a gridwidth of 3, the cell containing the text elds spans 3 columns, but the text
fi eld inside it doesn't
fi ll the
there is a way to getentire cell because the cell is larger than its preferred width. But
GridBagLayout
to override the preferred
size and stretch the component to
fi t the grid cell.
Rectangle Info
// ...p.add(widthTextField, gbc);gbc.fill = GridBagConstraints.HORIZONTAL;gbc.gridwidth = 3; // Take up (2,2), (3,2), and (4,2)gbc.gridx = 2;JTextField widthTextField = new JTextField(10);p.add(widthCaption, gbc);gbc.gridy = 2;gbc.gridx = 1;JLabel widthCaption = new JLabel("Width:");// ...
lined up properly next to the textNow it looks like this. It would be nice if the captions were all
fi elds rather than centered over
each other. The
JLabel
s don't
fi ll up their entire cells, but if we
stretch the component to
fi ll the grid cell, the component will add
change the alignment of the component within the cell.its own empty space around the text. What we want to do is
Rectangle Info
// ...p.add(widthTextField, gbc);gbc.fill = GridBagConstraints.HORIZONTAL;gbc.gridwidth = 3; // Take up (2,2), (3,2), and (4,2)gbc.gridx = 2;JTextField widthTextField = new JTextField(10);p.add(widthCaption, gbc);gbc.anchor = GridBagConstraints.EAST;gbc.gridy = 2;gbc.gridx = 1;JLabel widthCaption = new JLabel("Width:");// ...
Now the captions are aligned.
Rectangle Info