Skip to main content

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 R. Bermoy
IT Instructor/System Developer/Android Developer
Mobile: 09079373999
Telephone: 826-9296
E-mail:[email protected]

Visit and like my page on Facebook at: https://www.facebook.com/BermzISware

Subscribe at my YouTube Channel at: Ser Bermz

Visit my website at: www.campcodes.com

Note: Due to the size or complexity of this submission, the author has submitted it as a .zip file to shorten your download time. After downloading it, you will need a program like Winzip to decompress it.

Virus note: All files are scanned once-a-day by SourceCodester.com for viruses, but new viruses come out every day, so no prevention program can catch 100% of them.

FOR YOUR OWN SAFETY, PLEASE:

1. Re-scan downloaded files using your personal virus checker before using it.
2. NEVER, EVER run compiled files (.exe's, .ocx's, .dll's etc.)--only run source code.

Add new comment

CAPTCHA
This question is for testing whether or not you are a human visitor and to prevent automated spam submissions.