Swing - SPLessons

Swing Event Listeners

Home > Lesson > Chapter 6
SPLessons 5 Steps, 3 Clicks
5 Steps - 3 Clicks

Swing Event Listeners

Swing Event Listeners

shape Description

The Swing Event Listeners are backbone to every component to handle events, in Java there are more listeners, splessons will teach frequently used listeners and their functinalnity. Every method of a particular event listener will have a single parameter as an instance which is the subclass of EventObject class. Swing Event Listeners interface needs to be extended and it will be defined in Java.util package. Following is the syntax for Swing Event Listeners. [java]public interface EventListener[/java] Following are the frequently used listeners in the swing.

Listeners

The class needs to be implemented this interface and an object of this class should be registered with a component by using addActionListener() method. Following is the syntax. [java]public interface ActionListener extends EventListener[/java]

shape Example

[java] package splessons; import java.awt.*; import java.awt.event.*; import javax.swing.*; public class SwingListenerDemo { private JFrame mainFrame; private JLabel headerLabel; private JLabel statusLabel; private JPanel controlPanel; public SwingListenerDemo(){ prepareGUI(); } public static void main(String[] args){ SwingListenerDemo swingListenerDemo = new SwingListenerDemo(); swingListenerDemo.showActionListenerDemo(); } private void prepareGUI(){ mainFrame = new JFrame("SPLessons-Java SWING Examples"); mainFrame.setSize(400,400); mainFrame.setLayout(new GridLayout(3, 1)); headerLabel = new JLabel("",JLabel.CENTER ); statusLabel = new JLabel("",JLabel.CENTER); statusLabel.setSize(350,100); mainFrame.addWindowListener(new WindowAdapter() { public void windowClosing(WindowEvent windowEvent){ System.exit(0); } }); controlPanel = new JPanel(); controlPanel.setLayout(new FlowLayout()); mainFrame.add(headerLabel); mainFrame.add(controlPanel); mainFrame.add(statusLabel); mainFrame.setVisible(true); } private void showActionListenerDemo(){ headerLabel.setText("Listener in action: ActionListener"); JPanel panel = new JPanel(); panel.setBackground(Color.magenta); JButton okButton = new JButton("SPLesson"); okButton.addActionListener(new CustomActionListener()); panel.add(okButton); controlPanel.add(panel); mainFrame.setVisible(true); } class CustomActionListener implements ActionListener{ public void actionPerformed(ActionEvent e) { statusLabel.setText("SPLesson Button Clicked."); } } } [/java] ActionListener has been implemented here. [java]class CustomActionListener implements ActionListener{ public void actionPerformed(ActionEvent e) { statusLabel.setText("SPLesson Button Clicked.");[/java] JPanel Creates a new JPanel with a double buffer and a flow layout. JPanel(LayoutManager layout) Create a new buffered JPanel with the specified layout manager. Output When compiling the code following is, the output will be generated, when click on the button it give listener alert.
The class needs to be implemented this interface and an object of this class should be registered with a component by using addComponentListener() method. Following is the syntax. [java]public interface ComponentListener extends EventListener[/java]

shape Example

[java]package splessons; import java.awt.*; import java.awt.event.*; import javax.swing.*; public class SwingListenerDemo { private JFrame mainFrame; private JLabel headerLabel; private JLabel statusLabel; private JPanel controlPanel; public SwingListenerDemo(){ prepareGUI(); } public static void main(String[] args){ SwingListenerDemo swingListenerDemo = new SwingListenerDemo(); swingListenerDemo.showComponentListenerDemo(); } private void prepareGUI(){ mainFrame = new JFrame("SPlessons-Java SWING Examples"); mainFrame.setSize(400,400); mainFrame.setLayout(new GridLayout(3, 1)); headerLabel = new JLabel("",JLabel.CENTER ); statusLabel = new JLabel("",JLabel.CENTER); statusLabel.setSize(350,100); mainFrame.addWindowListener(new WindowAdapter() { public void windowClosing(WindowEvent windowEvent){ System.exit(0); } }); controlPanel = new JPanel(); controlPanel.setLayout(new FlowLayout()); mainFrame.add(headerLabel); mainFrame.add(controlPanel); mainFrame.add(statusLabel); mainFrame.setVisible(true); } private void showComponentListenerDemo(){ headerLabel.setText("Listener in action: ComponentListener"); JPanel panel = new JPanel(); panel.setBackground(Color.magenta); JLabel msglabel = new JLabel("Stop thinking start coding." ,JLabel.CENTER); panel.add(msglabel); msglabel.addComponentListener(new CustomComponentListener()); controlPanel.add(panel); mainFrame.setVisible(true); } class CustomComponentListener implements ComponentListener { public void componentResized(ComponentEvent e) { statusLabel.setText(statusLabel.getText() + e.getComponent().getClass().getSimpleName() + " resized. "); } public void componentMoved(ComponentEvent e) { statusLabel.setText(statusLabel.getText() + e.getComponent().getClass().getSimpleName() + " moved. "); } public void componentShown(ComponentEvent e) { statusLabel.setText(statusLabel.getText() + e.getComponent().getClass().getSimpleName() + " shown. "); } public void componentHidden(ComponentEvent e) { statusLabel.setText(statusLabel.getText() + e.getComponent().getClass().getSimpleName() + " hidden. "); } } }[/java] The class which processes the ComponentEvent should implement java.awt.event.ComponentListener interface.The object of that class must be registered with a component. The object can be registered using the addComponentListener() method. Component event are raised for information only. Output When compile the code following is the output will be generated.
The class which process the mouse event needs to be implemented this interface and an object of this class should be registered with a component by using addMouseListener() method. Following is the syntax. [java]public interface MouseListener extends EventListener[/java]

shape Example

[java]package splessons; import java.awt.*; import java.awt.event.*; import javax.swing.*; public class SwingListenerDemo { private JFrame mainFrame; private JLabel headerLabel; private JLabel statusLabel; private JPanel controlPanel; public SwingListenerDemo(){ prepareGUI(); } public static void main(String[] args){ SwingListenerDemo swingListenerDemo = new SwingListenerDemo(); swingListenerDemo.showMouseListenerDemo(); } private void prepareGUI(){ mainFrame = new JFrame("SPlesons - Java SWING Examples"); mainFrame.setSize(400,400); mainFrame.setLayout(new GridLayout(3, 1)); headerLabel = new JLabel("",JLabel.CENTER ); statusLabel = new JLabel("",JLabel.CENTER); statusLabel.setSize(350,100); mainFrame.addWindowListener(new WindowAdapter() { public void windowClosing(WindowEvent windowEvent){ System.exit(0); } }); controlPanel = new JPanel(); controlPanel.setLayout(new FlowLayout()); mainFrame.add(headerLabel); mainFrame.add(controlPanel); mainFrame.add(statusLabel); mainFrame.setVisible(true); } private void showMouseListenerDemo(){ headerLabel.setText("Listener in action: MouseListener"); JPanel panel = new JPanel(); panel.setBackground(Color.magenta); panel.setLayout(new FlowLayout()); panel.addMouseListener(new CustomMouseListener()); JLabel msglabel = new JLabel("Stop thinking start coding." ,JLabel.CENTER); panel.add(msglabel); msglabel.addMouseListener(new CustomMouseListener()); panel.add(msglabel); controlPanel.add(panel); mainFrame.setVisible(true); } class CustomMouseListener implements MouseListener{ public void mouseClicked(MouseEvent e) { statusLabel.setText("Mouse Clicked: ("+e.getX()+", "+e.getY() +")"); } public void mousePressed(MouseEvent e) { } public void mouseReleased(MouseEvent e) { } public void mouseEntered(MouseEvent e) { } public void mouseExited(MouseEvent e) { } } }[/java] To place buttons, text labels and other components on the program window, you need to understand about JPanel. It is a kind of container for components, which occupies the rectangular piece on the screen and shows the components arranged in some simple way. How exactly the components are arranged depends on which layout have you set to that panel. For the manual programming you will likely need to know at least the BorderLayout which places four components at the sides and one large component into the middle, then the FlowLayout which usually arranges them side by side into horizontal row and finally the GridLayout which arranges components into arbitrary n * m table. Output When compile the code following is the output will be generated .
The class which process the window event needs to be implemented this interface and an object of this class should be registered with a component by using addWindowListener() method. Following is the syntax. [java]public interface WindowListener extends EventListener[/java]

shape Example

[java]package splessons; import java.awt.*; import java.awt.event.*; import javax.swing.*; public class SwingListenerDemo { private JFrame mainFrame; private JLabel headerLabel; private JLabel statusLabel; private JPanel controlPanel; private JFrame aboutFrame; public SwingListenerDemo(){ prepareGUI(); } public static void main(String[] args){ SwingListenerDemo swingListenerDemo = new SwingListenerDemo(); swingListenerDemo.showWindowListenerDemo(); } private void prepareGUI(){ mainFrame = new JFrame("SPlessons - Java SWING Examples"); mainFrame.setSize(400,400); mainFrame.setLayout(new GridLayout(3, 1)); headerLabel = new JLabel("",JLabel.CENTER ); statusLabel = new JLabel("",JLabel.CENTER); statusLabel.setSize(350,100); mainFrame.addWindowListener(new WindowAdapter() { public void windowClosing(WindowEvent windowEvent){ System.exit(0); } }); controlPanel = new JPanel(); controlPanel.setLayout(new FlowLayout()); mainFrame.add(headerLabel); mainFrame.add(controlPanel); mainFrame.add(statusLabel); mainFrame.setVisible(true); } private void showWindowListenerDemo(){ headerLabel.setText("Listener in action: WindowListener"); JButton okButton = new JButton("OK"); aboutFrame = new JFrame(); aboutFrame.setSize(300,200);; aboutFrame.setTitle("SPlessons - WindowListener Demo"); aboutFrame.addWindowListener(new CustomWindowListener()); JPanel panel = new JPanel(); panel.setBackground(Color.white); JLabel msglabel = new JLabel("Stop thinking start coding." ,JLabel.CENTER); panel.add(msglabel); aboutFrame.add(panel); aboutFrame.setVisible(true); } class CustomWindowListener implements WindowListener { public void windowOpened(WindowEvent e) { } public void windowClosing(WindowEvent e) { aboutFrame.dispose(); } public void windowClosed(WindowEvent e) { } public void windowIconified(WindowEvent e) { } public void windowDeiconified(WindowEvent e) { } public void windowActivated(WindowEvent e) { } public void windowDeactivated(WindowEvent e) { } } }[/java]
Output When compile the code following is the output will be generated.
To receive the keyboard focus events this interface will be used.Following is the syntax. [java]public interface FocusListener extends EventListener[/java]

shape Example

[java]package splessons; import java.awt.*; import java.awt.event.*; import javax.swing.*; public class SwingListenerDemo { private JFrame mainFrame; private JLabel headerLabel; private JLabel statusLabel; private JPanel controlPanel; public SwingListenerDemo(){ prepareGUI(); } public static void main(String[] args){ SwingListenerDemo swingListenerDemo = new SwingListenerDemo(); swingListenerDemo.showFocusListenerDemo(); } private void prepareGUI(){ mainFrame = new JFrame("SPlessons - Java SWING Examples"); mainFrame.setSize(400,400); mainFrame.setLayout(new GridLayout(3, 1)); headerLabel = new JLabel("",JLabel.CENTER ); statusLabel = new JLabel("",JLabel.CENTER); statusLabel.setSize(350,100); mainFrame.addWindowListener(new WindowAdapter() { public void windowClosing(WindowEvent windowEvent){ System.exit(0); } }); controlPanel = new JPanel(); controlPanel.setLayout(new FlowLayout()); mainFrame.add(headerLabel); mainFrame.add(controlPanel); mainFrame.add(statusLabel); mainFrame.setVisible(true); } private void showFocusListenerDemo(){ headerLabel.setText("Listener in action: FocusListener"); JButton okButton = new JButton("OK"); JButton cancelButton = new JButton("Cancel"); okButton.addFocusListener(new CustomFocusListener()); cancelButton.addFocusListener(new CustomFocusListener()); controlPanel.add(okButton); controlPanel.add(cancelButton); mainFrame.setVisible(true); } class CustomFocusListener implements FocusListener{ public void focusGained(FocusEvent e) { statusLabel.setText(statusLabel.getText() + e.getComponent().getClass().getSimpleName() + " gained focus. "); } public void focusLost(FocusEvent e) { statusLabel.setText(statusLabel.getText() + e.getComponent().getClass().getSimpleName() + " lost focus. "); } } }[/java] The focusGained(FocusEvent e) is used to gain the window mean that when maximize the window, focusLost(FocusEvent e) is used to lost the focus mean that when minimize the window. Output When compile the code following is the output will be displayed. When click on okbutton following output will be displayed.

Summary

shape Key Points

  • Swing Event Listeners - The JPanel class is a generic lightweight container.
  • Swing Event Listeners - All the swing components will be available in java.awt.event.*; package.