Animalz
Animalz

Reputation: 88

JFrame labels appear overlapped

i just programmed a program which has a JFrame containing an array of JLabel components. The array gets the position of the single labels assigned by a for-loop:

for(int i=0; i<label_entries.length; i++){
    label_entries[i].setLocation(10, i*30);
    label_entries[i].setSize(120,30);
    dialog.add(label_entries[i]);   
}

Do not get confused, my frame's name is "dialog".

There is one simple problem: The for-loop doesn't work like a for-loop should, and I don't know why, here is the result in my frame:

Imgur.

Don't care about the single label entries, the interesting thing is the position of Telefon.

If I set the beginning of the loop to the following, it is the same problem, just with another label.

for(int i=0; i<label_entries.length-1; i++){...}

Here is the full code

JFrame dialog = new JFrame();
dialog.setBounds(25, 50, 500, 500);
dialog.setTitle("Eintrag hinzufügen");
dialog.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
dialog.setVisible(true);

JLabel[] label_entries = new JLabel[11];
JTextField[] textfields = new JTextField[11];
label_entries[0] = new JLabel("Vorname :");
label_entries[1] = new JLabel("Nachname :");
label_entries[2] = new JLabel("Nummer :");
label_entries[3] = new JLabel("Geburtstag :");
label_entries[4] = new JLabel("Land :");
label_entries[5] = new JLabel("PLZ :");
label_entries[6] = new JLabel("Stadt :");
label_entries[7] = new JLabel("Strasse :");
label_entries[8] = new JLabel("Hausnummer :");
label_entries[9] = new JLabel("E-Mail :");
label_entries[10] = new JLabel("Telefon :");

for(int i=0; i<label_entries.length; i++){
    label_entries[i].setLocation(10, i*30);
    label_entries[i].setSize(120,30);
    dialog.add(label_entries[i]);   
}

This should be easier to understand.

Upvotes: 1

Views: 52

Answers (2)

Andreas
Andreas

Reputation: 159096

As the javadoc of JFrame says:

The default content pane will have a BorderLayout manager set on it.

Javadoc of BorderLayout says:

A border layout lays out a container, arranging and resizing its components to fit in five regions: north, south, east, west, and center. Each region may contain no more than one component, and is identified by a corresponding constant: NORTH, SOUTH, EAST, WEST, and CENTER. When adding a component to a container with a border layout, use one of these five constants, for example:

Panel p = new Panel();
p.setLayout(new BorderLayout());
p.add(new Button("Okay"), BorderLayout.SOUTH);

As a convenience, BorderLayout interprets the absence of a string specification the same as the constant CENTER:

Panel p2 = new Panel();
p2.setLayout(new BorderLayout());
p2.add(new TextArea());  // Same as p.add(new TextArea(), BorderLayout.CENTER);

Since you call the 1-arg version of add(), all your JLabels are added with BorderLayout.CENTER, and so the last one wins, and the BorderLayout manager then auto-positions it at left-center.

To prevent that from happening, just remove the layout manager:

dialog.setLayout(null);

Upvotes: 1

Tom Hawtin - tackline
Tom Hawtin - tackline

Reputation: 147164

One obvious problem is that you are setting absolute positions of your components. Typically a LayoutManager is used for this purpose.

To clear whatever the default layout manager of a JFrame's content pane is, set it to null just after creating the frame.

JFrame dialog = new JFrame();
dialog.setLayout(null);

Upvotes: 2

Related Questions