• 追加された行はこの色です。
  • 削除された行はこの色です。
TITLE:Look and Feelの変更
#navi(../)
RIGHT:Posted by [[terai]] at 2003-10-22
*Look and Feelの変更 [#a86ced2d]
アプリケーションのLook and Feelを変更します。以下のサンプルコードは、''%JAVA_HOME%/demo/jfc/SwingSet2/src/SwingSet2.java'' から引用改変したものです。
---
category: swing
folder: LookAndFeel
title: Look and Feelの変更
tags: [LookAndFeel, JMenuBar]
author: aterai
pubdate: 2003-11-24
description: メニューバーから選択したLook and Feelを起動中のアプリケーションに適用します。
image: https://lh4.googleusercontent.com/_9Z4BYR88imo/TQTPf78s81I/AAAAAAAAAeE/DIOTnqtAOnY/s800/LookAndFeel.png
---
* 概要 [#summary]
メニューバーから選択した`Look and Feel`を起動中のアプリケーションに適用します。

-&jnlp;
-&jar;
-&zip;
#download(https://lh4.googleusercontent.com/_9Z4BYR88imo/TQTPf78s81I/AAAAAAAAAeE/DIOTnqtAOnY/s800/LookAndFeel.png)

#screenshot

**サンプルコード [#d19635f9]
#code{{
//%JAVA_HOME%/demo/jfc/SwingSet2/src/SwingSet2.java
* サンプルコード [#sourcecode]
#code(link){{
// %JAVA_HOME%/demo/jfc/SwingSet2/src/SwingSet2.java
// Possible Look & Feels
private static final String mac     = "com.sun.java.swing.plaf.mac.MacLookAndFeel";
private static final String metal   = "javax.swing.plaf.metal.MetalLookAndFeel";
private static final String motif   = "com.sun.java.swing.plaf.motif.MotifLookAndFeel";
private static final String windows = "com.sun.java.swing.plaf.windows.WindowsLookAndFeel";
private static final String gtk     = "com.sun.java.swing.plaf.gtk.GTKLookAndFeel";
private static final String nimbus  = "com.sun.java.swing.plaf.nimbus.NimbusLookAndFeel";
private static final String MAC = "com.sun.java.swing.plaf.mac.MacLookAndFeel";
private static final String METAL = "javax.swing.plaf.metal.MetalLookAndFeel";
private static final String MOTIF = "com.sun.java.swing.plaf.motif.MotifLookAndFeel";
private static final String WINDOWS = "com.sun.java.swing.plaf.windows.WindowsLookAndFeel";
private static final String GTK = "com.sun.java.swing.plaf.gtk.GTKLookAndFeel";
// JDK 1.6.0_10
// private static final String NIMBUS  = "com.sun.java.swing.plaf.nimbus.NimbusLookAndFeel";
// JDK 1.7.0
private static final String NIMBUS  = "javax.swing.plaf.nimbus.NimbusLookAndFeel";

// The current Look & Feel
private static String currentLookAndFeel = metal;
private static String currentLookAndFeel = METAL;
private final ButtonGroup lafMenuGroup = new ButtonGroup();
public JMenuItem createLafMenuItem(JMenu menu, String label, String laf) {
  JMenuItem mi = (JRadioButtonMenuItem) menu.add(new JRadioButtonMenuItem(label));
  JMenuItem mi = menu.add(new JRadioButtonMenuItem(label));
  lafMenuGroup.add(mi);
  mi.addActionListener(new ChangeLookAndFeelAction(laf));
  mi.setEnabled(isAvailableLookAndFeel(laf));
  return mi;
}

protected static boolean isAvailableLookAndFeel(String laf) {
  try{
  try {
    Class lnfClass = Class.forName(laf);
    LookAndFeel newLAF = (LookAndFeel)(lnfClass.newInstance());
    LookAndFeel newLAF = (LookAndFeel) lnfClass.newInstance();
    return newLAF.isSupportedLookAndFeel();
  }catch(Exception e) {
  } catch (Exception e) {
    return false;
  }
}
}}
#code{{
private class ChangeLookAndFeelAction extends AbstractAction{

// ...
private class ChangeLookAndFeelAction extends AbstractAction {
  private final String laf;
  protected ChangeLookAndFeelAction(String laf) {
    super("ChangeTheme");
    this.laf = laf;
  }
  public void actionPerformed(ActionEvent e) {

  @Override public void actionPerformed(ActionEvent e) {
    setLookAndFeel(laf);
  }
}

private void setLookAndFeel(String laf) {
  if(currentLookAndFeel.equals(laf)) return;
  if (currentLookAndFeel.equals(laf)) return;
  currentLookAndFeel = laf;
  try{
  try {
    UIManager.setLookAndFeel(currentLookAndFeel);
    SwingUtilities.updateComponentTreeUI(frame);
  }catch(Exception ex) {
  } catch (Exception ex) {
    ex.printStackTrace();
    System.out.println("Failed loading L&F: " + currentLookAndFeel);
  }
}
}}

**解説 [#hf60bcb1]
上記のサンプルでは、metal、motif、windowsなどのLookAndFeelを予め用意しておき、isAvailableLookAndFeel(laf)メソッドで、それらが実行した環境で使用できるかを調べてメニューの選択可不可を変更しています。
* 解説 [#explanation]
上記のサンプルでは、`metal`、`motif`、`windows`などの`LookAndFeel`を予め用意しておき、`isAvailableLookAndFeel(...)`メソッドでそれらが実行した環境で使用できるかを調べてメニューの選択可・不可を変更しています。

----
SwingSet3では、UIManager.getInstalledLookAndFeels()メソッドを使用して、使用可能なLookAndFeelのリストを取得してメニューに表示しています。
-[[LookAndFeelの一覧を取得する>Swing/InstalledLookAndFeels]]
- `Look and Feel`を切り替えて各種コンポーネントの表示などを比較したい場合は`%JAVA_HOME%/demo/jfc/SwingSet2/SwingSet2.jar`が便利
- `SwingSet3`では`UIManager.getInstalledLookAndFeels()`メソッドを使って利用可能な`LookAndFeel`の一覧を取得しこれをメニューに表示している
-- [[LookAndFeelの一覧を取得する>Swing/InstalledLookAndFeels]]
- `java.exe`などのコマンドオプションでデフォルトの`Look and Feel`を変更する場合は以下のように指定する
 java.exe -Dswing.defaultlaf=com.sun.java.swing.plaf.windows.WindowsLookAndFeel App

----
Look and Feelを切り替えて、いろんなコンポーネントの見栄えを比較したい場合は、SwingSet2((''%JAVA_HOME%\demo\jfc\SwingSet2\SwingSet2.jar'')) が手軽で分かりやすいと思います。

----
切り替えではなく、ネイティブの Look and Feel に変更したい場合は、UIManager.getSystemLookAndFeelClassName()で実装クラスの名前を取得し、これを設定してやります。
- `%JAVA_HOME%/lib`以下に`swing.properties`を作ってデフォルトの`Look and Feel`を指定する方法もある
-- [https://docs.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html#properties Specifying the Look and Feel: swing.properties File]
- `SystemLookAndFeel`を使用する場合は`UIManager.getSystemLookAndFeelClassName()`でその実装クラス名を取得して設定する
#code{{
try{
  //UIManager.getInstalledLookAndFeels();
try {
  UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
}catch(Exception e) {e.printStackTrace();}
} catch (Exception e) {
  e.printStackTrace();
}
}}

Ubuntu(GNOME) で、上記のようにネイティブL&F を指定すると、NullPointerException が発生する場合は、直前にUIManager.getInstalledLookAndFeels()を呼んでおくと回避できるようです。
-[[JDK1.6 で使うと JDK 側のバグ (6389282) に引っかかってしまうのか NPE>http://blogs.sun.com/katakai/entry/omegat_in_mdi_mode]]
-[[Java6 の痛いバグ… NetBeans デスクトップアプリが Linux で動かず… - Masaki Katakai's Weblog>http://blogs.sun.com/katakai/entry/bad_issue_for_swing_gtk]]
- %%`Ubuntu(GNOME)`で上記のようにシステム`LookAndFeel`を指定すると`NullPointerException`が発生する場合は、直前に`UIManager.getInstalledLookAndFeels()`を呼んでおくと回避可能%% `6u10`で修正済
-- [https://bugs.openjdk.org/browse/JDK-6389282 Bug ID: 6389282 NPE in GTKLookAndFeel.initSystemColorDefaults() on mustang when remote X11 displaying.]
// -- [http://blogs.sun.com/katakai/entry/omegat_in_mdi_mode JDK1.6 で使うと JDK 側のバグ (6389282) に引っかかってしまうのか NPE]
// -- [http://blogs.sun.com/katakai/entry/bad_issue_for_swing_gtk Java6 の痛いバグ… NetBeans デスクトップアプリが Linux で動かず… - Masaki Katakai's Weblog]

----
オプションでデフォルトのLook and Feelを変更する場合は以下のように指定します。
 java -Dswing.defaultlaf=com.sun.java.swing.plaf.windows.WindowsLookAndFeel App
* 参考リンク [#reference]
- `%JAVA_HOME%/demo/jfc/SwingSet2/src/SwingSet2.java`
- [https://docs.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html How to Set the Look and Feel]
- [[LookAndFeelの一覧を取得する>Swing/InstalledLookAndFeels]]
-- `SwingSet3`からコードを引用したサンプル

%JAVA_HOME%\lib以下に''swing.properties''を作ってデフォルトのLook and Feelを指定する方法もあります。
* コメント [#comment]
#comment
- スクリーンショットなどを更新。 -- &user(aterai); &new{2008-03-28 (金) 21:37:52};

**参考リンク [#w6ef8eac]
-[[How to Set the Look and Feel>http://java.sun.com/docs/books/tutorial/uiswing/lookandfeel/plaf.html]]
-[[LookAndFeelの一覧を取得する>Swing/InstalledLookAndFeels]]
--こちらは、SwingSet3からの引用です。

**コメント [#v06b763b]
- %%Nimbusを追加、%% スクリーンショットなどを更新。 -- [[terai]] &new{2008-03-28 (金) 21:37:52};

#comment