Feedback Form

JButton States and Events: ActionListener, Mouse Events, and Keyboard Support

JButton States and Events: ActionListener, Mouse Events, and Keyboard Support

JButton Overview

Java Swing में JButton एक बहुत ही commonly used component है, जिसका use किसी action को perform कराने के लिए किया जाता है — जैसे user button पर click करे और कोई task execute हो जाए। Example के लिए “Submit”, “Save”, “Next” या “Exit” जैसे buttons में JButton का use किया जाता है।

जब भी user JButton पर click करता है, तब internally event trigger होता है जिसे handle करने के लिए ActionListener, MouseListener या keyboard events का use किया जाता है। अब हम इन सारे concepts को detail में समझेंगे ताकि exam में आप confidently लिख सको।

Button States in JButton

JButton के अलग-अलग states होते हैं, जो user interaction के अनुसार change होते रहते हैं। इन states से हम समझ सकते हैं कि button किस स्थिति में है — pressed, released या selected।

1. Normal State

जब JButton default position में होता है यानी उस पर कोई action नहीं किया गया होता, तो उसे Normal State कहा जाता है।

2. Pressed State

जब user JButton पर click करता है और mouse button दबा हुआ होता है, तब JButton pressed state में चला जाता है। इस दौरान UI थोड़ा dark या sunken दिखाई देता है।

3. Released State

जब user button को छोड़ देता है (mouse release करता है), तो JButton released state में आता है और इसी समय उसका action event fire होता है।

4. Disabled State

अगर JButton को disable कर दिया गया है, यानी उस पर click नहीं किया जा सकता, तो वो Disabled State में होता है। इसे setEnabled(false) से achieve किया जाता है।

5. Rollover State

जब mouse pointer JButton के ऊपर आता है, तो button rollover state में चला जाता है। इस state को हम customize भी कर सकते हैं ताकि hover effect दे सकें।

JButton btn = new JButton("Submit"); btn.setRolloverEnabled(true);

ActionListener in JButton

अब आते हैं JButton के सबसे important part पर — ActionListener। ये interface button के click event को handle करता है। जब user JButton पर click करता है, तो ActionEvent generate होता है, जिसे handle करने के लिए हम addActionListener() method का use करते हैं।

ActionListener Interface

ActionListener interface में सिर्फ एक method होता है — actionPerformed(ActionEvent e)। इस method के अंदर हम वो logic लिखते हैं जो button click होने पर execute होना चाहिए।

import javax.swing.*; import java.awt.event.*; public class ButtonExample { public static void main(String[] args) { JFrame frame = new JFrame("ActionListener Example"); JButton btn = new JButton("Click Me"); btn.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { System.out.println("Button Clicked!"); } }); frame.add(btn); frame.setSize(300, 200); frame.setLayout(null); btn.setBounds(80, 80, 120, 30); frame.setVisible(true); } }

ऊपर के example में जब user button पर click करेगा, तो console में “Button Clicked!” print होगा। यही basic working of ActionListener है।

Lambda Expression के साथ ActionListener

Java 8 से हम ActionListener को lambda expression के साथ short form में भी लिख सकते हैं।

btn.addActionListener(e -> System.out.println("Clicked using Lambda!"));

यह method ज्यादा readable और concise होती है, इसलिए modern Java coding में prefer की जाती है।

Mouse Events in JButton

कई बार हमें JButton पर सिर्फ click event नहीं बल्कि mouse के अलग-अलग actions जैसे enter, exit, press या release भी detect करने की जरूरत होती है। ऐसे में MouseListener interface काम आता है।

MouseListener Interface Methods

MouseListener interface में पाँच main methods होते हैं:

  • mouseClicked(MouseEvent e): जब user JButton पर click करता है।
  • mousePressed(MouseEvent e): जब mouse button दबाया जाता है।
  • mouseReleased(MouseEvent e): जब mouse button छोड़ा जाता है।
  • mouseEntered(MouseEvent e): जब mouse pointer JButton के area में आता है।
  • mouseExited(MouseEvent e): जब mouse pointer JButton के area से बाहर जाता है।
btn.addMouseListener(new MouseListener() { public void mouseClicked(MouseEvent e) { System.out.println("Mouse Clicked!"); } public void mousePressed(MouseEvent e) { System.out.println("Mouse Pressed!"); } public void mouseReleased(MouseEvent e) { System.out.println("Mouse Released!"); } public void mouseEntered(MouseEvent e) { System.out.println("Mouse Entered!"); } public void mouseExited(MouseEvent e) { System.out.println("Mouse Exited!"); } });

