How to Create HTML Editor in Java

Introduction

 
This article explains how to create an HTML editor in Java using swing components. This application is GUI based in which we can write HTML code.
 

How to use HTML tags in Swing Components

 
In general, many Swing components display a text string as part of their GUI. By default, a component's text is displayed in a single font and color, all on one line. So we can embed multiple HTML tags within strings.
 
If we need to write multiple lines without using Java, in other words, if we prefer HTML tags to, for example, set font graphics then it is easy to do and maintain. The following code shows how to add multiple properties to a button in a single line of code.
  1. btton = new JButton("<html><b><u>T</u>w<i>o</i></b></html>");  
They print the character "T" in bold and underline format, "w" in bold format and "o" in bold and italic format.
 

How to create an HTML Editor in Java

 
For the proper understanding of how to use HTML tags in Swing components of Java we create this app. In this app, we need to use several steps to design it.
 
Step 1
 
Import some Swing and AWT packages as in the following:
  1. import java.awt.event.*;  
  2. import javax.swing.*;  
  3. import java.awt.*;  
Step 2
 
Now create a class that extends JPanel and implements ActionListener. JPanel displays HTML Components and ActionListener generates events when a button is clicked. Create objects of JLabel and JTextArea; JTextArea is used to write HTML tags and JLabel is used to set the dimensions.
  1. public class SwingHTMLEditor extends JPanel implements ActionListener   
  2. {  
  3.     JTextArea txtArea;  
  4.     JLabel labl;  
Step 3
 
Now create a constructor to initialize the components of Swing and HTML.
  1. public SwingHTMLEditor() {  
  2.     String htmlText = "<html>\n" +  
  3.         "Create table with using different font styles and colors:\n" +  
  4.         "<h1>C-#corner.com</h1>\n" +  
  5.         "<table border=3 margin=3>\n" +  
  6.         "<tr>\n" +  
  7.         "<td><font color=red>1</font></td>\n" +  
  8.         "<td><font color=blue>2</font></td>\n" +  
  9.         "<td><font color=green>3</font></td>\n" +  
  10.         "</tr>\n" +  
  11.         "<tr>\n" +  
  12.         "<td><font size=-2>4</font></td>\n" +  
  13.         "<td><font size=+2>5</font></td>\n" +  
  14.         "<td><i>6</i></td>\n" +  
  15.         "</tr>\n" +  
  16.         "<tr>\n" +  
  17.         "<td><b>7</b></td>\n" +  
  18.         "<td>8</td>\n" +  
  19.         "<td>9</td>\n" +  
  20.         "</tr>\n" +  
  21.         "</table>";  
  22.     txtArea = new JTextArea(2022);  
  23.     setLayout(new BoxLayout(this, BoxLayout.LINE_AXIS));  
  24.     txtArea.setText(htmlText);  
  25.     JScrollPane scrllPn = new JScrollPane(txtArea);  
  26.     JButton lablChngOtpt = new JButton("Click to change");  
  27.     lablChngOtpt.setMnemonic(KeyEvent.VK_C);  
  28.     lablChngOtpt.setAlignmentX(Component.CENTER_ALIGNMENT);  
  29.     lablChngOtpt.addActionListener(this);  
  30.     labl = new JLabel(htmlText) {  
  31.         public Dimension getMaximumSize() {  
  32.             return new Dimension(250250);  
  33.         }  
  34.   
  35.         public Dimension getPreferredSize() {  
  36.             return new Dimension(250250);  
  37.         }  
  38.   
  39.         public Dimension getMinimumSize() {  
  40.             return new Dimension(250250);  
  41.         }  
  42.     };  
  43.     labl.setHorizontalAlignment(SwingConstants.CENTER);  
  44.     labl.setVerticalAlignment(SwingConstants.CENTER);  
  45.     JPanel panlLeft = new JPanel();  
  46.     panlLeft.setLayout(new BoxLayout(panlLeft, BoxLayout.PAGE_AXIS));  
  47.     panlLeft.setBorder(BorderFactory.createCompoundBorder(  
  48.         BorderFactory.createTitledBorder(  
  49.             "Write your own HTML and click the button to see the changes"),  
  50.         BorderFactory.createEmptyBorder(15151515)));  
  51.     panlLeft.add(scrllPn);  
  52.     panlLeft.add(Box.createRigidArea(new Dimension(011)));  
  53.     panlLeft.add(lablChngOtpt);  
  54.     JPanel panlRght = new JPanel();  
  55.     panlRght.setLayout(new BoxLayout(panlRght, BoxLayout.PAGE_AXIS));  
  56.     panlRght.setBorder(BorderFactory.createCompoundBorder(  
  57.         BorderFactory.createTitledBorder("Swing label with HTML"),  
  58.         BorderFactory.createEmptyBorder(15151515)));  
  59.     panlRght.add(labl);  
  60.     setBorder(BorderFactory.createEmptyBorder(15151515));  
  61.     add(panlLeft);  
  62.     add(Box.createRigidArea(new Dimension(110)));  
  63.     add(panlRght);  
  64. }  
Step 4
 
Now create an actionPerformed event that works on our button "Click to change". The purpose of this listener is for when the user clicks on the button the output changes depending on the HTML tags; Changes are shown in the label area.
  1. //Perform changes when user click on the button specified.  
  2. public void actionPerformed(ActionEvent e) {  
  3. labl.setText(txtArea.getText());  
  4. }  
Step 5
 
Create a method showGUI() to generate a frame and add an initial component to it.
  1. private static void showGUI() {  
  2.     //Create and set up the window.  
  3.     JFrame frm = new JFrame("SwingHTMLEditor");  
  4.     frm.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);  
  5.     //Add content to the window.  
  6.     frm.add(new SwingHTMLEditor());  
  7.     //Display the window.  
  8.     frm.pack();  
  9.     frm.setVisible(true);  
  10. }  
