How can we implement the HTML text of JButton in Java?


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 the login based applications. A JButton can generate an ActionListener interface when we trying 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.

Example

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 Text Button");
      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

Updated on: 10-Feb-2020

140 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements