Example usage for javax.swing.plaf FontUIResource deriveFont

List of usage examples for javax.swing.plaf FontUIResource deriveFont

Introduction

In this page you can find the example usage for javax.swing.plaf FontUIResource deriveFont.

Prototype

public Font deriveFont(float size) 

Source Link

Document

Creates a new Font object by replicating the current Font object and applying a new size to it.

Usage

From source file:Main.java

public static void increaseDefaultFont(float multiplier) {
    for (Enumeration keys = UIManager.getDefaults().keys(); keys.hasMoreElements();) {
        Object key = keys.nextElement();
        Object value = UIManager.get(key);
        if (value != null && value instanceof FontUIResource) {
            FontUIResource fontUIResource = (FontUIResource) value;
            UIManager.put(key, fontUIResource.deriveFont(fontUIResource.getSize() * multiplier));
        }/*  ww w  .  j av a  2 s .  co m*/
    }
}

From source file:jshm.gui.GuiUtil.java

/**
 * This sets the default L&F as well as sets some icons
 *//*from   w w  w .j a v a2 s .c o m*/
public static void init() {
    try {
        // Set the Look & Feel to match the current system
        UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
    } catch (Exception e) {
    }

    // http://www.java2s.com/Tutorial/Java/0240__Swing/CustomizingaJOptionPaneLookandFeel.htm
    UIManager.put("OptionPane.errorIcon",
            new ImageIcon(GuiUtil.class.getResource("/jshm/resources/images/toolbar/delete.png")));
    UIManager.put("OptionPane.informationIcon",
            new ImageIcon(GuiUtil.class.getResource("/jshm/resources/images/toolbar/infoabout.png")));
    UIManager.put("OptionPane.questionIcon",
            new ImageIcon(GuiUtil.class.getResource("/jshm/resources/images/toolbar/help.png")));
    UIManager.put("OptionPane.warningIcon",
            new ImageIcon(GuiUtil.class.getResource("/jshm/resources/images/toolbar/pause.png")));
    UIManager.put("OptionPane.yesIcon",
            new ImageIcon(GuiUtil.class.getResource("/jshm/resources/images/toolbar/accept32.png")));
    UIManager.put("OptionPane.noIcon",
            new ImageIcon(GuiUtil.class.getResource("/jshm/resources/images/toolbar/delete32.png")));

    float scale = Config.getFloat("font.scale");

    if (1f != scale) {
        LOG.finest("setting ui font scale to + " + scale);

        Enumeration<Object> keys = UIManager.getDefaults().keys();
        while (keys.hasMoreElements()) {
            Object key = keys.nextElement();
            Object value = UIManager.get(key);

            if (value instanceof FontUIResource) {
                FontUIResource f = (FontUIResource) value;
                f = new FontUIResource(f.deriveFont(scale * f.getSize2D()));
                UIManager.put(key, f);
            }
        }
    }

    // hook into the wizard displayer
    System.setProperty("WizardDisplayer.default", "jshm.gui.wizards.displayer.WizardDisplayerImpl");
}

From source file:org.yccheok.jstock.gui.JStock.java

/**
 * @param args the command line arguments
 *//*from   w  ww.j  ava  2 s  .  c om*/
public static void main(String args[]) {
    /***********************************************************************
     * UI Manager initialization via JStockOptions.
     **********************************************************************/
    final JStockOptions jStockOptions = getJStockOptionsViaXML();

    // OSX menu bar at top.
    if (Utils.isMacOSX()) {
        System.setProperty("apple.laf.useScreenMenuBar", "true");
        System.setProperty("apple.awt.brushMetalLook", "true");
    }

    boolean uiManagerLookAndFeelSuccess = false;
    try {
        String lookNFeel = jStockOptions.getLooknFeel();
        if (null != lookNFeel) {
            UIManager.setLookAndFeel(lookNFeel);
            uiManagerLookAndFeelSuccess = true;
        }
    } catch (java.lang.ClassNotFoundException | java.lang.InstantiationException
            | java.lang.IllegalAccessException | javax.swing.UnsupportedLookAndFeelException exp) {
        log.error(null, exp);
    }

    if (!uiManagerLookAndFeelSuccess) {
        String className = Utils.setDefaultLookAndFeel();
        if (null != className) {
            final String lookNFeel = jStockOptions.getLooknFeel();
            // When jStockOptions.getLookNFeel returns null, it means we wish
            // to use system default value. Hence, don't overwrite the null value,
            // so that we can use the same jStockOptions, across different
            // platforms.
            if (lookNFeel != null) {
                jStockOptions.setLooknFeel(className);
            }
        }
    }

    /***********************************************************************
     * Ensure correct localization.
     **********************************************************************/
    // This global effect, should just come before anything else, 
    // after we get an instance of JStockOptions.
    Locale.setDefault(jStockOptions.getLocale());

    /***********************************************************************
     * Single application instance enforcement.
     **********************************************************************/
    if (false == AppLock.lock()) {
        final int choice = JOptionPane.showOptionDialog(null,
                MessagesBundle.getString("warning_message_running_2_jstock"),
                MessagesBundle.getString("warning_title_running_2_jstock"), JOptionPane.YES_NO_OPTION,
                JOptionPane.WARNING_MESSAGE, null,
                new String[] { MessagesBundle.getString("yes_button_running_2_jstock"),
                        MessagesBundle.getString("no_button_running_2_jstock") },
                MessagesBundle.getString("no_button_running_2_jstock"));
        if (choice != JOptionPane.YES_OPTION) {
            System.exit(0);
            return;
        }
    }

    // Avoid "JavaFX IllegalStateException when disposing JFXPanel in Swing"
    // http://stackoverflow.com/questions/16867120/javafx-illegalstateexception-when-disposing-jfxpanel-in-swing
    Platform.setImplicitExit(false);

    // As ProxyDetector is affected by system properties
    // http.proxyHost, we are forced to initialized ProxyDetector right here,
    // before we manually change the system properties according to
    // JStockOptions.
    ProxyDetector.getInstance();

    /***********************************************************************
     * Apply large font if possible.
     **********************************************************************/
    if (jStockOptions.useLargeFont()) {
        java.util.Enumeration keys = UIManager.getDefaults().keys();
        while (keys.hasMoreElements()) {
            Object key = keys.nextElement();
            Object value = UIManager.get(key);
            if (value != null && value instanceof javax.swing.plaf.FontUIResource) {
                javax.swing.plaf.FontUIResource fr = (javax.swing.plaf.FontUIResource) value;
                UIManager.put(key, new javax.swing.plaf.FontUIResource(
                        fr.deriveFont((float) fr.getSize2D() * (float) Constants.FONT_ENLARGE_FACTOR)));
            }
        }
    }

    /***********************************************************************
     * GA tracking.
     **********************************************************************/
    GA.trackAsynchronously("main");

    java.awt.EventQueue.invokeLater(new Runnable() {
        @Override
        public void run() {
            final JStock mainFrame = JStock.instance();

            // We need to first assign jStockOptions to mainFrame, as during
            // Utils.migrateXMLToCSVPortfolios, we will be accessing mainFrame's
            // jStockOptions.
            mainFrame.initJStockOptions(jStockOptions);

            mainFrame.init();
            mainFrame.setVisible(true);
            mainFrame.updateDividerLocation();
            mainFrame.requestFocusOnJComboBox();
        }
    });
}

