Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Customize JOptionPane Dialog

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.

like image 853
Victor Mukherjee Avatar asked Sep 02 '12 09:09

Victor Mukherjee


People also ask

What are the 4 JOptionPane dialog boxes?

The JOptionPane displays the dialog boxes with one of the four standard icons (question, information, warning, and error) or the custom icons specified by the user.

How do you make a new line in JOptionPane?

You have to use \n to break the string in different lines. Or you can: Another way to accomplish this task is to subclass the JOptionPane class and override the getMaxCharactersPerLineCount so that it returns the number of characters that you want to represent as the maximum for one line of text.

What is JOptionPane input dialog?

The JOptionPane class is used to provide standard dialog boxes such as message dialog box, confirm dialog box and input dialog box. These dialog boxes are used to display information or get input from the user. The JOptionPane class inherits JComponent class.


2 Answers

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

like image 83
MadProgrammer Avatar answered Oct 25 '22 15:10

MadProgrammer


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();
            }
        });
    }
}
like image 38
nIcE cOw Avatar answered Oct 25 '22 16:10

nIcE cOw