Docsity
Docsity

Prepare for your exams
Prepare for your exams

Study with the several resources on Docsity


Earn points to download
Earn points to download

Earn points by helping other students or get them with a premium plan


Guidelines and tips
Guidelines and tips

GridBagLayout in Java: Creating Flexible GUIs with GridBagConstraints - Prof. Douglas J. W, Quizzes of Computer Science

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

Pre 2010

Uploaded on 07/22/2009

koofers-user-5d0
koofers-user-5d0 🇺🇸

10 documents

1 / 35

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
CS251 Intermediate Programming
Lecture 13
GUIs #5
• Midterm next Wednesday.
• Homework 3 due Monday.
• Quiz 3 today.
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15
pf16
pf17
pf18
pf19
pf1a
pf1b
pf1c
pf1d
pf1e
pf1f
pf20
pf21
pf22
pf23

Partial preview of the text

Download GridBagLayout in Java: Creating Flexible GUIs with GridBagConstraints - Prof. Douglas J. W and more Quizzes Computer Science in PDF only on Docsity!

CS251 Intermediate Programming

Lecture 13

GUIs

  • Midterm next Wednesday.• Homework 3 due Monday.• Quiz 3 today.

GridBagLayout

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

Height:

Y:^ X:

Width:

// ...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

Y:

Height:

X:

Width:

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

Y:

Height:

X:

Width:

Now we get something like this. What's happening?

Rectangle Info

Y:

Height:

X:

Width:

size. Each component has a minimum, preferred, and maximumHow Components Help the Layout Managers

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:

Preferred Sizes

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

Y:

Height:

X:

Width:

// ...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

Y:

Height:

X:

Width:

// ...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

Y:

Height:

X:

Width: