Skip to content

Instantly share code, notes, and snippets.

@wchargin
Created February 24, 2016 14:52
Show Gist options
  • Save wchargin/4b551e1850281aae768d to your computer and use it in GitHub Desktop.
Save wchargin/4b551e1850281aae768d to your computer and use it in GitHub Desktop.
look-and-feel optimizer for swing
public class LAFOptimizer
{
/**
* Optimizes the look and feel to integrate with the operating system as
* much as possible.
* <p>
* To do this, this method at the installed look and feels and finds the
* system look and feel. If this is the same as the Metal look and feel, the
* method disables bold fonts to limit eyesores. If the element is the
* Windows look and feel, this method fixes a few known limitations
* contained therein (combo boxes have weird borders; text areas use
* monospaced fonts).
*/
private static void optimizeSwing()
{
// This code stolen from:
// https://github.com/WChargin/laf-optimizer/
String systemLaf = UIManager.getSystemLookAndFeelClassName();
try
{
if (systemLaf.toLowerCase().contains("metal"))
{
// Not much we can do here...just patching the holes.
UIManager.put("swing.boldMetal", Boolean.FALSE);
}
UIManager.setLookAndFeel(systemLaf);
if (systemLaf.toLowerCase().contains("synth")
|| systemLaf.toLowerCase().contains("gtk"))
{
// Don't paint labels above sliders.
UIManager.put("Slider.paintValue", false);
}
}
catch (Exception e)
{
// Last resort.
UIManager.put("swing.boldMetal", Boolean.FALSE);
}
if (systemLaf.toLowerCase().contains("windows"))
{
// Windows LAF uses monospaced fonts for JTextArea.
// Let's fix that.
Font defaultTextAreaFont = UIManager.getFont("TextArea.font");
UIManager.put(
"TextArea.font",
new FontUIResource(Font.SANS_SERIF, defaultTextAreaFont
.getStyle(), defaultTextAreaFont.getSize()));
// Again, Windows messes up combo box display with an "XPFillBorder"
if (UIManager.getBorder("ComboBox.border").getClass().getName()
.contains("XPFillBorder"))
{
UIManager.put("ComboBox.border", new EmptyBorder(0, 0, 0, 0));
}
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment