Tuesday , November 21 2017
Home / java / Java Swing JOptionPane Html Content Example

Java Swing JOptionPane Html Content Example

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 java.net.URL;

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");

        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        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",
                        JOptionPane.WARNING_MESSAGE);
            }
        });

        Message = new JButton("Click me..");
        Message.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent ae) {
                try {
                    url = new URL(
                            "file:\\C:\\Desktop\\images\\fox.png");
                } 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",
                        JOptionPane.INFORMATION_MESSAGE);

            }
        });

        ButtonPanel = new JPanel();
        ButtonPanel.add(Warning);
        ButtonPanel.add(Message);

        getContentPane().add(ButtonPanel);
        setSize(300, 300);
        setVisible(true);
    }
}

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 chandrashekhar

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+ ,

Recommended

How to add dynamic files to JTree

In the previous tutorial, we learn about a simple JTree. This tutorial is for adding …

Leave a Reply

Your email address will not be published. Required fields are marked *