e

Writing Netrexx programs using Java Swing

Swing Components (JLabel)

Part 6

 
 
The label object provides text information on a GUI, it displays a single line of read-only text, an image or both text and image, we use a Swing jlabel when we need a user interface that displays a message or an image.

The various contructors can be seen below.:

    JLabel()
      creates a JLabel instance with no image and with empty string.
      
>>>> label = JLabel( )
      use label.setText( "my label" ) to add text
      use
label.setHorizontalAlignment( JLabel.LEFT )
      use
label.setVerticalAlignment( JLabel.CENTER )

    JLabel( Icon image )
      creates a JLabel instance with the specified image.
     
>>>> label = JLabel( ImageIcon( "ezm.gif" ) )  defaults to the center alignment ( JLabel.CENTER )

   
JLabel( Icon image, int horizontalAlignment )
      creates a JLabel instance with the specified image and the horizontal alighment.
     
>>>> label = JLabel( ImageIcon( "ezm.gif" ),  JLabel.LEFT )

   
JLabel( String text )
      creates a JLabel instance with the specified text.
      >>>> label = JLabel( "my label" )  defaults to the left alignment ( JLabel.LEFT )

   
JLabel( String text, Icon image, int horizontalAlignment )
      creates a JLabel instance with the specified text, image and horizontal alignment.
     
>>>> label = JLabel( "my txt & img label",  ImageIcon( "ezm.gif" ),  JLabel.RIGHT )

   
JLabel( String text, int horizontalAlignment )
      creates a JLabel instance with the specified text
and horizontal alignment.
     
>>>> label = JLabel( "my label",  JLabel.RIGHT ) 




Here are some of  the available methods for the Label Class.:

      setText() - getText()                                          

         Set or get the text in the label

      setIcon() - getIcon()                                           
         
Set or get the image displayed by the label

      setDisabledIcon() - getDisabledIcon()          
         Set or get the image displayed by the label when it is disabled. If one is not specified then a default is created.

      setHorizontalAlignment() -  getHorizonalAlignment()       
      setVerticalAlignment() - getVerticalAlignment()
          Set or get how the label's contents should be shown. The SwingConstants allows five values for the horizontal alignment, LEFT, CENTER, RIGHT, LEADING &
          TRAILING. For vertical alignment, TOP, CENTER & BOTTOM.

     
setHorizontalTextPosition() -  getHorizonalTextPosition()       
      setVertical
TextPosition() - getVerticalTextPosition()
         
Set or get where the label's text should be shown relative to the image. The SwingConstants allows five values for the horizontal alignment, LEFT, CENTER, RIGHT,
          LEADING & TRAILING. For vertical alignment, TOP, CENTER & BOTTOM.

Pls see Java documentation or Internet for a full list of methods available.

 
Changing colours and fonts:

Using HTML codes you can have multiple lines in a label,  multiple fonts, multiple colours etc...

Examples using HTML code:
      -- text using html center alligned
      htmllabel = JLabel( "<html>line 1<p><font color=blue size=+2>big blue</font> line 2<p>line 3</html>", JLabel.CENTER )
 

 
      -- text using html center alligned
      htmllabel  = JLabel( "<html><FONT COLOR=RED>Red</FONT> and <FONT COLOR=BLUE>Blue</FONT> Text</html>", JLabel.CENTER )

      -- text using html left alligned
      htmllabel2 = JLabel( "<html><B>Bold</B> and <I>Italic</I> Text</html>", JLabel.LEFT )

Below is the same example as the first but including the last two HTML examples.
  

  
If your label only needs one line and colour you can use the setForground() and setFont() properties to change these.   

Examples:
      label = JLabel( "eSimpleSoft" )
  
label.setForeground( Color.yellow )
      label.setFont(  Font( "Serif", Font.BOLD, 48) )





Example codes can be found here for standard labels, here for html examples and here for the last labels using the Java properties.

Back
Next