概要

JButtonActionを設定したとき、そのアクション名をJButtonのテキストとして適用しないよう設定します。

サンプルコード

Action pasteAction = new DefaultEditorKit.PasteAction();
pasteAction.putValue(Action.LARGE_ICON_KEY, new ColorIcon(Color.GREEN));

JButton button = new JButton("text");
button.setFocusable(false);
button.setAction(pasteAction);
button.setIcon(new ColorIcon(Color.RED));
button.addActionListener(e -> Toolkit.getDefaultToolkit().beep());

button.setHideActionText(false);
View in GitHub: Java, Kotlin

解説

上記のサンプルでは、JButton#setHideActionText(false)を設定した状態でActionを変更した場合などのテストを行っています。

  • JButton#setHideActionText(false)を設定した状態でActionを変更した場合、そのアクション名やアイコンなどはJButtonに適用されない
    • JButton.setText(...)JButton.setIcon(...)の設定が優先される
  • JButton#setHideActionText(false)を設定した状態でもActionnull(初期状態)に変更するとJButtonのテキストやアイコンはクリアされてしまう
  • アクションから名前とアイコンが適用された状態でJButton#setHideActionText(false)を実行した場合、JButtonのテキストはクリアされるがアイコンは変化しない
    • JButton.setText(...)JButton.setIcon(...)で設定している場合も同様にJButtonのテキストはクリアされるが、アイコンは変化しない
  • JButton#setHideActionText(...)の状態にJButton.setText(...)JButton.setIcon(...)は依存しない

参考リンク

コメント