Customize JOptionPane Dialog
Asked Answered
S

3

11

I am learning java swing. The code below is a catch block which handles an IOException and shows a error message.

 catch(IOException e)
    {
        System.out.println("IOException");
        JOptionPane.showMessageDialog(null,"File not found",null,
                                    JOptionPane.ERROR_MESSAGE);
    }

I was thinking of declaring and customizing a JOptionPane of my own inside the catch block like the code below:

JOptionPane jop=new JOptionPane();
        jop.setLayout(new BorderLayout());
        JLabel im=new JLabel("Java Technology Dive Log",
                new ImageIcon("images/gwhite.gif"),JLabel.CENTER);
        jop.add(im,BorderLayout.NORTH);
        jop.setVisible(true);

But the problem is that I don't know how to make it appear on the screen as the showMessageDialogue method does. Please help. Thanks in advance.

Shoe answered 2/9, 2012 at 9:33 Comment(1)
Why not use a JWindow with a JPanel etc inside? If you are customizing it a lot then it is not much effort to do everything yourself. And then you have complete controlKandrakandy
G
27

You can simply add your components to a JPanel and then add this JPanel to your JOptionPane, as shown in this small example :

import java.awt.*;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import javax.swing.*;
import javax.imageio.ImageIO;

public class JOptionPaneExample {

    private void displayGUI() {
        JOptionPane.showConfirmDialog(null,
                        getPanel(),
                        "JOptionPane Example : ",
                        JOptionPane.OK_CANCEL_OPTION,
                        JOptionPane.PLAIN_MESSAGE);
    }

    private JPanel getPanel() {
        JPanel panel = new JPanel();
        JLabel label = new JLabel("Java Technology Dive Log");
        ImageIcon image = null;
        try {
            image = new ImageIcon(ImageIO.read(
                    new URL("http://i.imgur.com/6mbHZRU.png")));
        } catch(MalformedURLException mue) {
            mue.printStackTrace();
        } catch(IOException ioe) {
            ioe.printStackTrace();
        } 

        label.setIcon(image);
        panel.add(label);

        return panel;
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                new JOptionPaneExample().displayGUI();
            }
        });
    }
}
Geller answered 2/9, 2012 at 10:42 Comment(4)
showConfirmDialog, that is the method I was looking for. Thanks again.Shoe
You're MOST WELCOME and KEEP SMILING :-). You really can add your JPanel to any of the methods for JOptionPane, and do whatever you want to do with the Layout to suite your needs :-)Geller
Using the getPanel method, is there some difference if I use inputDialog, messageDialog or confirmDialog?Stepson
@Patrick: Actually the difference, will be the default components, that you get with each form of JOptionPane, like messageDialog will provide one, single button OK by default. 'inputDialog` will provide a JTextField, Ok and CANCEL button. Depending upon the requirements, of one's use case, seems like you choose which one best suits the needs. Simply use the common method for all JOptionPanes, JOptionPane.show"Message/Input/Confirm"Dialog(null, getPanel()), this will give one idea.Geller
C
6

I guess that depends on what's wrong with JOptionPaneshowMessageDialog(Component parentComponent, Object message, String title, int messageType, Icon icon)?

JOptionPane.showMessageDialog(null, "Java Technolgy Dive Log", "Dive", JOptionPane.INFORMATION_MESSAGE, new ImageIcon("images/gwhite.gif"));

Dialog

Canyon answered 2/9, 2012 at 9:42 Comment(2)
You have to marvel at blind down votes. Would you care to enlighten me with why you felt the need to down vote so we can all learn why this question didn't suit you?Canyon
I'd love to know what's wrong with this answer, which in one line can do basically what the op is asking forCanyon
A
3
JOptionPane jop = new JOptionPane();
JDialog dialog = jop.createDialog("File not found");
dialog.setLayout(new BorderLayout());
JLabel im = new JLabel("Java Technology Dive Log", new ImageIcon("images/gwhite.gif"), JLabel.CENTER);
dialog.add(im, BorderLayout.NORTH);
dialog.setVisible(true);
Athanasian answered 2/9, 2012 at 9:46 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.