इस तरह हम JButton पर mouse के हर action को अलग-अलग तरीके से control कर सकते हैं।

MouseAdapter Class

अगर हमें सारे methods implement करने की जरूरत नहीं है, तो हम MouseAdapter class को extend कर सकते हैं और सिर्फ जरूरी methods override कर सकते हैं।

btn.addMouseListener(new MouseAdapter() { public void mouseEntered(MouseEvent e) { btn.setText("Mouse Over"); } public void mouseExited(MouseEvent e) { btn.setText("Submit"); } });

यह तरीका ज्यादा efficient है और coding को clean बनाता है।

Keyboard Support in JButton

JButton सिर्फ mouse से ही नहीं, बल्कि keyboard से भी operate किया जा सकता है। कई बार accessibility या shortcut keys के लिए keyboard events बहुत काम आते हैं।

Using Mnemonics

Mnemonic key का use करके हम JButton को किसी specific keyboard key से activate कर सकते हैं।

btn.setMnemonic('S');

अब user “Alt + S” press करके JButton को click कर सकता है। यह feature UI accessibility के लिए बहुत helpful होता है।

Using Key Bindings

अगर आपको specific key press (जैसे Enter या Space) पर action trigger करना है, तो Key Bindings का use किया जा सकता है।

btn.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW) .put(KeyStroke.getKeyStroke("ENTER"), "clickButton"); btn.getActionMap().put("clickButton", new AbstractAction() { public void actionPerformed(ActionEvent e) { System.out.println("Enter Key Pressed!"); } });

इस code में जब user “Enter” key press करेगा, JButton का action trigger हो जाएगा।

Keyboard Focus

JButton को keyboard से focus देने के लिए tab key का use किया जाता है। जब JButton focus में होता है, तो उसे “Space” key से activate किया जा सकता है।

Event Handling Flow in JButton

अब समझते हैं कि internally JButton के events कैसे flow करते हैं।

Step Event Type Triggered By Handler
1 Action Event Button Click ActionListener
2 Mouse Event Mouse Movement/Click MouseListener / MouseAdapter
3 Keyboard Event Key Press Key Binding / Mnemonic

इस table से साफ पता चलता है कि JButton अलग-अलग input devices (mouse, keyboard) से interact कर सकता है और हर एक event के लिए अलग listener काम करता है।

Practical Example: Combined Events

चलो अब एक छोटा complete example देखते हैं जिसमें JButton के सारे events use किए गए हैं — Action, Mouse और Keyboard।

import javax.swing.*; import java.awt.event.*; public class JButtonEvents { public static void main(String[] args) { JFrame frame = new JFrame("JButton Events Example"); JButton btn = new JButton("Click / Hover / Press"); btn.setBounds(70, 70, 200, 40); // ActionListener btn.addActionListener(e -> System.out.println("Button Clicked!")); // MouseAdapter btn.addMouseListener(new MouseAdapter() { public void mouseEntered(MouseEvent e) { btn.setText("Mouse Over"); } public void mouseExited(MouseEvent e) { btn.setText("Click / Hover / Press"); } }); // Mnemonic (Alt + C) btn.setMnemonic('C'); frame.add(btn); frame.setSize(350, 200); frame.setLayout(null); frame.setVisible(true); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); } }

इस program में JButton तीनों तरह के events को handle कर रहा है — mouse hover, click, और keyboard shortcut (Alt + C)।

Exam Tips and Notes

  • ActionListener हमेशा button click को handle करने के लिए use किया जाता है।
  • MouseListener से mouse actions जैसे enter, exit, press, release को detect किया जा सकता है।
  • Mnemonics का use keyboard shortcut के लिए किया जाता है।
  • MouseAdapter code को simplify करता है — जब सारे methods की जरूरत न हो।
  • JButton के different states (Normal, Pressed, Disabled, Rollover) UI response improve करते हैं।
  • Exam में अक्सर question आता है — “Explain ActionListener with Example” या “Differentiate between MouseListener and ActionListener”।

Quick Notes (Short Summary)

Listener Purpose Important Method
ActionListener Handle button click actionPerformed()
MouseListener Handle mouse events mouseClicked(), mouseEntered(), mouseExited()
MouseAdapter Simplify mouse handling Override needed methods only
Mnemonic Enable keyboard shortcut setMnemonic()
Key Binding Bind key press with action getInputMap(), getActionMap()