How to Put an HTML code in a Button in Java

This tutorial is about how to put an HTML code in a button in Java. The HTML tags will be put inside the JButton to display text. So, now let's start this tutorial! 1. Open JCreator or NetBeans and make a java program with a file name of htmlInButton.java. 2. Import the javax.swing.* package library that is used to access the JButton, and JFrame class.
  1. import javax.swing.*; //used to access the JButton, and JFrame class.
3. Initialize your variable in your Main, variable frame for JFrame, and variable button as JButton.
  1. JFrame frame = new JFrame("HTML Code in a Button");
  2. JButton button = new JButton();
4. Now, to create an HTML code inside the button we will use the setText method of our button. The setText method can recognize HTML code. It has an advantage of calling a text with a font, color, alignment, font style without using the classes of awt library like font class, color class, alignment, and the styles of the font. Have this code below:
  1. button.setText ("<html><font face='Tahoma'" + " color=blue><b>This text has an HTML Code<b></font><br><font face='courier new'" + " color=red><center>Sourcecodester<center></font></html>");
  2.  
html - an html tag font face='Tahoma'" + " color=blue - a font tag with a font style of tahoma and font color of blue b- start of the bold tag This text has an HTML Code - the text inside the html with a specific font of tahoma and blue color of font font face='courier new'" + " color=red - a font tag with a font style of courier new and font color of red center - a center alignment tag Sourcecodester - the text inside the html with a specific font of courier new and red color of font Add the button to the frame using the add method.
  1. frame.getContentPane().add(button);
5. Lastly, set the size, set visibility to true, and the close operation of the frame. Have this code below:
  1. frame.setSize(300, 200);
  2. frame.setVisible(true);
  3. frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Output: output Here's the full code of this tutorial:
  1. import javax.swing.*; //used to access the JButton, and JFrame class.
  2.  
  3. public class htmlInButton{
  4.  
  5. public static void main(String[] a) {
  6. JFrame frame = new JFrame("HTML Code in a Button");
  7. JButton button = new JButton();
  8. button.setText ("<html><font face='Tahoma'" + " color=blue><b>This text has an HTML Code<b></font><br><font face='courier new'" + " color=red><center>Sourcecodester<center></font></html>");
  9.  
  10. frame.getContentPane().add(button);
  11. frame.setSize(300, 200);
  12. frame.setVisible(true);
  13. frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  14. }
  15.  
  16. }
For more inquiries and need programmer for your thesis systems in any kind of programming languages, just contact my number below. Best Regards, Engr. Lyndon Bermoy IT Instructor/System Developer/Android Developer/Freelance Programmer Mobile: 09488225971 Landline: 826-9296 E-mail:[email protected] Add and Follow me on Facebook: https://www.facebook.com/donzzsky Visit and like my page on Facebook at: https://www.facebook.com/BermzISware

Add new comment