Example usage for javax.swing UIDefaults getColor

List of usage examples for javax.swing UIDefaults getColor

Introduction

In this page you can find the example usage for javax.swing UIDefaults getColor.

Prototype

public Color getColor(Object key) 

Source Link

Document

If the value of key is a Color return it, otherwise return null.

Usage

From source file:Main.java

public static void main(String[] argv) {
    UIDefaults uidefs = UIManager.getLookAndFeelDefaults();
    String[] keys = (String[]) uidefs.keySet().toArray(new String[0]);
    for (int i = 0; i < keys.length; i++) {
        Object v = uidefs.get(keys[i]);
        if (v instanceof Integer) {
            int intVal = uidefs.getInt(keys[i]);
        } else if (v instanceof Boolean) {
            boolean boolVal = uidefs.getBoolean(keys[i]);
        } else if (v instanceof String) {
            String strVal = uidefs.getString(keys[i]);
        } else if (v instanceof Dimension) {
            Dimension dimVal = uidefs.getDimension(keys[i]);
        } else if (v instanceof Insets) {
            Insets insetsVal = uidefs.getInsets(keys[i]);
        } else if (v instanceof Color) {
            Color colorVal = uidefs.getColor(keys[i]);
        } else if (v instanceof Font) {
            Font fontVal = uidefs.getFont(keys[i]);
        } else if (v instanceof Border) {
            Border borderVal = uidefs.getBorder(keys[i]);
        } else if (v instanceof Icon) {
            Icon iconVal = uidefs.getIcon(keys[i]);
        } else if (v instanceof javax.swing.text.JTextComponent.KeyBinding[]) {
            JTextComponent.KeyBinding[] keyBindsVal = (JTextComponent.KeyBinding[]) uidefs.get(keys[i]);
        } else if (v instanceof InputMap) {
            InputMap imapVal = (InputMap) uidefs.get(keys[i]);
        } else {//www.j  a  va  2 s. c o  m
            System.out.println("Unknown type");
        }
    }
}

From source file:Main.java

public static void main(String[] argv) {
    UIDefaults uidefs = UIManager.getLookAndFeelDefaults();
    String[] keys = (String[]) uidefs.keySet().toArray(new String[0]);
    for (int i = 0; i < keys.length; i++) {
        Object v = uidefs.get(keys[i]);
        if (v instanceof Integer) {
            int intVal = uidefs.getInt(keys[i]);
        } else if (v instanceof Boolean) {
            boolean boolVal = uidefs.getBoolean(keys[i]);
        } else if (v instanceof String) {
            String strVal = uidefs.getString(keys[i]);
        } else if (v instanceof Dimension) {
            Dimension dimVal = uidefs.getDimension(keys[i]);
        } else if (v instanceof Insets) {
            Insets insetsVal = uidefs.getInsets(keys[i]);
        } else if (v instanceof Color) {
            Color colorVal = uidefs.getColor(keys[i]);
        } else if (v instanceof Font) {
            Font fontVal = uidefs.getFont(keys[i]);
        } else if (v instanceof Border) {
            Border borderVal = uidefs.getBorder(keys[i]);
        } else if (v instanceof Icon) {
            Icon iconVal = uidefs.getIcon(keys[i]);
        } else if (v instanceof javax.swing.text.JTextComponent.KeyBinding[]) {
            javax.swing.text.JTextComponent.KeyBinding[] keyBindsVal = (javax.swing.text.JTextComponent.KeyBinding[]) uidefs
                    .get(keys[i]);/*from w  ww  .j  a v a  2s.c  o  m*/
        } else if (v instanceof InputMap) {
            InputMap imapVal = (InputMap) uidefs.get(keys[i]);
        } else {
            System.out.println("Unknown type");
        }
    }
}

From source file:Main.java

public static void flattenSplitPane(JSplitPane jSplitPane) {
    UIDefaults defaults = javax.swing.UIManager.getDefaults();
    final Color light = defaults.getColor("SplitPane.highlight");
    final Color dark = defaults.getColor("SplitPane.darkShadow");

    // */*from ww w  .  java 2s.c o m*/
    jSplitPane.setUI(new BasicSplitPaneUI() {
        public BasicSplitPaneDivider createDefaultDivider() {
            BasicSplitPaneDivider divider = new BasicSplitPaneDivider(this) {
                private static final long serialVersionUID = 1L;

                @Override
                public int getDividerSize() {
                    return 5;
                }

                @Override
                public void paint(Graphics g) {
                    // super.paint(g);
                    int orientation = this.getBasicSplitPaneUI().getOrientation();

                    Dimension size = this.getSize();

                    if (orientation == JSplitPane.VERTICAL_SPLIT) {
                        int[] lines = new int[2];
                        lines[0] = 0;
                        lines[1] = size.height - 2;

                        for (int i = 0; i < size.width; i += 4) {
                            for (int j = 0; j < lines.length; j++) {
                                int y = lines[j];
                                g.setColor(light);
                                g.fillRect(i, y, 2, 2);
                                g.setColor(dark);
                                g.fillRect(i, y, 1, 1);
                            }
                        }
                    } else {
                        int[] rows = new int[2];
                        rows[0] = 0;
                        rows[1] = size.width - 2;

                        for (int i = 0; i < size.height; i += 4) {
                            for (int j = 0; j < rows.length; j++) {
                                int x = rows[j];
                                g.setColor(light);
                                g.fillRect(x, i, 2, 2);
                                g.setColor(dark);
                                g.fillRect(x, i, 1, 1);
                            }
                        }
                    }
                }
            };
            return divider;
        }
    });
    jSplitPane.setBorder(null);
    // */
}