Step 6
 
Now define the main method to execute our application.
  1. public static void main(String[] args) {  
  2.     SwingUtilities.invokeLater(new Runnable() {  
  3.         public void run() {  
  4.             UIManager.put("swing.boldMetal", Boolean.FALSE);  
  5.             showGUI();  
  6.         }  
  7.     });  
  8. }  
Complete Code
 
SwingHTMLEditor.java
  1. import java.awt.event.*;  
  2. import javax.swing.*;  
  3. import java.awt.*;  
  4.   
  5. public class SwingHTMLEditor extends JPanel  
  6. implements ActionListener {  
  7.     JTextArea txtArea;  
  8.     JLabel labl;  
  9.     public SwingHTMLEditor() {  
  10.         String htmlText = "<html>\n" +  
  11.             "Create table with using different font styles and colors:\n" +  
  12.             "<h1>C-#corner.com</h1>\n" +  
  13.             "<table border=3 margin=3>\n" +  
  14.             "<tr>\n" +  
  15.             "<td><font color=red>1</font></td>\n" +  
  16.             "<td><font color=blue>2</font></td>\n" +  
  17.             "<td><font color=green>3</font></td>\n" +  
  18.             "</tr>\n" +  
  19.             "<tr>\n" +  
  20.             "<td><font size=-2>4</font></td>\n" +  
  21.             "<td><font size=+2>5</font></td>\n" +  
  22.             "<td><i>6</i></td>\n" +  
  23.             "</tr>\n" +  
  24.             "<tr>\n" +  
  25.             "<td><b>7</b></td>\n" +  
  26.             "<td>8</td>\n" +  
  27.             "<td>9</td>\n" +  
  28.             "</tr>\n" +  
  29.             "</table>";  
  30.         txtArea = new JTextArea(2022);  
  31.         setLayout(new BoxLayout(this, BoxLayout.LINE_AXIS));  
  32.         txtArea.setText(htmlText);  
  33.         JScrollPane scrllPn = new JScrollPane(txtArea);  
  34.         JButton lablChngOtpt = new JButton("Click to change");  
  35.         lablChngOtpt.setMnemonic(KeyEvent.VK_C);  
  36.         lablChngOtpt.setAlignmentX(Component.CENTER_ALIGNMENT);  
  37.         lablChngOtpt.addActionListener(this);  
  38.         labl = new JLabel(htmlText) {  
  39.             public Dimension getMaximumSize() {  
  40.                 return new Dimension(250250);  
  41.             }  
  42.   
  43.             public Dimension getPreferredSize() {  
  44.                 return new Dimension(250250);  
  45.             }  
  46.   
  47.             public Dimension getMinimumSize() {  
  48.                 return new Dimension(250250);  
  49.             }  
  50.         };  
  51.   
  52.         labl.setHorizontalAlignment(SwingConstants.CENTER);  
  53.         labl.setVerticalAlignment(SwingConstants.CENTER);  
  54.         JPanel panlLeft = new JPanel();  
  55.         panlLeft.setLayout(new BoxLayout(panlLeft, BoxLayout.PAGE_AXIS));  
  56.         panlLeft.setBorder(BorderFactory.createCompoundBorder(  
  57.             BorderFactory.createTitledBorder(  
  58.                 "Write your own HTML and click the button to see the changes"),  
  59.             BorderFactory.createEmptyBorder(15151515)));  
  60.         panlLeft.add(scrllPn);  
  61.         panlLeft.add(Box.createRigidArea(new Dimension(011)));  
  62.         panlLeft.add(lablChngOtpt);  
  63.         JPanel panlRght = new JPanel();  
  64.         panlRght.setLayout(new BoxLayout(panlRght, BoxLayout.PAGE_AXIS));  
  65.         panlRght.setBorder(BorderFactory.createCompoundBorder(  
  66.             BorderFactory.createTitledBorder("Swing label with HTML"),  
  67.             BorderFactory.createEmptyBorder(15151515)));  
  68.         panlRght.add(labl);  
  69.         setBorder(BorderFactory.createEmptyBorder(15151515));  
  70.         add(panlLeft);  
  71.         add(Box.createRigidArea(new Dimension(110)));  
  72.         add(panlRght);  
  73.     }  
  74.     //Perform changes when user click on the button specified.  
  75.   
  76.     public void actionPerformed(ActionEvent e) {  
  77.         labl.setText(txtArea.getText());  
  78.     }  
  79.   
  80.     private static void showGUI() {  
  81.         //Create and set up the window.  
  82.         JFrame frm = new JFrame("SwingHTMLEditor");  
  83.         frm.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);  
  84.         //Add content to the window.  
  85.         frm.add(new SwingHTMLEditor());  
  86.         //Display the window.  
  87.         frm.pack();  
  88.         frm.setVisible(true);  
  89.     }  
  90.   
  91.     public static void main(String[] args) {  
  92.         SwingUtilities.invokeLater(new Runnable() {  
  93.             public void run() {  
  94.                 UIManager.put("swing.boldMetal", Boolean.FALSE);  
  95.                 showGUI();  
  96.             }  
  97.         });  
  98.     }  
  99. }  
Output
 
Now compile the program on the console.
 
fig-1.jpg
 
Now press enter to see the output.
 
fig-2.jpg
 
Now change the HTML coding.
  1. <html>  
  2. <form>  
  3. First Name: <input type="text" name="fname">  
  4. Last Name: <input type="text" name="lname">  
  5. Create Password: <input type="password" name="pass">  
  6. Confirm Password: <input type="password" name="pass">  
  7. <input type="submit" value="submit">  
  8. <input type="reset" value="reset">  
Now click on the "Click to change" button. The output changes depending on the HTML as shown below.
 
Fig-3.jpg
 
Note
 
It is just a sample editor. It doesn't support all components of HTML like HTML5 components, image uploaded, etcetera.
 
Thanks for reading