Problem when inserting 2 labels into JApplet

Hi, people!

I am having a big headache with JApplet/Swing. Whenever i create 2 labels into the Japplet' default pane, only the second one is showed! Why? Whats wrong?

Regards, Euclides.

My code follows below:

import java.awt.*;

import javax.swing.*;

import java.util.*;

public class PaineleLabel extends JApplet {

// by Euclides - nov. 2001

public void init() {

// criei e adicionei um label !

JLabel jl = new JLabel("texto pequeno");

jl.setVerticalAlignment( JLabel.CENTER );

// criei um novo label

JLabel jla = new JLabel("Texto Grande");

Font tg = new Font("Arial", Font.BOLD,23);

jla.setFont(tg);

getContentPane().add(jl);

getContentPane().add(jla);

}

[785 byte] By [euclidesjr] at [2007-9-27 14:53:04]
# 1

I think the default layout for contentPane is BorderLayout i.e. the pane is split into 5 section - North, South, East, West, Center. As You ahve not specified in which area to add the labels, it is probably the case that one being placed on top of the other This should work.

import java.awt.*;

import javax.swing.*;

import java.util.*;

public class PaineleLabel extends JApplet {

public void init()

{

Container c = getContentPane();

c.setLayout(new BorderLayout());

JLabel jl = new JLabel("texto pequeno");

jl.setVerticalAlignment( JLabel.CENTER );

JLabel jla = new JLabel("Texto Grande");

Font tg = new Font("Arial", Font.BOLD,23);

jla.setFont(tg);

c.add(jl, BorderLayout.NORTH);

c.add(jla, BorderLayoout.SOUTH);

}

Alternately, set the Layout to FlowLayout and components are added left to right until there is no more space and the next one is added below . Or, you can add your Labels to a JPanel and then add this to the Container.

eg.

// Panel with two rows and one column

JPanel pan = new JPanel(new GridLayout(2,1,10,10));

pan.add(1stLabel);

pan.add(2ndLabel);

c.add(pan, BorderLayout.CENTER);

There's loads of possibilities. For more advanced GUIs (unless you want to use the dreaded GridBag Layout) you'll end up placing panels inside panels inside panels to get the look you want. Have a look at the tutorial on Layout managers.

alhay99 at 2007-7-5 22:53:14 > top of Java-index,Archived Forums,Swing...
# 2
Exactumundo! Or, you can go get the TableLayout component and specify the exact position to display it in in a much more controlled manner.
buckman1 at 2007-7-5 22:53:14 > top of Java-index,Archived Forums,Swing...
# 3
thanks
euclidesjr at 2007-7-5 22:53:14 > top of Java-index,Archived Forums,Swing...