Java Swing JOptionPane Html Content Example - onlinetutorialspoint

PROGRAMMINGJava ExamplesC Examples
Java Examples
C Examples
C Tutorials
aws
JAVAEXCEPTIONSCOLLECTIONSSWINGJDBC
EXCEPTIONS
COLLECTIONS
SWING
JDBC
JAVA 8
SPRING
SPRING BOOT
HIBERNATE
PYTHON
PHP
JQUERY
PROGRAMMINGJava ExamplesC Examples
Java Examples
C Examples
C Tutorials
aws
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.
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 = "Brain Draining  has become a serious ";
                Msg = Msg
                        + " problem which needs  immediate  attention";
                Msg = Msg + " What do u  think  ";
                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 = "";
                JOptionPane.showMessageDialog(ButtonPanel, "" + ImageSrc
                        + " How is the Message ",
                        "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 Content :
JOptionPane with Images:
Happy Learning 🙂

How to change Look and Feel of Swing setLookAndFeel
Java JList Multiple Selection Example
Java JColorChooser Example
How to add dynamic files to JTree
Java Swing JOptionPane Example
Java Swing JLabel Example
Java Swing Login Example
Java Swing JSplitPane Example
Java Swing ProgressBar Example
Java Swing JTable Example
Java Swing Advanced JTable Example
Java Swing JTabbedPane Example
Java Swing JMenu Example
Java Swing JToolBar Example
Java Swing JTree Example

How to change Look and Feel of Swing setLookAndFeel
Java JList Multiple Selection Example
Java JColorChooser Example
How to add dynamic files to JTree
Java Swing JOptionPane Example
Java Swing JLabel Example
Java Swing Login Example
Java Swing JSplitPane Example
Java Swing ProgressBar Example
Java Swing JTable Example
Java Swing Advanced JTable Example
Java Swing JTabbedPane Example
Java Swing JMenu Example
Java Swing JToolBar Example
Java Swing JTree Example
 


Δ
 Install Java on Mac OS
 Install AWS CLI on Windows
 Install Minikube on Windows
 Install Docker Toolbox on Windows
 Install SOAPUI on Windows
 Install Gradle on Windows
 Install RabbitMQ on Windows
 Install PuTTY on windows
 Install Mysql on Windows
 Install Hibernate Tools in Eclipse
 Install Elasticsearch on Windows
 Install Maven on Windows
 Install Maven on Ubuntu
 Install Maven on Windows Command
 Add OJDBC jar to Maven Repository
 Install Ant on Windows
 Install RabbitMQ on Windows
 Install Apache Kafka on Ubuntu
 Install Apache Kafka on Windows
 Java8 – Install Windows
 Java8 – foreach
 Java8 – forEach with index
 Java8 – Stream Filter Objects
 Java8 – Comparator Userdefined
 Java8 – GroupingBy
 Java8 – SummingInt
 Java8 – walk ReadFiles
 Java8 – JAVA_HOME on Windows
 Howto – Install Java on Mac OS
 Howto – Convert Iterable to Stream
 Howto – Get common elements from two Lists
 Howto – Convert List to String
 Howto – Concatenate Arrays using Stream
 Howto – Remove duplicates from List
 Howto – Filter null values from Stream
 Howto – Convert List to Map
 Howto – Convert Stream to List
 Howto – Sort a Map
 Howto – Filter a Map
 Howto – Get Current UTC Time
 Howto – Verify an Array contains a specific value
 Howto – Convert ArrayList to Array
 Howto – Read File Line By Line
 Howto – Convert Date to LocalDate
 Howto – Merge Streams
 Howto – Resolve NullPointerException in toMap
 Howto -Get Stream count
 Howto – Get Min and Max values in a Stream
 Howto – Convert InputStream to String