Systemfarben in JTable ausgeben

Aus Byte-Welt Wiki
Version vom 24. April 2019, 18:23 Uhr von L-ectron-X (Diskussion | Beiträge)
(Unterschied) ← Nächstältere Version | Aktuelle Version (Unterschied) | Nächstjüngere Version → (Unterschied)
Zur Navigation springenZur Suche springen

SystemColor.png

import java.awt.Color;
import java.awt.Component;
import java.awt.SystemColor;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;
import javax.swing.table.DefaultTableCellRenderer;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableModel;

/**
 *
 * @author Gernot Segieth
 */
public class ShowSystemColor {

    public static void main(String[] args) {
        createAndShowGUI();
    }

    private static void createAndShowGUI() {
        JFrame.setDefaultLookAndFeelDecorated(true);
        UIManager.put("swing.boldMetal", false);
        JFrame frame = new JFrame("SystemColors - wiki.byte-welt.net");
        frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        JTable table = new JTable(createSystemColorTableModel());
        table.setDefaultRenderer(Color.class, new ColorTableRenderer());
        frame.add(new JScrollPane(table));
        frame.pack();
        frame.setLocationByPlatform(true);
        SwingUtilities.invokeLater(() -> {
            frame.setVisible(true);
        });

    }

    private static TableModel createSystemColorTableModel() {
        String[] columnNames = new String[]{"Name", "View"};
        Object[][] colors = new Object[][]{
            {"SystemColor.activeCaption", SystemColor.activeCaption},
            {"SystemColor.activeCaptionBorder", SystemColor.activeCaptionBorder},
            {"SystemColor.activeCaptionText", SystemColor.activeCaptionText},
            {"SystemColor.control", SystemColor.control},
            {"SystemColor.controlDkShadow", SystemColor.controlDkShadow},
            {"SystemColor.controlHighlight", SystemColor.controlHighlight},
            {"SystemColor.controlLtHighlight", SystemColor.controlLtHighlight},
            {"SystemColor.controlShadow", SystemColor.controlShadow},
            {"SystemColor.controlText", SystemColor.controlText},
            {"SystemColor.desktop", SystemColor.desktop},
            {"SystemColor.inactiveCaption", SystemColor.inactiveCaption},
            {"SystemColor.inactiveCaptionBorder", SystemColor.inactiveCaptionBorder},
            {"SystemColor.inactiveCaptionText", SystemColor.inactiveCaptionText},
            {"SystemColor.info", SystemColor.info},
            {"SystemColor.infoText", SystemColor.infoText},
            {"SystemColor.menu", SystemColor.menu},
            {"SystemColor.menuText", SystemColor.menuText},
            {"SystemColor.scrollbar", SystemColor.scrollbar},
            {"SystemColor.text", SystemColor.text},
            {"SystemColor.textHighlight", SystemColor.textHighlight},
            {"SystemColor.textHighlightText", SystemColor.textHighlightText},
            {"SystemColor.textInactiveText", SystemColor.textInactiveText},
            {"SystemColor.textText", SystemColor.textText},
            {"SystemColor.window", SystemColor.window},
            {"SystemColor.windowBorder", SystemColor.windowBorder},
            {"SystemColor.windowText", SystemColor.windowText}
        };

        return new DefaultTableModel(colors, columnNames) {
            @Override
            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return false;
            }

            @Override
            public Class getColumnClass(int columnIndex) {
                switch (columnIndex) {
                    case 1:
                        return Color.class;

                    default:
                        return String.class;
                }
            }
        };
    }

    private static class ColorTableRenderer extends DefaultTableCellRenderer {

        @Override
        public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) {
            super.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column);

            if (value instanceof Color) {
                setBackground((Color) value);
            }
            setText(null);
            return this;
        }
    }
}