JButtonのテキストとしてActionの名前を適用しないよう設定する
Total: 3470, Today: 1, Yesterday: 0
Posted by aterai at
Last-modified:
Summary
JButtonにActionを設定したとき、そのアクション名をJButtonのテキストとして適用しないよう設定します。
Screenshot

Advertisement
Source Code Examples
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, KotlinDescription
上記のサンプルでは、JButton#setHideActionText(false)を設定した状態でActionを変更した場合などのテストを行っています。
JButton#setHideActionText(false)を設定した状態でActionを変更した場合、そのアクション名やアイコンなどはJButtonに適用されないJButton.setText(...)、JButton.setIcon(...)の設定が優先される
JButton#setHideActionText(false)を設定した状態でもActionをnull(初期状態)に変更するとJButtonのテキストやアイコンはクリアされてしまう- アクションから名前とアイコンが適用された状態で
JButton#setHideActionText(false)を実行した場合、JButtonのテキストはクリアされるがアイコンは変化しないJButton.setText(...)、JButton.setIcon(...)でテキストやアイコンを設定している場合も同様にJButtonのテキストはクリアされるが、アイコンは変化しない
JButton#setHideActionText(...)の状態にJButton.setText(...)、JButton.setIcon(...)は依存しない