From source file:UIUtilities.java

/**
 * Set up the user interface./*  w  w  w  .  j  a  v a2  s  .c o m*/
 */
public static void setupUI() {
    try {
        final String classname = UIManager.getSystemLookAndFeelClassName();
        UIManager.setLookAndFeel(classname);
    } catch (Exception e) {
        e.printStackTrace();
    }

    final UIDefaults defaults = UIManager.getDefaults();

    defaults.put("PopupMenu.border", new BorderUIResource.EtchedBorderUIResource(EtchedBorder.RAISED,
            defaults.getColor("controlShadow"), defaults.getColor("controlLtHighlight")));

    final MatteBorder matteborder = new MatteBorder(1, 1, 1, 1, Color.black);
    final EmptyBorder emptyborder = new MatteBorder(2, 2, 2, 2, defaults.getColor("control"));
    final BorderUIResource.CompoundBorderUIResource compBorder = new BorderUIResource.CompoundBorderUIResource(
            emptyborder, matteborder);
    final BorderUIResource.EmptyBorderUIResource emptyBorderUI = new BorderUIResource.EmptyBorderUIResource(0,
            0, 0, 0);
    defaults.put("SplitPane.border", emptyBorderUI);
    defaults.put("Table.scrollPaneBorder", emptyBorderUI);
    defaults.put("ComboBox.border", compBorder);
    defaults.put("TextField.border", compBorder);
    defaults.put("TextArea.border", compBorder);
    defaults.put("CheckBox.border", compBorder);
    defaults.put("ScrollPane.border", emptyBorderUI);

}

From source file:org.pgptool.gui.ui.tools.UiUtils.java

public static void setLookAndFeel() {
    // NOTE: We doing it this way to prevent dead=locks that is sometimes
    // happens if do it in main thread
    Edt.invokeOnEdtAndWait(new Runnable() {
        @Override//w w w.j  av  a  2 s.  c o m
        public void run() {
            try {
                UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                fixCheckBoxMenuItemForeground();
                fixFontSize();
            } catch (Throwable t) {
                log.error("Failed to set L&F", t);
            }
        }

        /**
         * In some cases (depends on OS theme) check menu item foreground is same as
         * bacground - thus it;'s invisible when cheked
         */
        private void fixCheckBoxMenuItemForeground() {
            UIDefaults defaults = UIManager.getDefaults();
            Color selectionForeground = defaults.getColor("CheckBoxMenuItem.selectionForeground");
            Color foreground = defaults.getColor("CheckBoxMenuItem.foreground");
            Color background = defaults.getColor("CheckBoxMenuItem.background");
            if (colorsDiffPercentage(selectionForeground, background) < 10) {
                // TODO: That doesn't actually affect defaults. Need to find out how to fix it
                defaults.put("CheckBoxMenuItem.selectionForeground", foreground);
            }
        }

        private int colorsDiffPercentage(Color c1, Color c2) {
            int diffRed = Math.abs(c1.getRed() - c2.getRed());
            int diffGreen = Math.abs(c1.getGreen() - c2.getGreen());
            int diffBlue = Math.abs(c1.getBlue() - c2.getBlue());

            float pctDiffRed = (float) diffRed / 255;
            float pctDiffGreen = (float) diffGreen / 255;
            float pctDiffBlue = (float) diffBlue / 255;

            return (int) ((pctDiffRed + pctDiffGreen + pctDiffBlue) / 3 * 100);
        }

        private void fixFontSize() {
            if (isJreHandlesScaling()) {
                log.info("JRE handles font scaling, won't change it");
                return;
            }
            log.info("JRE doesnt't seem to support font scaling");

            Toolkit toolkit = Toolkit.getDefaultToolkit();
            int dpi = toolkit.getScreenResolution();
            if (dpi == 96) {
                if (log.isDebugEnabled()) {
                    Font font = UIManager.getDefaults().getFont("TextField.font");
                    String current = font != null ? Integer.toString(font.getSize()) : "unknown";
                    log.debug(
                            "Screen dpi seem to be 96. Not going to change font size. Btw current size seem to be "
                                    + current);
                }
                return;
            }
            int targetFontSize = 12 * dpi / 96;
            log.debug("Screen dpi = " + dpi + ", decided to change font size to " + targetFontSize);
            setDefaultSize(targetFontSize);
        }

        private boolean isJreHandlesScaling() {
            try {
                JreVersion noNeedToScaleForVer = JreVersion.parseString("9");
                String jreVersionStr = System.getProperty("java.version");
                if (jreVersionStr != null) {
                    JreVersion curVersion = JreVersion.parseString(jreVersionStr);
                    if (noNeedToScaleForVer.compareTo(curVersion) <= 0) {
                        return true;
                    }
                }

                return false;
            } catch (Throwable t) {
                log.warn("Failed to see oif JRE can handle font scaling. Will assume it does. JRE version: "
                        + System.getProperty("java.version"), t);
                return true;
            }
        }

        public void setDefaultSize(int size) {
            Set<Object> keySet = UIManager.getLookAndFeelDefaults().keySet();
            Object[] keys = keySet.toArray(new Object[keySet.size()]);
            for (Object key : keys) {
                if (key != null && key.toString().toLowerCase().contains("font")) {
                    Font font = UIManager.getDefaults().getFont(key);
                    if (font != null) {
                        Font changedFont = font.deriveFont((float) size);
                        UIManager.put(key, changedFont);
                        Font doubleCheck = UIManager.getDefaults().getFont(key);
                        log.debug("Font size changed for " + key + ". From " + font.getSize() + " to "
                                + doubleCheck.getSize());
                    }
                }
            }
        }
    });
    log.info("L&F set");
}