Creating and Customizing JButton: Text, Icons, Tooltips, and Mnemonics
Creating and Customizing JButton: Text, Icons, Tooltips, and Mnemonics
Creating and Customizing JButton
Java में जब हम GUI (Graphical User Interface) applications बनाते हैं, तो user interaction के लिए सबसे ज्यादा use किया जाने वाला component है JButton। JButton का काम होता है user से किसी action को trigger करना — जैसे किसी button पर click करते ही कोई event perform होना। Swing framework में JButton class का use करके हम आसानी से button create और customize कर सकते हैं।
What is JButton?
JButton एक component है जो Swing package (javax.swing.*) में मौजूद होता है। यह एक push button की तरह काम करता है जो किसी specific action को perform करने के लिए इस्तेमाल होता है। इसे हम text, icon, tooltip, और mnemonic जैसी properties से customize कर सकते हैं ताकि interface attractive और user-friendly बने।
Basic Syntax of JButton
JButton बनाने का syntax बहुत simple है:
JButton b = new JButton("Click Me");
ऊपर दिए गए code में "Click Me" button पर दिखने वाला text है। JButton को container (जैसे JFrame या JPanel) में add करना जरूरी होता है ताकि वो GUI में दिखाई दे।
Example of JButton Creation
import javax.swing.*;
public class JButtonExample {
public static void main(String[] args) {
JFrame f = new JFrame("JButton Example");
JButton b = new JButton("Click Me");
b.setBounds(100, 100, 120, 40);
f.add(b);
f.setSize(300, 200);
f.setLayout(null);
f.setVisible(true);
}
}
इस example में एक JFrame के अंदर JButton create किया गया है और उसे setBounds() से position और size दी गई है।
Customizing JButton
अब हम समझेंगे कि JButton को कैसे customize किया जा सकता है। एक JButton को user-friendly और visually appealing बनाने के लिए हम चार main customization techniques use करते हैं:
- Text
- Icon
- Tooltip
- Mnemonic
1. Setting Text on JButton
Button पर दिखाई देने वाला text user को बताता है कि button का purpose क्या है। JButton में text set करने के लिए हम constructor या setText() method का use करते हैं।
JButton b1 = new JButton("Submit");
b1.setText("Send");
ऊपर के code में पहले "Submit" लिखा था लेकिन बाद में हमने setText() से उसे "Send" में बदल दिया।
2. Adding Icons to JButton
Icons buttons को visually बेहतर बनाते हैं और user को quickly समझने में मदद करते हैं कि button क्या काम करेगा। JButton में icon set करने के लिए ImageIcon class का use किया जाता है।
ImageIcon icon = new ImageIcon("send.png");
JButton b2 = new JButton("Send", icon);
यहाँ “send.png” एक image file है जो JButton के साथ display होगी। आप चाहें तो केवल icon के साथ button बना सकते हैं:
JButton b3 = new JButton(icon);
3. Adding Tooltips to JButton
Tooltip वो small text होता है जो button पर mouse hover करने पर दिखाई देता है। यह user को बताता है कि button क्या करता है। Tooltip set करने के लिए setToolTipText() method use होता है।
b2.setToolTipText("Click to send message");
जब user mouse को button पर लाएगा, तो यह message tooltip के रूप में दिखाई देगा।
4. Adding Mnemonics to JButton
Mnemonic keyboard shortcut keys होती हैं जिससे user बिना mouse के भी button को activate कर सकता है। Mnemonic set करने के लिए setMnemonic() method का use करते हैं।
b2.setMnemonic('S');
अब user “Alt + S” दबाकर button को activate कर सकता है। यह accessibility बढ़ाने में बहुत helpful feature है।
Advanced JButton Customization
Swing framework हमें JButton को और भी advance तरीके से customize करने की सुविधा देता है जैसे – background color, font style, text alignment, icon position आदि। इससे आप button का complete look customize कर सकते हैं।
Changing Button Color and Font
b2.setBackground(Color.BLUE);
b2.setForeground(Color.WHITE);
b2.setFont(new Font("Arial", Font.BOLD, 14));
यह code button को blue background और white text देता है, साथ ही text bold और readable बन जाता है।
Changing Text and Icon Alignment
आप text और icon की position को control कर सकते हैं। इसके लिए setHorizontalTextPosition() और setVerticalTextPosition() methods का use होता है।
b2.setHorizontalTextPosition(SwingConstants.CENTER);
b2.setVerticalTextPosition(SwingConstants.BOTTOM);
इससे text icon के नीचे आ जाएगा, जिससे layout और appealing बनता है।
Disable and Enable JButton
कभी-कभी हमें button को temporarily disable करना पड़ता है ताकि user उसे click न कर सके। इसके लिए setEnabled() method का use होता है।
b2.setEnabled(false); // disables the button
b2.setEnabled(true); // enables the button again
Setting Border and Focus
Swing में JButton की border और focus को भी control किया जा सकता है ताकि design minimalistic लगे।
b2.setBorderPainted(false);
b2.setFocusPainted(false);
इससे button की border और focus outline हट जाती है — जिससे modern UI look मिलता है।
Examples of Custom JButton
नीचे कुछ examples दिए गए हैं जो JButton के विभिन्न customization को practically दिखाते हैं:
import javax.swing.*;
import java.awt.*;
public class CustomButtonExample {
public static void main(String[] args) {
JFrame frame = new JFrame("Custom JButton Example");
frame.setSize(400, 300);
frame.setLayout(null);
ImageIcon sendIcon = new ImageIcon("send.png");
JButton sendButton = new JButton("Send", sendIcon);
sendButton.setBounds(100, 80, 150, 50);
sendButton.setBackground(Color.BLUE);
sendButton.setForeground(Color.WHITE);
sendButton.setFont(new Font("Tahoma", Font.BOLD, 14));
sendButton.setToolTipText("Click here to send message");
sendButton.setMnemonic('S');
sendButton.setFocusPainted(false);
frame.add(sendButton);
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
इस example में JButton को पूरी तरह customize किया गया है — color, font, tooltip, mnemonic, और icon के साथ।
JButton Customization Table
नीचे दी गई table में JButton customization methods और उनके use दिखाए गए हैं:
| Method | Description | Example |
|---|---|---|
setText(String) |
Button का text बदलता है | b.setText("OK"); |
setIcon(Icon) |
Button पर image set करता है | b.setIcon(new ImageIcon("ok.png")); |
setToolTipText(String) |
Tooltip text दिखाता है | b.setToolTipText("Click to proceed"); |
setMnemonic(char) |
Shortcut key assign करता है | b.setMnemonic('O'); |
setEnabled(boolean) |
Button enable/disable करता है | b.setEnabled(false); |
setBackground(Color) |
Background color set करता है | b.setBackground(Color.GREEN); |
setFont(Font) |
Font style और size set करता है | b.setFont(new Font("Arial", Font.BOLD, 14)); |
Important Notes for Students
- JButton class
javax.swingpackage का part है। - Button को JFrame या JPanel में add करना जरूरी होता है ताकि वो visible हो।
setMnemonic()method accessibility improve करने के लिए helpful होता है।- Tooltips exam के short answer में लिखने योग्य important point है।
- JButton के साथ ActionListener का use event handling के लिए किया जाता है।
Example with ActionListener
import javax.swing.*;
import java.awt.event.*;
public class ActionExample {
public static void main(String[] args) {
JFrame f = new JFrame("Action Example");
JButton b = new JButton("Click Here");
b.setBounds(100, 100, 150, 40);
b.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
JOptionPane.showMessageDialog(f,"Button Clicked Successfully!");
}
});
f.add(b);
f.setSize(400, 300);
f.setLayout(null);
f.setVisible(true);
}
}
इस example में ActionListener का use किया गया है ताकि button click होने पर message dialog show हो।
Exam Focused Notes
- Class: JButton (extends AbstractButton)
- Package: javax.swing
- Constructor: JButton(), JButton(String text), JButton(Icon icon), JButton(String text, Icon icon)
- Common Methods: setText(), getText(), setIcon(), setToolTipText(), setMnemonic(), setEnabled(), addActionListener()
- Purpose: JButton का use किसी action को perform करने के लिए किया जाता है।
- Customization: Text, Icon, Tooltip, Mnemonic, Color, Font
- Shortcut Key: Alt + Character (Mnemonic)
- Use Case: Login forms, submit buttons, cancel options आदि में।