Back to Buoy resources page

BLabel

BLabels are widgets that show a piece of information to the user. They cannot be changed by the user and do not ask for input. They can be used to state what value should be entered in a text field, or display the app version in an About dialog.

BLabels can show a piece of text, an image, or a combination. One of the most interesting feature is that if the text String contains HTML code, then this code is rendered in the label. This makes it possible to display a fairly complex text & image layout using a single widget.

The demo code is reproduced at the end of this page; it can also be downloaded here. You also need the icon.

The following sample app illustrates how to combine the BLabel layout abilities with those of the LayoutInfo class. The figure was obtained using a 3x3 FormContainer, 9 BLabels and one icon ( ??? ) (you may have to resize the window the identify the individual labels).

 fc.add( new BLabel( "North", icon, BLabel.CENTER, BLabel.NORTH ), 1, 0,
                 new LayoutInfo( LayoutInfo.SOUTH, LayoutInfo.NONE, new Insets( 0, 0, 0, 0 ), null ) );
 fc.add( new BLabel( "East", icon, BLabel.CENTER, BLabel.EAST ), 2, 1,
                 new LayoutInfo( LayoutInfo.WEST, LayoutInfo.NONE, new Insets( 0, 0, 0, 0 ), null ) );
 fc.add( new BLabel( "West", icon, BLabel.CENTER, BLabel.WEST ), 0, 1,
                 new LayoutInfo( LayoutInfo.EAST, LayoutInfo.NONE, new Insets( 0, 0, 0, 0 ), null ) );
 fc.add( new BLabel( "South", icon, BLabel.CENTER, BLabel.SOUTH ), 1, 2,
                 new LayoutInfo( LayoutInfo.NORTH, LayoutInfo.NONE, new Insets( 0, 0, 0, 0 ), null ) );
 
 fc.add( new BLabel( "Center", icon, BLabel.CENTER, BLabel.CENTER ), 1, 1,
                 new LayoutInfo( LayoutInfo.CENTER, LayoutInfo.NONE, new Insets( 0, 0, 0, 0 ), null ) );
 
 fc.add( new BLabel( "NorthEast", icon, BLabel.CENTER, BLabel.NORTHEAST ), 2, 0,
                 new LayoutInfo( LayoutInfo.SOUTHWEST, LayoutInfo.NONE, new Insets( 0, 0, 0, 0 ), null ) );
 fc.add( new BLabel( "NorthWest", icon, BLabel.CENTER, BLabel.NORTHWEST ), 0, 0,
                 new LayoutInfo( LayoutInfo.SOUTHEAST, LayoutInfo.NONE, new Insets( 0, 0, 0, 0 ), null ) );
 fc.add( new BLabel( "SouthEast", icon, BLabel.CENTER, BLabel.SOUTHEAST ), 2, 2,
                 new LayoutInfo( LayoutInfo.NORTHWEST, LayoutInfo.NONE, new Insets( 0, 0, 0, 0 ), null ) );
 fc.add( new BLabel( "SouthWest", icon, BLabel.CENTER, BLabel.SOUTHWEST ), 0, 2,
                 new LayoutInfo( LayoutInfo.NORTHEAST, LayoutInfo.NONE, new Insets( 0, 0, 0, 0 ), null ) );

The label on the right of the window displays HTML code written with Mozilla:

String htmlCode = "<html><table style=\"text-align: left; width: 100%;\" border=\"1\" cellspacing=\"2\" cellpadding=\"2\">"
 + "<tbody><tr><td style=\"vertical-align: top; text-align: center;\">Some piece of text<br>"
 + "</td><td style=\"vertical-align: top;\"><br></td><td style=\"vertical-align: top;\"><br>"
 + "</td></tr><tr><td style=\"vertical-align: top;\"><br></td>"
 + "<td style=\"vertical-align: top; text-align: center;\"><img style=\"width: 48px; height: 48px;\" alt=\"icon.png\" src=\"icon.png\"><br>"
 + "</td><td style=\"text-align: center; vertical-align: middle;\">etc...<br></td></tr>"
 + "<tr><td style=\"vertical-align: top; text-align: center;\">"
 + "<span style=\"font-family: courier,courier,monospace; font-weight: bold;\">"
 + "Another piece of text (courrier bold)</span><br>"
 + "</td><td style=\"vertical-align: top;\"><br></td>"
 + "<td style=\"vertical-align: top;\"><br></td></tr></tbody></table><br>"
 + "</html>";

The label is added to the window the usual way:

bc.add( new BLabel( htmlCode ), BorderContainer.EAST );

