Java JButton get action map

Description

Java JButton get action map

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.event.ActionEvent;

import javax.swing.AbstractAction;
import javax.swing.Action;
import javax.swing.JButton;
import javax.swing.JFrame;

public class Main extends JFrame {

   public Main() {
      JButton component = new JButton();
      MyAction action = new MyAction();
      component.getActionMap().put(action.getValue(Action.NAME), action);

      this.getContentPane().add(component, BorderLayout.NORTH);
      setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      this.getContentPane().setBackground(Color.red);
      this.setSize(320, 200);
      this.setVisible(true);
   }//  w  w w.j  a v a 2s .c o  m

   public static void main(String[] args) {
      Main t = new Main();
   }
}

class MyAction extends AbstractAction {
   public MyAction() {
      super("my action");
   }

   public void actionPerformed(ActionEvent e) {
      System.out.println("hi");

   }
}



PreviousNext

Related