# javax.swing.text.html

***

**1. Create an HTML Editor**

```java
import javax.swing.text.html.*;
import javax.swing.JEditorPane;
import javax.swing.JFrame;
import javax.swing.JScrollPane;

public class SwingHTMLEditor {
    public static void main(String[] args) {
        JEditorPane editor = new JEditorPane();
        editor.setContentType("text/html");
        editor.setText("<html><body><h1>Hello World</h1></body></html>");

        JFrame frame = new JFrame("HTML Editor");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setSize(400, 300);
        frame.add(new JScrollPane(editor), "Center");
        frame.setVisible(true);
    }
}
```

**2. Display HTML from a URL**

```java
import javax.swing.text.html.*;
import javax.swing.JEditorPane;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import java.net.URL;

public class DisplayHTMLFromURL {
    public static void main(String[] args) {
        try {
            URL url = new URL("https://www.example.com");
            JEditorPane editor = new JEditorPane(url);
            editor.setContentType("text/html");

            JFrame frame = new JFrame("HTML Display");
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            frame.setSize(400, 300);
            frame.add(new JScrollPane(editor), "Center");
            frame.setVisible(true);
        } catch (Exception e) {
            JOptionPane.showMessageDialog(null, "Error: " + e.getMessage());
        }
    }
}
```

**3. Load HTML from a String**

```java
import javax.swing.text.html.*;
import javax.swing.JEditorPane;
import javax.swing.JFrame;
import java.io.StringReader;

public class LoadHTMLFromString {
    public static void main(String[] args) {
        String html = "<html><body><h1>Hello World</h1></body></html>";
        JEditorPane editor = new JEditorPane();
        editor.setContentType("text/html");
        editor.setDocument(new HTMLDocument());
        editor.read(new StringReader(html), null);

        JFrame frame = new JFrame("HTML Display");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setSize(400, 300);
        frame.add(new JScrollPane(editor), "Center");
        frame.setVisible(true);
    }
}
```

**4. Parse HTML and Get DOM**

```java
import javax.swing.text.html.*;
import javax.swing.JEditorPane;
import javax.swing.JFrame;
import org.w3c.dom.*;

public class ParseHTMLGetDOM {
    public static void main(String[] args) {
        String html = "<html><body><h1>Hello World</h1></body></html>";
        JEditorPane editor = new JEditorPane();
        editor.setContentType("text/html");
        editor.setText(html);

        try {
            Document doc = (Document) editor.getDocument();
            Element h1 = doc.getElementsByTagName("h1").item(0);
            System.out.println(h1.getTextContent());
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
```

**5. Add CSS Styles to HTML**

```java
import javax.swing.text.html.*;
import javax.swing.JEditorPane;
import javax.swing.JFrame;
import java.awt.*;

public class AddCSSStylesToHTML {
    public static void main(String[] args) {
        JEditorPane editor = new JEditorPane();
        editor.setContentType("text/html");
        editor.setText(
            "<html>" +
                "<head>" +
                    "<style>" +
                        "h1 { color: red; font-size: 2em; }" +
                    "</style>" +
                "</head>" +
                "<body>" +
                    "<h1>Hello World</h1>" +
                "</body>" +
            "</html>"
        );

        JFrame frame = new JFrame("HTML Display");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setSize(400, 300);
        frame.add(new JScrollPane(editor), "Center");
        frame.setVisible(true);
    }
}
```

**6. Style HTML with Inline CSS**

```java
import javax.swing.text.html.*;
import javax.swing.JEditorPane;
import javax.swing.JFrame;
import java.awt.*;

public class StyleHTMLWithInlineCSS {
    public static void main(String[] args) {
        JEditorPane editor = new JEditorPane();
        editor.setContentType("text/html");
        editor.setText(
            "<html>" +
                "<body>" +
                    "<h1 style=\"color: red; font-size: 2em;\">Hello World</h1>" +
                "</body>" +
            "</html>"
        );

        JFrame frame = new JFrame("HTML Display");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setSize(400, 300);
        frame.add(new JScrollPane(editor), "Center");
        frame.setVisible(true);
    }
}
```

**7. Insert Images into HTML**

```java
import javax.swing.text.html.*;
import javax.swing.JEditorPane;
import javax.swing.JFrame;
import java.awt.*;
import java.net.URL;

public class InsertImagesIntoHTML {
    public static void main(String[] args) {
        JEditorPane editor = new JEditorPane();
        editor.setContentType("text/html");
        editor.setText(
            "<html>" +
                "<body>" +
                    "<img src=\"https://example.com/image.jpg\" alt=\"Image\" />" +
                "</body>" +
            "</html>"
        );

        JFrame frame = new JFrame("HTML Display");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setSize(400, 300);
        frame.add(new JScrollPane(editor), "Center");
        frame.setVisible(true);
    }
}
```

**8. Add Hyperlinks to HTML**

```java
import javax.swing.text.html.*;
import javax.swing.JEditorPane;
import javax.swing.JFrame;
import java.awt.*;
import java.net.URL;

public class AddHyperlinksToHTML {
    public static void main(String[] args) {
        JEditorPane editor = new JEditorPane();
        editor.setContentType("text/html");
        editor.setText(
            "<html>" +
                "<body>" +
                    "<a href=\"https://example.com\">Visit Example.com</a>" +
                "</body>" +
            "</html>"
        );

        JFrame frame = new JFrame("HTML Display");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setSize(400, 300);
        frame.add(new JScrollPane(editor), "Center");
        frame.setVisible(true);
    }
}
```

**9. Create a Simple HTML Viewer**

