1

I have a extended JDialog for a custom dialog because it's a bit complicated and I can't create it with JOptionPane but my issue is I still want to add to the JDialog the same icons that JOptionPane has to keep my dialogs similar in the entire app. These are the Icons provided by java in JOptionPane, but when I create a JDialog it doesn't have an Icon, how can I add one just like JOptionPane without changing into a JOptionPane.

enter image description here enter image description here

Girl Codes
  • 328
  • 4
  • 17
  • 2
    `UIManager.getLookAndFeel().getDefaults().getIcon(...)` and pass it one of the following keys based on what you need `OptionPane.errorIcon`; `OptionPane.informationIcon`; `OptionPane.warningIcon`; `OptionPane.questionIcon` – MadProgrammer Jan 31 '23 at 23:09
  • For [example](https://stackoverflow.com/a/12228640/230513). – trashgod Jan 31 '23 at 23:13
  • 1
    Check out [UIManager Defaults](https://tips4java.wordpress.com/2008/10/09/uimanager-defaults/) for the list of keywords you can use to access the Icons of the JOptionPane or any Swing component. – camickr Feb 01 '23 at 01:50

1 Answers1

2

On MacOS, JOptionPane, left to right, top to bottom, Error; Information; Warning; Question

enter image description here

Loaded from UIManager

enter image description here

import java.awt.EventQueue;
import java.awt.GridLayout;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.UIManager;

public class Main {
    public static void main(String[] args) {
        new Main();
    }

    public Main() {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                JFrame frame = new JFrame();
                frame.setLayout(new GridLayout(2, 2));
                frame.add(new JLabel(UIManager.getLookAndFeel().getDefaults().getIcon("OptionPane.errorIcon")));
                frame.add(new JLabel(UIManager.getLookAndFeel().getDefaults().getIcon("OptionPane.informationIcon")));
                frame.add(new JLabel(UIManager.getLookAndFeel().getDefaults().getIcon("OptionPane.warningIcon")));
                frame.add(new JLabel(UIManager.getLookAndFeel().getDefaults().getIcon("OptionPane.questionIcon")));
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);

                JOptionPane.showMessageDialog(frame, "Test", "Test", JOptionPane.ERROR_MESSAGE);
                JOptionPane.showMessageDialog(frame, "Test", "Test", JOptionPane.INFORMATION_MESSAGE);
                JOptionPane.showMessageDialog(frame, "Test", "Test", JOptionPane.WARNING_MESSAGE);
                JOptionPane.showMessageDialog(frame, "Test", "Test", JOptionPane.QUESTION_MESSAGE);
            }
        });
    }
}
MadProgrammer
  • 343,457
  • 22
  • 230
  • 366