From source file:shuffle.fwk.ShuffleController.java

/**
 * Creates a ShuffleController with the given configuration paths for the primary configuration
 * (which tells other managers where to get their configurations). If there are none passed, then
 * "config/main.txt" is assumed.//from   ww w.  j  a va2  s.  c o m
 * 
 * @param configPaths
 *           The paths as Strings
 */
public ShuffleController(String... configPaths) {
    if (configPaths.length > 0 && configPaths[0] != null) {
        factory = new ConfigFactory(configPaths[0]);
    } else {
        factory = new ConfigFactory();
    }
    Integer menuFontOverride = getPreferencesManager().getIntegerValue(KEY_FONT_SIZE_SCALING);
    if (menuFontOverride != null && menuFontOverride != 100 && menuFontOverride >= 1
            && menuFontOverride <= 10000) {
        float scale = menuFontOverride.floatValue() / 100.0f;
        try {
            // This is the cleanest and most bug-free way to do this hack.
            Set<Object> allKeys = new HashSet<Object>();
            allKeys.add("JMenu.font");
            // Yes we're not supposed to use this, but it is the only one that works with Nimbus LAF
            allKeys.addAll(UIManager.getLookAndFeelDefaults().keySet());
            Object value = UIManager.get("defaultFont");
            if (value != null && value instanceof FontUIResource) {
                FontUIResource fromFont = (javax.swing.plaf.FontUIResource) value;
                FontUIResource toFont = new FontUIResource(fromFont.deriveFont(fromFont.getSize() * scale));
                // This one is necessary
                UIManager.getLookAndFeel().getDefaults().put("defaultFont", toFont);
                // And this one allows other LAF to be used in the future
                UIManager.getDefaults().put("defaultFont", toFont);
            }

            // Needed for Nimbus's JTable row height adjustment
            Object tableFontValue = UIManager.getLookAndFeel().getDefaults().get("Table.font");
            Number bestRowHeight = null;
            if (tableFontValue != null && tableFontValue instanceof FontUIResource) {
                FontUIResource fromFont = (FontUIResource) tableFontValue;
                bestRowHeight = fromFont.getSize();
            }
            Object rowHeightValue = UIManager.getLookAndFeel().getDefaults().get("Table.rowHeight");
            if (rowHeightValue != null && rowHeightValue instanceof Number) {
                Number rowHeight = (Number) rowHeightValue;
                rowHeight = rowHeight.doubleValue() * scale;
                if (bestRowHeight == null || bestRowHeight.intValue() < rowHeight.intValue()) {
                    bestRowHeight = rowHeight;
                }
            }
            if (bestRowHeight != null) {
                bestRowHeight = bestRowHeight.doubleValue() * (4.0 / 3.0);
            }
            if (bestRowHeight != null && bestRowHeight.intValue() > 0) {
                UIManager.getLookAndFeel().getDefaults().put("Table.rowHeight", bestRowHeight.intValue());
            }
        } catch (Exception e) {
            StringWriter sw = new StringWriter();
            PrintWriter pw = new PrintWriter(sw);
            e.printStackTrace(pw);
            LOG.log(Level.SEVERE, "Cannot override menu font sizes!", e);
        }
    }
    try {
        setModel(new ShuffleModel(this));
        setView(new ShuffleView(this));
        getModel().checkLocaleConfig();
        loadFrame();
    } catch (Exception e) {
        StringWriter sw = new StringWriter();
        PrintWriter pw = new PrintWriter(sw);
        e.printStackTrace(pw);
        LOG.log(Level.SEVERE, "Failure on start:", e);
    }
}