event
Java ActionListener example
In this tutorial we are going to see how ActionListener
works in Java. This is one the most important components you have to work with when you’re developing a GUI Application. The ActionListener
in able to monitor a number of important events that occur in GUI Apps.
In short, all you have to do to work with an ActionListener
in Java is:
- Create an
ActionListener
instance. - Override the methods that correspond to the events that you want to monitor about the components e.g,
actionPerformed
and customize as you wish the handling of the respective events. Now every time one of these events occurs, the corresponding method will be executed. - Use
addActionListener
to add theActionListener
to a specific component.
Let’s take a closer look a the code snippet that follows:
001 002 003 004 005 006 007 008 009 010 011 012 013 014 015 016 017 018 019 020 021 022 023 024 025 026 027 028 029 030 031 032 033 034 035 036 037 038 039 040 041 042 043 044 045 046 047 048 049 050 051 052 053 054 055 056 057 058 059 060 061 062 063 064 065 066 067 068 069 070 071 072 073 074 075 076 077 078 079 080 081 082 083 084 085 086 087 088 089 090 091 092 093 094 095 096 097 098 099 100 101 102 103 104 105 106 107 108 109 110 111 112 | package com.javacodegeeks.snippets.desktop; 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 Main { public static void main(String args[]) { JFrame jFrame = new JFrame(); Container cPane = jFrame.getContentPane(); ActionListener actListener = new ActionListener() { @Override public void actionPerformed(ActionEvent event) { System.out.println( "Command: " + event.getActionCommand()); System.out.println( "Modifiers: " ); int mods = event.getModifiers(); System.out.println( " Alt : " + cmodifiers(mods, ActionEvent.ALT_MASK)); System.out.println( " Ctrl : " + cmodifiers(mods, ActionEvent.CTRL_MASK)); System.out.println( " Meta : " + cmodifiers(mods, ActionEvent.META_MASK)); System.out.println( " Shift: " + cmodifiers(mods, ActionEvent.SHIFT_MASK)); Object obj = event.getSource(); if (obj instanceof JComboBox) { JComboBox jb = (JComboBox) obj; System.out.println( "Combo: " + jb.getSelectedItem()); } } private boolean cmodifiers( int mods, int mask) { return ((mods & mask) == mask); } }; String array[] = { "Item 1" , "Item 2" , "Item 3" }; JComboBox box = new JComboBox(array); box.setMaximumRowCount( 10 ); box.setEditable( true ); box.addActionListener(actListener); cPane.add(box, BorderLayout.NORTH); JButton jButton = new JButton( "Button!" ); jButton.addActionListener(actListener); cPane.add(jButton, BorderLayout.CENTER); JPanel jPanel = new JPanel(); JLabel label = new JLabel( "" ); JTextField text = new JTextField( "Type here" , 22 ); text.addActionListener(actListener); label.setDisplayedMnemonic(KeyEvent.VK_1); label.setLabelFor(text); jPanel.add(label); jPanel.add(text); cPane.add(jPanel, BorderLayout.SOUTH); jFrame.pack(); jFrame.setVisible( true ); } } |
This was an example on how to work with ActionListener in Java.