概要

NimbusLookAndFeelJScrollPaneのビューポートにテキストコンポーネントがひとつだけ配置されている場合、そのフォーカスボーダーを親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に適用しない
    • テキストコンポーネントがJLayerでラップされている場合でも無効で内部のテキストコンポーネントにフォーカスボーダーが適用される

参考リンク

コメント