Implement HTML Text of JButton in Java



In this article, we will learn to implement the HTML text of a JButton in Java. The Swing JButton component has a useful feature in which developers can use HTML to style button labels. This feature enables us to make buttons appear more appealing by using simple HTML elements in our Java applications.

What is a JButton?

A JButton is a subclass of AbstractButton, and it is an important component in the Java Swing hierarchy. A JButton can be used mostly in login-based applications.

Syntax

The following is the syntax for JButton initialization:

JButton button = new JButton("Button");

A JButton can generate an ActionListener interface when we try to press or click a button. A JButton has a text or icon or both text and icon, We can also implement bold, italic text using HTML tags.

Why Use HTML in JButtons?

HTML in JButton text has several advantages that enhance button labels in Java Swing applications to be more interactive and efficient. It enables developers to make a more visually appealing GUI by applying various fonts, colors, and text effects to a single button.

This proves useful when we want to display a large amount of the button text or keep the same appearance as the GUI. In addition, you can show multiple lines of text on one button using HTML, which is useful when there is not enough space or when we want to add more information.

Basic Implementation

To use HTML in a JButton, we need to wrap the text in HTML tags:

JButton button = new JButton("<html>Tutorials <b>Point</b></html>");

This will create a button with the text "Tutorials Point" where "Point" is bold.

Implementing HTML text in a JButton

The following are the steps for implementing the HTML text in a JButton in Java:

  • Creating the JFrame: We begin by creating a JFrame, which will be our application's main window.
  • Setting Up the JFrame: The window's title, size, close operation, and centering on the screen will be configured using the methods setTitle(), setSize(), setDefaultCloseOperation(), and setLocationRelativeTo().
  • Creating Buttons:
    • Normal Button (jbutton1): It displays the plain text "Normal Button" with centered alignment and is placed on the left side (WEST) of the frame.
    • HTML Button (jbutton2): It uses the basic HTML tags for formatting, also centers its text, and is positioned on the right side (EAST) of the window.
  • Displaying the Window: Finally, setVisible(true) makes the window appear on the screen.

Normal Button:

jbutton1 = new JButton("Normal Button");
jbutton1.setHorizontalAlignment(SwingConstants.CENTER);
add(jbutton1, BorderLayout.WEST);

HTML Button:

jbutton2 = new JButton("HTML Text Button"));
jbutton2.setHorizontalAlignment(SwingConstants.CENTER);
add(jbutton2, BorderLayout.EAST);

Example of HTML text in a JButton

Below is an example of implementing the Normal text and HTML text in a JButton in Java:

import java.awt.*;
import javax.swing.*;
public class JButtonHtmlTextTest extends JFrame {
   private JButton jbutton1, jbutton2;
   public JButtonHtmlTextTest() {
      setTitle("JButtonHtmlText Test");
      jbutton1 = new JButton("Normal Button");
      jbutton1.setHorizontalAlignment(SwingConstants.CENTER);
      add(jbutton1, BorderLayout.WEST);
      jbutton2 = new JButton("<html>HTML Text Button</html>"));
      jbutton2.setHorizontalAlignment(SwingConstants.CENTER);
      add(jbutton2, BorderLayout.EAST);
      setSize(375, 250);
      setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      setLocationRelativeTo(null);
      setVisible(true);
   }
   public static void main(String[] args) {
      new JButtonHtmlTextTest();
   }
}

Output

Alshifa Hasnain
Alshifa Hasnain

Converting Code to Clarity

Updated on: 2025-04-30T17:39:09+05:30

237 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements