Source Code : Demonstrating the ActionListener

Java Is Open Source Programming Language You Can Download From Java and Java Libraries From http://www.oracle.com. Click Here to download
We provide this code related to title for you to solve your developing problem easily. Libraries which is import in this program you can download from http://www.oracle.com. Click Here or search from google with Libraries Name you get jar file related it

Demonstrating the ActionListener

Demonstrating the ActionListener
import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;

import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;

public class ActionTest {
  public static void main(String args[]) {
    JFrame frame = new JFrame();
    Container contentPane = frame.getContentPane();
    ActionListener listener = new ActionListener() {
      public void actionPerformed(ActionEvent e) {
        System.out.println("Command: " + e.getActionCommand());
        System.out.println("Modifiers: ");
        int modifiers = e.getModifiers();
        System.out.println("	ALT : "
            + checkMod(modifiers, ActionEvent.ALT_MASK));
        System.out.println("	CTRL : "
            + checkMod(modifiers, ActionEvent.CTRL_MASK));
        System.out.println("	META : "
            + checkMod(modifiers, ActionEvent.META_MASK));
        System.out.println("	SHIFT: "
            + checkMod(modifiers, ActionEvent.SHIFT_MASK));
        Object source = e.getSource();
        if (source instanceof JComboBox) {
          JComboBox jb = (JComboBox) source;
          System.out.println("Combo: " + jb.getSelectedItem());
        }
      }

      private boolean checkMod(int modifiers, int mask) {
        return ((modifiers & mask) == mask);
      }
    };

    String flavors[] = { "Item 1", "Item 2", "Item 3"};
    JComboBox jc = new JComboBox(flavors);
    jc.setMaximumRowCount(4);
    jc.setEditable(true);
    jc.addActionListener(listener);
    contentPane.add(jc, BorderLayout.NORTH);

    JButton b = new JButton("Button!");
    b.addActionListener(listener);
    contentPane.add(b, BorderLayout.CENTER);

    JPanel panel = new JPanel();
    JLabel label = new JLabel("Label 1: ");
    JTextField text = new JTextField("Type your text", 15);
    text.addActionListener(listener);
    label.setDisplayedMnemonic(KeyEvent.VK_1);
    label.setLabelFor(text);
    panel.add(label);
    panel.add(text);
    contentPane.add(panel, BorderLayout.SOUTH);

    frame.pack();
    frame.show();
  }
}


           
       

Thank with us