• category: swing folder: ScrollPaneUseChildTextComponentFocus title: NimbusLookAndFeelで子テキストコンポーネントのフォーカスボーダーを親JScrollPaneに適用する tags: [JScrollPane, JTextComponent, NimbusLookAndFeel, Focus, UIManager] author: aterai pubdate: 2022-09-12T03:52:41+09:00 description: NimbusLookAndFeelでJScrollPaneのビューポートにテキストコンポーネントがひとつだけ配置されている場合、そのフォーカスボーダーを親JScrollPaneに適用するかを切り替えます。 image: https://drive.google.com/uc?id=1wHPpe9jRQbjdxqHXxX0WlG6cOm48FUzL

概要

NimbusLookAndFeelでJScrollPaneのビューポートにテキストコンポーネントがひとつだけ配置されている場合、そのフォーカスボーダーを親JScrollPaneに適用するかを切り替えます。

サンプルコード

StringBuilder buf = new StringBuilder();
IntStream.range(0, 100).forEach(i -> buf.append(i).append(LF));
String str = buf.toString();
JScrollPane scroll = new JScrollPane(new JTextArea(str));

String key = "ScrollPane.useChildTextComponentFocus";
JCheckBox check = new JCheckBox(key, UIManager.getBoolean(key));
check.addActionListener(e -> {
  UIManager.put(key, ((JCheckBox) e.getSource()).isSelected());
  SwingUtilities.updateComponentTreeUI(scroll);
});

JPanel p = new JPanel(new GridLayout(1, 2));
p.add(new JScrollPane(new JTextArea(str)));
p.add(scroll);
View in GitHub: Java, Kotlin

解説

  • ScrollPane.useChildTextComponentFocus: true
    • NimbusLookAndFeelのデフォルト
    • JScrollPaneのビューポートにテキストコンポーネントがひとつだけ配置されている場合、そのフォーカスボーダーを親JScrollPaneに適用する
    • SynthScrollPaneUIを継承するScrollPaneUIでのみ有効で、BasicScrollPaneUIを使用するMetalLookAndFeelなどでは無効
  • ScrollPane.useChildTextComponentFocus: false
    • JScrollPaneのビューポートにテキストコンポーネントがひとつだけ配置されている場合でも、そのフォーカスボーダーを親JScrollPaneに適用しない

参考リンク

コメント