```java
import javax.swing.text.html.*;
import javax.swing.JEditorPane;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import java.awt.*;
import java.net.URL;

public class SimpleHTMLViewer {
    public static void main(String[] args) {
        JFrame frame = new JFrame("Simple HTML Viewer");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setSize(400, 300);

        JEditorPane editor = new JEditorPane();
        editor.setContentType("text/html");
        editor.setEditable(false);
        editor.addHyperlinkListener(e -> {
            try {
                editor.setPage(e.getURL());
            } catch (Exception ex) {
                ex.printStackTrace();
            }
        });

        frame.add(new JScrollPane(editor), "Center");
        frame.setVisible(true);
    }
}
```

**10. Display HTML in a JTextArea**

```java
import javax.swing.text.html.*;
import javax.swing.JTextArea;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import java.awt.*;
import java.net.URL;

public class DisplayHTMLInJTextArea {
    public static void main(String[] args) {
        try {
            URL url = new URL("https://example.com");
            HTMLDocument doc = (HTMLDocument) new HTMLEditorKit().createDefaultDocument();
            doc.getStyleSheet().addRule("body { font-family: Arial; font-size: 14px; }");
            doc.setBase(url);
            doc.processHTML(url.openStream(), null);

            JTextArea editor = new JTextArea();
            editor.setDocument(doc);
            editor.setEditable(false);

            JFrame frame = new JFrame("HTML Display");
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            frame.setSize(400, 300);
            frame.add(new JScrollPane(editor), "Center");
            frame.setVisible(true);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
```

**11. Convert HTML to Plain Text**

```java
import javax.swing.text.html.*;
import javax.swing.JEditorPane;
import java.io.Writer;
import java.io.StringWriter;

public class ConvertHTMLToPlainText {
    public static void main(String[] args) {
        String html = "<html><body><h1>Hello World</h1></body></html>";
        JEditorPane editor = new JEditorPane();
        editor.setContentType("text/html");
        editor.setText(html);

        Writer writer = new StringWriter();
        try {
            editor.write(writer, "text/plain", null);
        } catch (Exception e) {
            e.printStackTrace();
        }

        System.out.println(writer.toString());
    }
}
```

**12. Extract Text from HTML**

```java
import javax.swing.text.html.*;
import javax.swing.JEditorPane;
import java.io.Reader;
import java.io.StringReader;

public class ExtractTextFromHTML {
    public static void main(String[] args) {
        String html = "<html><body><h1>Hello World</h1></body></html>";
        JEditorPane editor = new JEditorPane();
        editor.setContentType("text/html");
        editor.setText(html);

        Reader reader = new StringReader(editor.getText());
        try {
            HTMLDocument doc = (HTMLDocument) editor.getDocument();
            HTMLReader parser = new HTMLReader(reader, doc);
            parser.parse();

            System.out.println(doc.getText(0, doc.getLength()));
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
```

**13. Create a Custom HTML Editor**

```java
import javax.swing.text.html.*;
import javax.swing.JEditorPane;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JMenuBar;
import javax.swing.JMenu;
import javax.swing.JMenuItem;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

public class CustomHTMLEditor {
    public static void main(String[] args) {
        JFrame frame = new JFrame("Custom HTML Editor");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setSize(400, 300);

        JEditorPane editor = new JEditorPane();
        editor.setContentType("text/html");

        JScrollPane scrollPane = new JScrollPane(editor);
        frame.add(scrollPane, "Center");

        JMenuBar menuBar = new JMenuBar();
        JMenu fileMenu = new JMenu("File");
        menuBar.add(fileMenu);

        JMenuItem openMenuItem = new JMenuItem("Open");
        openMenuItem.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                // Implement open file functionality
            }
        });
        fileMenu.add(openMenuItem);

        JMenuItem saveMenuItem = new JMenuItem("Save");
        saveMenuItem.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                // Implement save file functionality
            }
        });
        fileMenu.add(saveMenuItem);

        frame.setJMenuBar(menuBar);

        frame.setVisible(true);
    }
}
```

**14. Create a Web Browser**

```java
import javax.swing.text.html.*;
import javax.swing.JEditorPane;
import javax.swing.JFrame;
import javax.swing.JTextField;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.net.URL;

public class WebBrowser {
    public static void main(String[] args) {
        JFrame frame = new JFrame("Web Browser");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setSize(400, 300);

        JTextField urlField = new JTextField("https://example.com");
        JButton goButton = new JButton("Go");
        JEditorPane editor = new JEditorPane();
        editor.setContentType("text/html");

        goButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                try {
                    editor.setPage(new URL(urlField.getText()));
                } catch (Exception ex) {
                    ex.printStackTrace();
                }
            }
        });

        frame.add(urlField, "North");
        frame.add(goButton, "East");
        frame.add(editor, "Center");

        frame.setVisible(true);
    }
}
```

**15. Embed HTML in a Swing Application**

```java
import javax.swing.text.html.*;
import javax.swing.JEditorPane;
import javax.swing.JPanel;
import javax.swing.JFrame;
import java.awt.*;

public class EmbedHTMLInSwingApp {
    public static void main(String[] args) {
        JFrame frame = new JFrame("Embed HTML");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setSize(400, 300);

        String html = "<html><body><h1>Hello World</h1></body></html>";
        JEditorPane editor = new JEditorPane();
        editor.setContentType("text/html");

        JPanel panel = new JPanel(new BorderLayout());
        panel.add(editor, BorderLayout.CENTER);
        frame.add(panel);

        editor.setText(html);

        frame.setVisible(true);
    }
}
```

**16. Create an HTML-Based Image Viewer**

```java
```
