0

I need simple icons like gears, arrows and such for my program. Giving away my program without worrying about extra images on the host file system would be nice. I know some images are contained in JRE for Swing, like the yellow warning triangle:

enter image description here

Still, i can not locate the above triangle within the JRE i am using. So i would not know how to use it (except by using JOptionPane.showMessageDialog(null, "My warning message", "WARNING", JOptionPane.WARNING_MESSAGE);

BabaNew
  • 626
  • 1
  • 10
  • 18
  • Consider using Unicode characters, instead of images. For gear, try using `"\u2699"`, `"\u26ed"`, or `"\u26ee"`. For arrows, there are entire blocks in Unicode: [Arrows](http://www.fileformat.info/info/unicode/block/arrows/list.htm); Supplemental Arrows-A, -B, and -C; and [Miscellaneous Symbols and Arrows](http://www.fileformat.info/info/unicode/block/miscellaneous_symbols_and_arrows/list.htm). There are many symbol blocks, like Miscellaneous Symbols, Dingbats, Geometric Shapes, and Miscellaneous Symbols and Pictographs. A nice benefit of using characters is that they scale quite well. – VGR Apr 01 '20 at 00:36

2 Answers2

2

These images are bundled with the standard library class files, within a big "modules" file in your JDK directory. See here: How to extract the file jre-9/lib/modules?

This warning.png image in particular is stored as javax/swing/plaf/metal/icons/ocean/warning.png within that modules file. You can see a list of all the "ocean" theme icons in OpenJDK source here: https://github.com/openjdk/jdk/tree/master/src/java.desktop/share/classes/javax/swing/plaf/metal/icons/ocean

Joni
  • 101,441
  • 12
  • 123
  • 178
0

Consider using Unicode characters, instead of images. For gear, try using "\u2699", "\u26ed", or "\u26ee". For arrows, there are entire blocks in Unicode: Arrows; Supplemental Arrows-A, -B, and -C; and Miscellaneous Symbols and Arrows. There are many symbol blocks, like Miscellaneous Symbols, Dingbats, Geometric Shapes, and Miscellaneous Symbols and Pictographs. A nice benefit of using characters is that they scale quite well.

If you must have the JOptionPane icons, you can retrieve them using their corresponding defaults keys:

import java.awt.*;
import javax.swing.*;

public class JOptionPaneIconDisplayer {
    static void show() {
        Icon info = UIManager.getIcon("OptionPane.informationIcon");
        Icon warning = UIManager.getIcon("OptionPane.warningIcon");
        Icon error = UIManager.getIcon("OptionPane.errorIcon");
        Icon question = UIManager.getIcon("OptionPane.questionIcon");

        JPanel panel = new JPanel(new GridLayout(1, 0, 12, 12));
        panel.add(new JLabel(info));
        panel.add(new JLabel(warning));
        panel.add(new JLabel(error));
        panel.add(new JLabel(question));

        panel.setBorder(BorderFactory.createEmptyBorder(24, 24, 24, 24));

        JFrame frame = new JFrame("JOptionPane Icons");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.getContentPane().add(panel);
        frame.pack();
        frame.setLocationByPlatform(true);
        frame.setVisible(true);
    }

    public static void main(String[] args) {
        EventQueue.invokeLater(() -> show());
    }
}

Attempting to read the icons directly from a classpath resource is not a good idea, as their names and locations may change from one Java release to the next.

VGR
  • 33,718
  • 4
  • 37
  • 50