Set any property for the JButton with the Action object - Java Swing

Java examples for Swing:JButton

Introduction

To set any property for the JButton with the Action object, use putValue(String key, Object value) method of the Action interface.

The following snippet of code sets the tool tip text and mnemonic key for the object closeAction:

Demo Code

import java.awt.event.ActionEvent;
import java.awt.event.KeyEvent;

import javax.swing.AbstractAction;
import javax.swing.Action;

public class Main {

  public static void main(String[] args) {
    Action closeAction = new CloseAction();
    closeAction.putValue(Action.SHORT_DESCRIPTION, "Closes the application");
    closeAction.putValue(Action.MNEMONIC_KEY, KeyEvent.VK_C);
  }/* www  .j av a2  s.co  m*/
}

class CloseAction extends AbstractAction {
  public CloseAction() {
    super("Close");
  }

  @Override
  public void actionPerformed(ActionEvent event) {
    System.exit(0);
  }
}

Related Tutorials