Set Action (and other values)
Set Action Command Text
Hide the Text of the Action
Do Click
Change Multi-Click Threshhold
Print if Button is the Default
Set Default Button of the RootPane
Disable Default Capable
Set Mnemonic
Set Displayed Mnemonic
Set Button Group
Set New Model
Set Action Command Text
Hide the Text of the Action
Do Click
Change Multi-Click Threshhold
Print if Button is the Default
Set Default Button of the RootPane
Disable Default Capable
Set Mnemonic
Set Displayed Mnemonic
Set Button Group
Set New Model
Set Action (and other values)
Action action = new AbstractAction(){
@Override
public void actionPerformed(ActionEvent e) {
componentTemplate.addTextToTextArea("The New Action ");
}
};
ImageIcon icon = new ImageIcon(getClass().getResource("/new_icon.png"));
action.putValue(Action.LARGE_ICON_KEY, icon);
action.putValue(Action.NAME, "The New Action");
action.putValue(Action.ACTION_COMMAND_KEY, "Action Command from Action");
action.setEnabled(true);
jButton1.setAction(action);
Reset:
jButton1.setAction(null);
Notes:
This method can be used to set these values of a JButton:
Action.NAME - sets the text
Action.SHORT_DESCRIPTION - tooltip
Action.ACTION_COMMAND_KEY
Action.MNEMONIC_KEY
Action.DISPLAYED_MNEMONIC_INDEX_KEY
Action.LARGE_ICON_KEY - primary icon
also, can set the Enabled state
Set Action Command Text
jButton1.setActionCommand("The New Action Command Text");
Reset:
jButton1.setActionCommand(null);
Notes:
Sets the text that is passed to the Action Event when the Button is Clicked.
Hide the Text of the Action
jButton1.setHideActionText(true);
Reset:
jButton1.setHideActionText(false);
Notes:
Hide the Button text that was set specifically from the Action.
Do Click
jButton1.doClick();
//reset
// jButton1.doClick();
Change Multi-Click Threshhold
jButton1.setMultiClickThreshhold(2000);
Reset:
jButton1.setMultiClickThreshhold(0);
Notes:
Ignore any manual mouse clicks that happen that are less than the number of milliseconds (e.g. 2000ms) since the last mouse click
Print if Button is the Default
System.out.println("Is button the Default Button: "+jButton1.isDefaultButton());
Description:
Whether or not this JButton will get the default focus if the parent component gets focus
Set Default Button of the RootPane
JRootPane root = SwingUtilities.getRootPane(jButton1);
if(root != null){
root.setDefaultButton(jButton1);
}
Reset:
JRootPane root = SwingUtilities.getRootPane(jButton1);
if(root != null){
root.setDefaultButton(null);
}
Disable Default Capable
jButton1.setDefaultCapable(false);
Reset:
jButton1.setDefaultCapable(true);
Notes:
This flag is checked when the button gains focus
Set Mnemonic
jButton1.setMnemonic('R');
Description:
Assigning the character 'R', for example, requires typing Alt+R to perform a click on this JButton
Reset:
jButton1.setMnemonic(-1);
Note:
This also resets the Displayed Mnemonic Index
Set Displayed Mnemonic
int index = 1;
if(jButton1.getText() != null && jButton1.getText().length() >= (index+1)){
jButton1.setDisplayedMnemonicIndex(index);
}
Description:
Places an underscore on the text character at the index
Reset:
jButton1.setDisplayedMnemonicIndex(-1);
Notes:
Doesn't show up when the JButton is using HTML text
The provided index must be less than the text length
Set Button Group
((DefaultButtonModel)jButton1.getModel()).setGroup(new ButtonGroup());
Reset:
((DefaultButtonModel)jButton1.getModel()).setGroup(null);
Notes:
Not useful for non-toggle buttons (like this one)
Set New Model
jButton1.setModel(new DefaultButtonModel());
//reset
// jButton1.setModel();