by Dinesh Thakur Category: Swing

Any component that can take text can also make HTML text , which will be formatted according to HTML rules. this means you can easily add text decorated in a Swing component.

It must begin with the text "<html>", then you can use the normal HTML tags. Note that it is not forced to include normal closing tag.
The ActionListener adds a new JLabel to the form, which also contains HTML text. However, this tag is not added during init () so that should call the validate () method of the container force a redrawing of the components (and thus display the new label). You can also use HTML text for JTabbedPane, JMenuItem, JToolTip, JRadioButton and JCheckBox.
Here is the example of HTML text on Swing Components

/* <applet code=HTMLText.class width=320 height=200> </applet> */
import javax.swing.*;
import java.awt.event.*;
import java.awt.*;
public class HTMLText extends JApplet
         JButton btnClick = new JButton("<html><b><font size=+1>" + "<center><i>Click Here!");
         public void init()
              Frame frm = (Frame)this.getParent().getParent();
              frm.setTitle("HTML text on Swing Components");
              btnClick.addActionListener(new ActionListener()
                    public void actionPerformed(ActionEvent e)
                          getContentPane().add(new JLabel("<html>"+ "<i><font size=+1> "));
                          Container container = getContentPane();
                          container.setLayout(new FlowLayout());

HTML text on Swing Components

About Dinesh Thakur

Dinesh ThakurDinesh Thakur holds an B.SC (Computer Science), MCSE, MCDBA, CCNA, CCNP, A+, SCJP certifications. Dinesh authors the hugely popular blog. Where he writes how-to guides around Computer fundamental , computer software, Computer programming, and web apps. For any type of query or something that you think is missing, please feel free to Contact us.

Search Content

Popular Article