• 追加された行はこの色です。
  • 削除された行はこの色です。
---
category: swing
folder: ComponentSizeVariant
title: NimbusLookAndFeelでJComponentのサイズを変更する
tags: [NimbusLookAndFeel, JComponent, Font]
author: aterai
pubdate: 2014-07-28T00:18:57+09:00
description: NimbusLookAndFeelを使用している場合、クライアントプロパティを設定することでJComponentの表示サイズを変更することが出来ます。
description: NimbusLookAndFeelを使用している場合、コンポーネントの表示サイズをクライアントプロパティ設定で変更可能です。
image: https://lh6.googleusercontent.com/-Uru9Oco4olg/U9T5RHzJ_5I/AAAAAAAACKY/7N7a3Ya3sYw/s800/ComponentSizeVariant.png
---
* 概要 [#summary]
`NimbusLookAndFeel`を使用している場合、クライアントプロパティを設定することで`JComponent`の表示サイズを変更することが出来ます。
`NimbusLookAndFeel`を使用している場合、コンポーネントの表示サイズをクライアントプロパティ設定で変更可能です。

#download(https://lh6.googleusercontent.com/-Uru9Oco4olg/U9T5RHzJ_5I/AAAAAAAACKY/7N7a3Ya3sYw/s800/ComponentSizeVariant.png)

* サンプルコード [#sourcecode]
#code(link){{
JMenu menu = new JMenu("Resizing a Component");
ButtonGroup bg = new ButtonGroup();
Stream.of("regular", "mini", "small", "large")
    .forEach(cmd -> menu.add(createSizeVariantItem(cmd, bg)));
// ...
booleanRenderer.putClientProperty("JComponent.sizeVariant", "mini");
}}

* 解説 [#explanation]
- `NimbusLookAndFeel`を使用している場合、`JComponent#putClientProperty("JComponent.sizeVariant", "mini");`のようにクライアントプロパティを設定することでコンポーネントの表示サイズが変更可能
-- デフォルトは`regular`、その他は小さい順に`mini`, `small`, `large`が設定可能
-- `JComponent#updateUI()`を実行しないと、更新されない
--- このサンプルでは、`SwingUtilities.updateComponentTreeUI(window);`で実行
-- フォントがユーザー指定されている場合は、文字サイズは更新されない?
--- このサンプルでは、`updateUI()`の前に、フォントを`jc.setFont(new FontUIResource(jc.getFont()));`で`UIResource`化することで回避している
- `NimbusLookAndFeel`を使用している場合`JComponent#putClientProperty("JComponent.sizeVariant", "mini")`のようにクライアントプロパティを設定することでコンポーネントの表示サイズが変更可能
-- デフォルトは`regular`、その他は小さい順に`mini`、`small`、`large`が設定可能
-- `JComponent#updateUI()`を実行しないと更新されない
--- このサンプルでは`SwingUtilities.updateComponentTreeUI(window)`で実行
-- コンポーネントにフォントがユーザ指定されている場合は文字サイズは更新されない
--- このサンプルでは`updateUI()`の前にフォントを`c.setFont(new FontUIResource(c.getFont()))`で`UIResource`化することで回避
-- `JTable`の`BooleanRenderer`が二回目以降変更されない
--- 初回の`JCheckBox booleanRenderer = (JCheckBox) table.getDefaultRenderer(Boolean.class); booleanRenderer.putClientProperty("JComponent.sizeVariant", "mini");`は、正常に動作しているように見える

* 参考リンク [#reference]
- [http://docs.oracle.com/javase/tutorial/uiswing/lookandfeel/size.html Resizing a Component (The Java™ Tutorials > Creating a GUI With JFC/Swing > Modifying the Look and Feel)]
- [https://docs.oracle.com/javase/tutorial/uiswing/lookandfeel/size.html Resizing a Component (The Java™ Tutorials > Creating a GUI With JFC/Swing > Modifying the Look and Feel)]

* コメント [#comment]
#comment
#comment