???

 import java.awt.*;
 import buoy.event.*;
 import buoy.widget.*;
 import javax.swing.ImageIcon;
 
 public class BLabelDemo
          extends BFrame
 {
 
     /**
      *  Constructor for the BLabelDemo object
      */
     public BLabelDemo()
     {
         super( "BLabelDemo" );
 
         BorderContainer bc = new BorderContainer();
 
         FormContainer fc = new FormContainer( 3, 3 );
 
         ImageIcon icon = new ImageIcon( "icon.png" );
 
         fc.add( new BLabel( "North", icon, BLabel.CENTER, BLabel.NORTH ), 1, 0,
                 new LayoutInfo( LayoutInfo.SOUTH, LayoutInfo.NONE, new Insets( 0, 0, 0, 0 ), null ) );
         fc.add( new BLabel( "East", icon, BLabel.CENTER, BLabel.EAST ), 2, 1,
                 new LayoutInfo( LayoutInfo.WEST, LayoutInfo.NONE, new Insets( 0, 0, 0, 0 ), null ) );
         fc.add( new BLabel( "West", icon, BLabel.CENTER, BLabel.WEST ), 0, 1,
                 new LayoutInfo( LayoutInfo.EAST, LayoutInfo.NONE, new Insets( 0, 0, 0, 0 ), null ) );
         fc.add( new BLabel( "South", icon, BLabel.CENTER, BLabel.SOUTH ), 1, 2,
                 new LayoutInfo( LayoutInfo.NORTH, LayoutInfo.NONE, new Insets( 0, 0, 0, 0 ), null ) );
 
         fc.add( new BLabel( "Center", icon, BLabel.CENTER, BLabel.CENTER ), 1, 1,
                 new LayoutInfo( LayoutInfo.CENTER, LayoutInfo.NONE, new Insets( 0, 0, 0, 0 ), null ) );
 
         fc.add( new BLabel( "NorthEast", icon, BLabel.CENTER, BLabel.NORTHEAST ), 2, 0,
                 new LayoutInfo( LayoutInfo.SOUTHWEST, LayoutInfo.NONE, new Insets( 0, 0, 0, 0 ), null ) );
         fc.add( new BLabel( "NorthWest", icon, BLabel.CENTER, BLabel.NORTHWEST ), 0, 0,
                 new LayoutInfo( LayoutInfo.SOUTHEAST, LayoutInfo.NONE, new Insets( 0, 0, 0, 0 ), null ) );
         fc.add( new BLabel( "SouthEast", icon, BLabel.CENTER, BLabel.SOUTHEAST ), 2, 2,
                 new LayoutInfo( LayoutInfo.NORTHWEST, LayoutInfo.NONE, new Insets( 0, 0, 0, 0 ), null ) );
         fc.add( new BLabel( "SouthWest", icon, BLabel.CENTER, BLabel.SOUTHWEST ), 0, 2,
                 new LayoutInfo( LayoutInfo.NORTHEAST, LayoutInfo.NONE, new Insets( 0, 0, 0, 0 ), null ) );
 
         bc.add( fc, BorderContainer.WEST );
 
         String htmlCode = "<html><table style=\"text-align: left; width: 100%;\" border=\"1\" cellspacing=\"2\" cellpadding=\"2\">"
                  + "<tbody><tr><td style=\"vertical-align: top; text-align: center;\">Some piece of text<br>"
                  + "</td><td style=\"vertical-align: top;\"><br></td><td style=\"vertical-align: top;\"><br>"
                  + "</td></tr><tr><td style=\"vertical-align: top;\"><br></td>"
                  + "<td style=\"vertical-align: top; text-align: center;\"><img style=\"width: 48px; height: 48px;\" alt=\"icon.png\" src=\"icon.png\"><br>"
                  + "</td><td style=\"text-align: center; vertical-align: middle;\">etc...<br></td></tr>"
                  + "<tr><td style=\"vertical-align: top; text-align: center;\">"
                  + "<span style=\"font-family: courier,courier,monospace; font-weight: bold;\">"
                  + "Another piece of text (courrier bold)</span><br>"
                  + "</td><td style=\"vertical-align: top;\"><br></td>"
                  + "<td style=\"vertical-align: top;\"><br></td></tr></tbody></table><br>"
                  + "</html>";
 
         bc.add( new BLabel( htmlCode ), BorderContainer.EAST );
         setContent( bc );
 
         addEventLink( WindowClosingEvent.class, this, "doQuit" );
 
         pack();
         setVisible( true );
 
     }
 
 
     /**
      *  Quit
      */
     private void doQuit()
     {
         System.exit( 0 );
     }
 
 
     /**
      *  Main
      *
      *@param  args  The command line arguments
      */
     public static void main( String[] args )
     {
         new BLabelDemo();
     }
 }