Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / GUI / October 2007

Tip: Looking for answers? Try searching our database.

JLebel as a container

Thread view: 
Chanchal - 26 Oct 2007 09:55 GMT
Hi All,

I'm trying to add a JLable 'jLable2 to another JLabel 'jLabel1'. But
jLabel2 is not getting displayed.

My code is

import javax.swing.*;

public class LabelTest extends JFrame {

   public LabelTest(){

setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
       JLabel jLabel1 = new javax.swing.JLabel();
       jLabel1.setText("jLabel1");
       JLabel jLabel2 = new JLabel();
       jLabel2.setText(" TEXT ");
       jLabel1.add(jLabel2);
       getContentPane().add(jLabel1, java.awt.BorderLayout.CENTER);
       pack();
   }

   public static void main(String args[]) {
    LabelTest lTest = new LabelTest ();
    lTest.setSize(400,300);
    lTest.setVisible(true);
   }

}

Please advice on how i can make jLabel2 visible.
Thanks in advance

Chanchal
Andrew Thompson - 26 Oct 2007 10:41 GMT
...
>I'm trying to add a JLable 'jLable2 to another JLabel 'jLabel1'.

Why?

>..But
>jLabel2 is not getting displayed.

Try this variant that adds both JLabels directly to the JFrame.
If that is not the effect you are after, there are many alternatives,
including adding them both to a JPanel that is itself added to
one particular area of the BorderLayout of the JFrame.

<sscce>
import java.awt.BorderLayout;
import javax.swing.*;

public class LabelTest extends JFrame {

 public LabelTest(){

   setDefaultCloseOperation(
     WindowConstants.EXIT_ON_CLOSE);
   JLabel jLabel1 = new JLabel();
   jLabel1.setText("jLabel1");
   JLabel jLabel2 = new JLabel();
   jLabel2.setText(" TEXT ");
   //jLabel1.add(jLabel2);
   getContentPane().add(
     jLabel1, BorderLayout.CENTER);
   getContentPane().add(
     jLabel2, BorderLayout.EAST);
   pack();
 }

 public static void main(String args[]) {
   LabelTest lTest = new LabelTest ();
   lTest.setSize(400,300);
   lTest.setVisible(true);
 }
}
</sscce>

Signature

Andrew Thompson
http://www.athompson.info/andrew/



Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.