event

List actions of some Components

With this example we shall show you how to create a simple application that lists the actions of some Components you can use in your Java Desktop Application.

You might find it particularly useful if you want to to add a “Help” functionality in your applications.

In short, all you have to do is :

  • Create a String array with the name of the contents you want.
  • Use getActions() method to get the list of all the action that are associated with this component.

Let’s see the code snippets that follow:

package com.javacodegeeks.snippets.desktop;

import java.awt.BorderLayout;
import java.awt.Component;
import java.awt.Container;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ItemListener;
import java.io.PrintWriter;
import java.io.StringWriter;
import java.util.Arrays;
import java.util.Comparator;
import java.util.Enumeration;
import java.util.Vector;
import javax.swing.AbstractButton;
import javax.swing.Action;
import javax.swing.BorderFactory;
import javax.swing.ButtonGroup;
import javax.swing.JEditorPane;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JRadioButton;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.JTextPane;
import javax.swing.border.Border;
import javax.swing.event.ChangeListener;
import javax.swing.text.JTextComponent;

public class Main {

    public static void main(String args[]) {

  JFrame jFrame = new JFrame("Action List");

  Container cPane = jFrame.getContentPane();

  String components[] = {"JTextField", "JPasswordField", "JTextArea", "JTextPane", "JEditorPane"};

  final JTextArea text = new JTextArea();

  text.setEditable(false);

  JScrollPane sPane = new JScrollPane(text);

  cPane.add(sPane, BorderLayout.CENTER);

  ActionListener actListner = new ActionListener() {

@Override

public void actionPerformed(ActionEvent event) {

    String command = event.getActionCommand();

    JTextComponent comp = null;

    if (command.equals("JTextField")) {

  comp = new JTextField();

    } else if (command.equals("JPasswordField")) {

  comp = new JPasswordField();

    } else if (command.equals("JTextArea")) {

  comp = new JTextArea();

    } else if (command.equals("JTextPane")) {

  comp = new JTextPane();

    } else {

  comp = new JEditorPane();

    }

    Action acts[] = comp.getActions();

    Comparator compare = new Comparator() {

  @Override

  public int compare(Object object1, Object object2) {

int result = 0;

if ((object1 instanceof Action) && (object2 instanceof Action)) {

    String fName = (String) ((Action) object1).getValue(Action.NAME);

    String sName = (String) ((Action) object2).getValue(Action.NAME);

    result = fName.compareTo(sName);

}

return result;

  }

    };

    Arrays.sort(acts, compare);

    StringWriter stringWriter = new StringWriter();

    PrintWriter printWriter = new PrintWriter(stringWriter, true);

    int actlen = acts.length;

    printWriter.println("Count: " + actlen);

    for (int iter = 0; iter < actlen; iter++) {

  printWriter.print(acts[iter].getValue(Action.NAME));

  printWriter.print(" : ");

  printWriter.println(acts[iter].getClass().getName());

    }

    printWriter.close();

    text.setText(stringWriter.toString());

    text.setCaretPosition(0);

}

  };

  final Container container = ButtonOparations.ButtonGroup(components, "List Actions", actListner);

  cPane.add(container, BorderLayout.WEST);

  jFrame.setSize(600, 350);

  jFrame.setVisible(true);
    }
}
class ButtonOparations {

    private ButtonOparations() {
    }

    public static Enumeration getSelectedElements(Container container) {

  Vector vec = new Vector();

  Component comps[] = container.getComponents();

  for (int iter = 0, n = comps.length; iter < n; iter++) {

if (comps[iter] instanceof AbstractButton) {

    AbstractButton absButton = (AbstractButton) comps[iter];

    if (absButton.isSelected()) {

  vec.addElement(absButton.getText());

    }

}

  }

  return vec.elements();
    }

    public static Container ButtonGroup(String elem[]) {

  return ButtonGroup(elem, null, null, null, null);
    }

    public static Container ButtonGroup(String elem[],

String title) {

  return ButtonGroup(elem, title, null, null, null);
    }

    public static Container ButtonGroup(String elem[],

String title, ItemListener iListner) {

  return ButtonGroup(elem, title, null, iListner,

    null);
    }

    public static Container ButtonGroup(String elem[],

String title, ActionListener actListener) {

  return ButtonGroup(elem, title, actListener, null,

    null);
    }

    public static Container ButtonGroup(String elem[],

String title, ActionListener actListener,

ItemListener itemListener) {

  return ButtonGroup(elem, title, actListener,

    itemListener, null);
    }

    public static Container ButtonGroup(String elem[], String str, ActionListener actListener,

ItemListener itemListener, ChangeListener changeListener) {

  JPanel jPanel = new JPanel(new GridLayout(0, 1));

  if (str != null) {

Border surr = BorderFactory.createTitledBorder(str);

jPanel.setBorder(surr);

  }

  ButtonGroup grp = new ButtonGroup();

  JRadioButton radioButton;

  for (int iter = 0, n = elem.length; iter < n; iter++) {

radioButton = new JRadioButton(elem[iter]);

jPanel.add(radioButton);

grp.add(radioButton);

if (actListener != null) {

    radioButton.addActionListener(actListener);

}

if (itemListener != null) {

    radioButton.addItemListener(itemListener);

}

if (changeListener != null) {

    radioButton.addChangeListener(changeListener);

}

  }

  return jPanel;
    }
}

 
This was an example on how to create an application that lists all the actions bundled with a component.

Byron Kiourtzoglou

Byron is a master software engineer working in the IT and Telecom domains. He is an applications developer in a wide variety of applications/services. He is currently acting as the team leader and technical architect for a proprietary service creation and integration platform for both the IT and Telecom industries in addition to a in-house big data real-time analytics solution. He is always fascinated by SOA, middleware services and mobile development. Byron is co-founder and Executive Editor at Java Code Geeks.
Subscribe
Notify of
guest

This site uses Akismet to reduce spam. Learn how your comment data is processed.

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
Back to top button