GridLayout does not leave room between edge of window and objects.

J

Jenny

Hi,

According to API,

public GridLayout(int rows,
int cols,
int hgap,
int vgap)Creates a grid layout with the specified
number of rows and columns. All components in the layout are given
equal size.
In addition, the horizontal and vertical gaps are set to the specified
values. Horizontal gaps are placed at the left and right edges, and
between each of the columns. Vertical gaps are placed at the top and
bottom edges, and between each of the rows.

But if you run the code below, GridLayout does not leave room between
edge of window and objects. Is this a bug? Thanks.

import javax.swing.*;
import java.awt.*;
public class WindowGridLayout {
public static void main(String[] args) {
JFrame f = new JFrame("hello");
f.setSize(400, 400);
f.setLocation(100, 100);
JLabel u= new JLabel("User Name");
JTextField user = new JTextField(8);
JLabel p= new JLabel("Password");
JPasswordField password = new JPasswordField(10);
password.setEchoChar('#');
GridLayout grd = new GridLayout(3,2,50,50);
Container pane = f.getContentPane();
pane.setLayout(grd);
pane.add(u);
pane.add(user);
pane.add(p);
pane.add(password);
JTextArea a = new JTextArea(3,8);
a.insert("test \none",0);
pane.add(a);
f.setVisible(true);
}}
 
F

Filip Larsen

Jenny wrote
According to API,

public GridLayout(int rows,
int cols,
int hgap,
int vgap)Creates a grid layout with the specified
number of rows and columns. All components in the layout are given
equal size.
In addition, the horizontal and vertical gaps are set to the specified
values. Horizontal gaps are placed at the left and right edges, and
between each of the columns. Vertical gaps are placed at the top and
bottom edges, and between each of the rows.

But if you run the code below, GridLayout does not leave room between
edge of window and objects. Is this a bug? Thanks.

No, the GridLayout is working as described in the API so its not a bug.
Usually you want panel layout managers to only care about the internal
layout (thats almost by definition). Any spacing and placement around a
panel are handled by the placement of this panel into its parent
container.

However, with Swing you get the option of placing a (possibly empty)
border on every JComponent and since the default content pane delegated
from JRootPane in the JFrame is a JPanel, you can place a border
directly on that. In the context of your code that could look like

JPanel panel = (JPanel) f.getContentPane();
panel.setBorder(BorderFactory.createEmptyBorder(5,5,5,5));


May I also add that GridLayout are not very useful for laying out input
forms like the one you seem to be building. For that purpose I recommend
that you look into GridBagLayout, SpringLayout or some of the nice
third-party layout managers out there. These are more complicated to
use, but with them you can solve great many layout problems. You can
also try find a GUI-builder with a form layout wizzard that can assist
you building the form.


Regards,
 
J

Jenny

Thanks a lot. So

"Horizontal gaps are placed at the left and right edges, and
between each of the columns. Vertical gaps are placed at the top and
bottom edges, and between each of the rows."

means in a panal, not in JFrame.

Could you look at anthoer question I posted on 8/3/04 under replaceRange?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,764
Messages
2,569,567
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top