Java Swing How to - List all actions associated with JTextComponent








Question

We would like to know how to list all actions associated with JTextComponent.

Answer

//  w  w w  .  j  a v a  2s . com
   
import java.util.Arrays;
import java.util.Comparator;

import javax.swing.Action;
import javax.swing.JTextField;
import javax.swing.text.JTextComponent;

public class Main {
  public static void main(String args[]) {
    JTextComponent component = new JTextField();

    // Process action list
    Action actions[] = component.getActions();
    // Define comparator to sort actions
    Comparator<Action> comparator = new Comparator<Action>() {
      public int compare(Action a1, Action a2) {
        String firstName = (String) a1.getValue(Action.NAME);
        String secondName = (String) a2.getValue(Action.NAME);
        return firstName.compareTo(secondName);
      }
    };
    Arrays.sort(actions, comparator);

    int count = actions.length;
    System.out.println("Count: " + count);
    for (int i = 0; i < count; i++) {
      System.out.printf("%28s : %s\n",actions[i].getValue(Action.NAME),actions[i].getClass().getName());
    }
  }
}