Header javaperspective.com
JavaPerspective.com  >   Advanced Tutorials  >   1. Advanced GUI Features  >   1.7. HTML formatting

1.7. HTML formatting
Last updated: 25 January 2013.

This tutorial will show you how to use HTML formatting to customize components in Java.

An interesting but frequently unused feature of Swing is that you can use HTML to customize the appearance of labels, tool tips, buttons, radio buttons, check boxes, menus, tabbed panes, tables and trees.

So far, to set the colors and fonts of the components I just mentioned, I have used the methods setForeground and setFont. With these methods, a component is displayed in a single font and foreground color. Furthermore, the component's text is rendered in a single line.

HTML formatting offers greater flexibility since it allows you to mix different fonts and foreground colors to render the text of a component on one or more lines. For example, the next sample uses HTML formatting to render the text of a button on three lines, using several colors and fonts:

JButton button = new JButton("<html><font color=red>Three</font><br/><font color=blue size=5><u>Lines</u></font><br/><b><i>Button</i></b></html>");

The button's text must be valid HTML. Here is the result:


HTML formatting example


With HTML formatting, you can effortlessly enhance the appearance of your GUI. As an example, the following picture shows a slightly modified version of the GUI you may have already seen in the tutorial radio buttons. In this version, I have used HTML formatting to set the color but also to underline the first letter of each radio button's text.


HTML formatting example 2



Here is the code:


import java.awt.Color;
import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.BorderFactory;
import javax.swing.BoxLayout;
import javax.swing.ButtonGroup;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.SwingConstants;
import javax.swing.SwingUtilities;
import javax.swing.border.BevelBorder;

public final class RadioButtons extends JFrame implements ActionListener {

   
private JRadioButton blackButton;
   
private JRadioButton redButton;
   
private JRadioButton greenButton;
   
private JRadioButton blueButton;

   
private JLabel label;

   
public RadioButtons(){
         
init();
          addComponents
();

          setDefaultCloseOperation
(JFrame.EXIT_ON_CLOSE);
          setVisible
(true);
   
}


   
public void actionPerformed(ActionEvent e) {
         
if(e.getSource().equals(blackButton))
               
label.setForeground(Color.BLACK);
         
else if(e.getSource().equals(redButton))
               
label.setForeground(Color.RED);
         
else if(e.getSource().equals(greenButton))
               
label.setForeground(Color.GREEN);
         
else if(e.getSource().equals(blueButton))
               
label.setForeground(Color.BLUE);
   
}


   
private void addComponents(){
         
JPanel contentPane = new JPanel();
          contentPane.setLayout
(new BoxLayout(contentPane, BoxLayout.LINE_AXIS));

          ButtonGroup colorButtonGroup =
new ButtonGroup();

         
// The left pane contains the radio buttons --------------------------------------------------------
          // -----------------------------------------------------------------------------------------------------
         
JPanel leftPane = new JPanel(new GridLayout(0, 1));
          leftPane.setBorder
(BorderFactory.createBevelBorder(BevelBorder.LOWERED));

          blackButton =
new JRadioButton("<html><font color=black><u>B</u>lack</font></html>");
          blackButton.setSelected
(true);
          blackButton.addActionListener
(this);

          redButton =
new JRadioButton("<html><font color=red><u>R</u>ed</font></html>");
          redButton.addActionListener
(this);

          greenButton =
new JRadioButton("<html><font color=green><u>G</u>reen</font></html>");
          greenButton.addActionListener
(this);

          blueButton =
new JRadioButton("<html><font color=blue><u>B</u>lue</font></html>");
          blueButton.addActionListener
(this);

          leftPane.add
(blackButton);
          leftPane.add
(redButton);
          leftPane.add
(greenButton);
          leftPane.add
(blueButton);

          contentPane.add
(leftPane);
         
// -----------------------------------------------------------------------------------------------------


          // The right pane contains the label ----------------------------------------------------------------
          // -----------------------------------------------------------------------------------------------------
         
JPanel rightPane = new JPanel();
          rightPane.setBorder
(BorderFactory.createBevelBorder(BevelBorder.LOWERED));

          label =
new JLabel();
          label.setHorizontalAlignment
(SwingConstants.CENTER);
          label.setPreferredSize
(new Dimension(180, 130));
          label.setText
("Radio buttons example");

          rightPane.add
(label);

          contentPane.add
(rightPane);
         
// ----------------------------------------------------------------------------------------------------


          // Add the radio buttons to the ButtonGroup
         
colorButtonGroup.add(blackButton);
          colorButtonGroup.add
(redButton);
          colorButtonGroup.add
(greenButton);
          colorButtonGroup.add
(blueButton);


         
// Add the content pane to the JFrame
         
add(contentPane);
   
}


   
private void init(){
         
setTitle("Radio buttons");
          setSize
(300, 170);
          setLocationRelativeTo
(null);
   
}


   
public static void main(String[] args){
         
SwingUtilities.invokeLater(new Runnable() {
               
public void run() {
                     
new RadioButtons();
               
}
          })
;
   
}
}


You are here :  JavaPerspective.com  >   Advanced Tutorials  >   1. Advanced GUI Features  >   1.7. HTML formatting
Next tutorial :  JavaPerspective.com  >   Advanced Tutorials  >   2. JDBC - database access

Copyright © 2013 - 2016. JavaPerspective.com. All rights reserved.  ( Terms | Contact | About ) 
Java is a trademark of Oracle Corporation
Image 1 Image 2 Image 3 Image 4 Image 5 Image 6 Image 7