Swing - SPLessons

Swing Events Adapters

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

Swing Events Adapters

Swing Events Adapters

shape Description

Abstract classes can be called as Swing Events Adapters for receiving various events. An Swing Events Adapters class gives a default implementation of all methods in an event listener interface. These classes are utilized while processing few of the events. Following are the frequently used Swing Events Adapters.

Adapters

It is utilized to receive keyboard focus events.

shape Example

[java]package splessons; import java.awt.*; import java.awt.event.*; import javax.swing.*; public class SwingAdapterDemo { private JFrame mainFrame; private JLabel headerLabel; private JLabel statusLabel; private JPanel controlPanel; public SwingAdapterDemo(){ prepareGUI(); } public static void main(String[] args){ SwingAdapterDemo swingAdapterDemo = new SwingAdapterDemo(); swingAdapterDemo.showFocusAdapterDemo(); } private void prepareGUI(){ mainFrame = new JFrame("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 showFocusAdapterDemo(){ headerLabel.setText("Listener in action: FocusAdapter"); JButton okButton = new JButton("OK"); JButton cancelButton = new JButton("Cancel"); okButton.addFocusListener(new FocusAdapter(){ public void focusGained(FocusEvent e) { statusLabel.setText(statusLabel.getText() + e.getComponent().getClass().getSimpleName() + " gained focus. "); } }); cancelButton.addFocusListener(new FocusAdapter(){ public void focusLost(FocusEvent e) { statusLabel.setText(statusLabel.getText() + e.getComponent().getClass().getSimpleName() + " lost focus. "); } }); controlPanel.add(okButton); controlPanel.add(cancelButton); mainFrame.setVisible(true); } }[/java] Following is the code to gain the focus or lost the focus. [java]public void focusGained(FocusEvent e) { statusLabel.setText(statusLabel.getText() + e.getComponent().getClass().getSimpleName() + " gained focus. "); }[/java] Output When compile the code following is the output will be generated. When click on ok button following alert will be displayed.
This is utilized to receive mouse events and all methods of this class is empty.

shape Example

[java]package splessons; import java.awt.*; import java.awt.event.*; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JPanel; public class SwingAdapterDemo { private static SwingAdapterDemo swingAdapterDemo; private JFrame mainFrame; private JLabel headerLabel; private JLabel statusLabel; private JPanel controlPanel; public SwingAdapterDemo(){ prepareGUI(); } public static void main(String[] args){ SwingAdapterDemo swingAdapterDemo = new SwingAdapterDemo(); swingAdapterDemo.showMouseAdapterDemo(); } private void prepareGUI(){ mainFrame = new JFrame("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 showMouseAdapterDemo(){ headerLabel.setText("Listener in action: MouseAdapter"); JPanel panel = new JPanel(); panel.setBackground(Color.magenta); panel.setLayout(new FlowLayout()); panel.addMouseListener(new MouseAdapter(){ public void mouseClicked(MouseEvent e) { statusLabel.setText("Mouse Clicked: (" +e.getX()+", "+e.getY() +")"); } }); JLabel msglabel = new JLabel("Stop thinking start coding." ,JLabel.CENTER); msglabel.addMouseListener(new MouseAdapter(){ public void mouseClicked(MouseEvent e) { statusLabel.setText("Mouse Clicked: (" +e.getX()+", "+e.getY() +")"); } }); panel.add(msglabel); controlPanel.add(panel); mainFrame.setVisible(true); } }[/java] The void mouseDragged(MouseEvent e) Invoked when a mouse button is pressed on a component and then dragged and void mouseMoved(MouseEvent e) Invoked when the mouse cursor has been moved onto a component but no buttons have been pushed. Following is the code for the mouse clicked action. [java]public void mouseClicked(MouseEvent e) { statusLabel.setText("Mouse Clicked: (" +e.getX()+", "+e.getY() +")");[/java] Output When compile the code following is the output will be displayed.
This is utilized to receive mouse events and all methods of this class is empty.

shape Example

SwingAdapterDemo.java [java]import java.awt.*; import java.awt.event.*; public class SwingAdapterDemo { private JFrame mainFrame; private JLabel headerLabel; private JLabel statusLabel; private JPanel controlPanel; public SwingAdapterDemo(){ prepareGUI(); } public static void main(String[] args){ SwingAdapterDemo swingAdapterDemo = new SwingAdapterDemo(); swingAdapterDemo.showMouseMotionAdapterDemo(); } private void prepareGUI(){ mainFrame = new JFrame("JAVA SWING"); 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 showMouseMotionAdapterDemo(){ headerLabel.setText("Listener in action: MouseMotionAdapter"); JPanel panel = new JPanel(); panel.setBackground(Color.magenta); panel.setLayout(new FlowLayout()); panel.addMouseMotionListener(new MouseMotionAdapter(){ public void mouseMoved(MouseEvent e) { statusLabel.setText("Mouse Moved: ("+e.getX()+", "+e.getY() +")"); } }); JLabel msglabel = new JLabel("Welcome to SPLessons." ,JLabel.CENTER); panel.add(msglabel); controlPanel.add(panel); mainFrame.setVisible(true); } }[/java] The void mouseDragged(MouseEvent e) Invoked when a mouse button is pressed on a component and then dragged and the void mouseMoved(MouseEvent e) Invoked when the mouse cursor has been moved onto a component but no buttons have been pushed.
WindowAdapter class is an abstract for retrieving window events and methods of this class are empty.

shape Example

[java]package splessons; import java.awt.*; import java.awt.event.*; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JPanel; public class SwingAdapterDemo { private JFrame mainFrame; private JLabel headerLabel; private JLabel statusLabel; private JPanel controlPanel; public SwingAdapterDemo(){ prepareGUI(); } public static void main(String[] args){ SwingAdapterDemo swingAdapterDemo = new SwingAdapterDemo(); swingAdapterDemo.showWindowAdapterDemo(); } private void prepareGUI(){ mainFrame = new JFrame("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 showWindowAdapterDemo(){ headerLabel.setText("Listener in action: WindowAdapter"); JButton okButton = new JButton("OK"); final JFrame aboutFrame = new JFrame(); aboutFrame.setSize(300,200);; aboutFrame.setTitle("WindowAdapter Demo"); aboutFrame.addWindowListener(new WindowAdapter() { public void windowClosing(WindowEvent windowEvent){ aboutFrame.dispose(); } }); JLabel msglabel = new JLabel("Stop thinking start coding." ,JLabel.CENTER); aboutFrame.add(msglabel); aboutFrame.setVisible(true); } }[/java] The class WindowAdapter is an abstract (adapter) class for receiving window events. All methods of this class are empty. This class is convenience class for creating listener objects. The void windowGainedFocus(WindowEvent e) Invoked when the Window is set to be the focused Window, which means that the Window, or one of its subcomponents, will receive keyboard events. Output When compile the code following is the output will be generated.

Summary

shape Key Points

  • Swing Events Adapters are utilized to receive the input events from the keyboard.
  • void mouseDragged(MouseEvent e) method will be invoked when button is pressed.