Java Swing How to - extends DefaultMetalTheme








Question

We would like to know how to extends DefaultMetalTheme.

Answer

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JRootPane;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;
import javax.swing.plaf.ColorUIResource;
import javax.swing.plaf.metal.DefaultMetalTheme;
import javax.swing.plaf.metal.MetalLookAndFeel;
//from w  ww.  j av  a  2 s . co m
public class Main {
  public static void main_helper(String args[]) {
    JFrame f = new JFrame();
    f.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
    f.setSize(300, 300);

    f.setUndecorated(true);
    f.getRootPane().setWindowDecorationStyle(JRootPane.FRAME);

    JPanel panel = new JPanel();
    panel.setBackground(java.awt.Color.white);
    f.setContentPane(panel);

    MetalLookAndFeel.setCurrentTheme(new MyDefaultMetalTheme());

    try {
      UIManager.setLookAndFeel(new MetalLookAndFeel());
    } catch (Exception e) {
      e.printStackTrace();
    }

    SwingUtilities.updateComponentTreeUI(f);

    f.setVisible(true);

  }

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

class MyDefaultMetalTheme extends DefaultMetalTheme {
  // inactive title color
  public ColorUIResource getWindowTitleInactiveBackground() {
    return new ColorUIResource(java.awt.Color.orange);
  }

  // active title color
  public ColorUIResource getWindowTitleBackground() {
    return new ColorUIResource(java.awt.Color.orange);
  }

  // start ActiveBumps
  public ColorUIResource getPrimaryControlHighlight() {
    return new ColorUIResource(java.awt.Color.orange);
  }

  public ColorUIResource getPrimaryControlDarkShadow() {
    return new ColorUIResource(java.awt.Color.orange);
  }

  public ColorUIResource getPrimaryControl() {
    return new ColorUIResource(java.awt.Color.orange);
  }

  // end ActiveBumps

  // start inActiveBumps
  public ColorUIResource getControlHighlight() {
    return new ColorUIResource(java.awt.Color.orange);
  }

  public ColorUIResource getControlDarkShadow() {
    return new ColorUIResource(java.awt.Color.orange);
  }

  public ColorUIResource getControl() {
    return new ColorUIResource(java.awt.Color.orange);
  }
  // end inActiveBumps

}