Java Swing JOptionPane Html Content Example

By | 2017-11-12T09:49:36+00:00 November 12th, 2017|java|0 Comments

In the previous tutorial, we learn about the basic Swing JOptionPane and saw different types of dialogue boxes. Here  we are going to see how to add html content including images to JOptionPane.

JOptionPane Html Content :

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;

class MyJOptionPane extends JFrame {
    JButton Warning, Message;
    JPanel ButtonPanel;
    URL url;

    MyJOptionPane() {
        super("JOptionPane Demo");


        Warning = new JButton("Warning for U");
        Warning.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent ae) {
                String Msg = "<html><font color=red><b><i>Brain Draining </i></b></font> has become a serious<br> ";
                Msg = Msg
                        + " problem which needs <b> immediate </b> attention<br>";
                Msg = Msg + " What do u <i> think </i> </html>";
                JOptionPane.showMessageDialog(ButtonPanel, Msg, "HTML Message",

        Message = new JButton("Click me..");
        Message.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent ae) {
                try {
                    url = new URL(
                } catch (Exception e) {
                    System.out.println("File not Loaded" + e);
                String ImageSrc = "<img src=\"" + url
                        + "\" height=100 width=200>";
                JOptionPane.showMessageDialog(ButtonPanel, "<html>" + ImageSrc
                        + "<br><center> How is the Message </center></html>",
                        "HTML Message with Image",


        ButtonPanel = new JPanel();

        setSize(300, 300);

class JOptionPane_HTML {
    public static void main(String args[]) {
        MyJOptionPane frame = new MyJOptionPane();

Output :

JOptionPane Html

JOptionPane HTML Content :

JOptionPane Html 1

JOptionPane with Images:

JOptionPane Html Image

Happy Learning 🙂

About the Author:

Hi Folks, you have reach this so far, that shows you like what you are learning. Then why don't you support us to improve for bettor tutorials by leaving your valuable comments and why not you keep in touch with us for latest updates on your favorite blog @ facebook , twitter , Or Google+ ,

Leave A Comment