SymbolChooser - Symbole und Icons auswählen

Aus Byte-Welt Wiki

Eine eigene Komponente zum Auswählen von Icons, Bildern oder Symbolen kann man mit Hilfe einer JOptionPane und einem JDialog erzeugen.

Hier eine komplette Klasse:

    public class SymbolChooser() {
        private static final int INSETS = 10;
        public SymbolChooser() {

        JOptionPane pane = new JOptionPane(null, JOptionPane.PLAIN_MESSAGE, JOptionPane.DEFAULT_OPTION, null, new Object[]{});

        JPanel panel = new JPanel(new GridLayout(0, 7));
        panel.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));
        panel.setBackground(Color.WHITE);
        for (Symbol s : Symbol.values()) {
            Icon icon = new ImageIcon(SymbolCatalog.getImage(s, 32, 32));
            JLabel label = new JLabel(icon);
            label.setPreferredSize(new Dimension(icon.getIconWidth() + INSETS, icon.getIconHeight() + INSETS));
            label.setOpaque(true);
            if (s.equals(this.symbol)) {
                label.setBackground(iconList.getSelectionBackground());
            } else {
                label.setBackground(panel.getBackground());
            }
            label.setToolTipText(SymbolCatalog.getDescription(s));
            panel.add(label);
        }

        JDialog dialog = pane.createDialog(iconList, "Symbol auswählen");
        dialog.setContentPane(panel);
        dialog.pack();
        dialog.setVisible(true);
        }
    }