Feedback Form

Building GUI in JApplet: Swing Integration and Parameter Passing

Building GUI in JApplet: Swing Integration and Parameter Passing

Building GUI in JApplet क्या है?

जब हम Java Applet के अंदर graphical user interface (GUI) बनाना चाहते हैं, तो हम JApplet class का use करते हैं। JApplet, Swing का हिस्सा है और यह पुराने AWT (Abstract Window Toolkit) से ज्यादा powerful और flexible होता है। GUI यानी Graphical User Interface वो होता है जहाँ user, buttons, labels, text fields आदि के जरिए program से interact करता है।

JApplet हमें web browser या Applet Viewer में GUI components दिखाने की सुविधा देता है। इससे हम interactive programs बना सकते हैं जो visually attractive और user-friendly हों।

Swing Integration in JApplet

Swing, Java का एक advanced GUI toolkit है जो lightweight components provide करता है जैसे कि JButton, JLabel, JTextField, JComboBox, आदि। जब हम JApplet में Swing components use करते हैं, तो इसे हम Swing Integration कहते हैं।

यह integration हमें AWT की limitations से मुक्त करता है और बेहतर look and feel देता है। Swing components platform independent होते हैं — यानी ये हर operating system पर एक जैसे दिखते हैं।

JApplet Class Hierarchy

JApplet की hierarchy इस प्रकार है:

java.lang.Object → java.awt.Component → java.awt.Container → java.awt.Panel → java.applet.Applet → javax.swing.JApplet

Important Methods of JApplet

JApplet में कुछ predefined methods होते हैं जिन्हें हम override करके GUI elements initialize या manage कर सकते हैं।

  • init() – Applet शुरू होते समय call होता है। GUI components को initialize करने के लिए use किया जाता है।
  • start() – जब Applet visible होता है तो यह method run होता है।
  • stop() – जब Applet invisible होता है, तब run होता है।
  • destroy() – जब Applet बंद होता है, तब memory free करने के लिए use होता है।

Steps to Build GUI in JApplet

एक proper GUI बनाने के लिए हमें कुछ specific steps follow करने होते हैं। नीचे दिए गए steps आपको हर बार एक perfect GUI Applet design करने में मदद करेंगे।

Step 1: Create a Class Extending JApplet

सबसे पहले आपको अपनी class को JApplet से extend करना होगा ताकि वो applet के behavior को inherit कर सके।

import javax.swing.*; import java.awt.*; public class MyApplet extends JApplet { public void init() { // GUI initialization code here } }

Step 2: Use init() Method for GUI Initialization

GUI elements जैसे कि JLabel, JButton, और JTextField को init() method के अंदर initialize किया जाता है क्योंकि ये method Applet start होते ही automatically call होता है।

public void init() { JLabel label = new JLabel("Enter your name:"); JTextField textField = new JTextField(15); JButton button = new JButton("Submit"); setLayout(new FlowLayout()); add(label); add(textField); add(button); }

Step 3: Set Layout Manager

Layout Manager GUI components को organize करने में मदद करता है। Common layout managers हैं — FlowLayout, BorderLayout, GridLayout, और BoxLayout।

Layout ManagerDescription
FlowLayoutComponents को left-to-right arrange करता है।
BorderLayoutContainer को 5 parts में divide करता है (NORTH, SOUTH, EAST, WEST, CENTER)।
GridLayoutRows और Columns के रूप में layout बनाता है।
BoxLayoutComponents को vertically या horizontally align करता है।

Step 4: Add Components

अब आप अपनी आवश्यकता के अनुसार GUI components जोड़ सकते हैं। उदाहरण के लिए, नीचे एक simple Applet GUI दिया गया है।

import javax.swing.*; import java.awt.*; public class SimpleApplet extends JApplet { public void init() { setLayout(new FlowLayout()); JLabel label = new JLabel("Enter your Name:"); JTextField nameField = new JTextField(15); JButton submit = new JButton("Submit"); add(label); add(nameField); add(submit); } }

Step 5: Run Applet

Applet को run करने के लिए हम HTML file बनाते हैं जिसमें <applet> या <object> tag का use होता है।

<html> <body> <applet code="SimpleApplet.class" width="300" height="200"></applet> </body> </html>

Parameter Passing in JApplet

कभी-कभी हमें Applet में external values भेजनी होती हैं — जैसे user name, color, message आदि। ऐसे values HTML file से Applet में भेजने की process को Parameter Passing कहते हैं।

How to Pass Parameters

हम HTML file में <param> tag का use करके parameters भेजते हैं।

<applet code="ParamApplet.class" width="300" height="200"> <param name="username" value="Ravi"> <param name="color" value="blue"> </applet>

Receiving Parameters in JApplet

JApplet में parameter को read करने के लिए हम getParameter() method का use करते हैं। यह method parameter के name से उसकी value return करता है।

import javax.swing.*; import java.awt.*; public class ParamApplet extends JApplet { public void init() { String user = getParameter("username"); String color = getParameter("color"); JLabel label = new JLabel("Welcome " + user); add(label); if(color.equals("blue")) getContentPane().setBackground(Color.blue); else getContentPane().setBackground(Color.gray); } }

Example Output

अगर HTML में username="Ravi" दिया गया है, तो Applet output में दिखाएगा — Welcome Ravi और background color blue होगा।

Event Handling in JApplet

GUI applications में event handling बहुत जरूरी होती है। Event handling का मतलब होता है user interaction (जैसे button click, key press, mouse movement) को handle करना।

Example: Button Click Event

import javax.swing.*; import java.awt.*; import java.awt.event.*; public class ButtonApplet extends JApplet implements ActionListener { JTextField field; JButton button; public void init() { setLayout(new FlowLayout()); JLabel label = new JLabel("Enter text:"); field = new JTextField(10); button = new JButton("Show"); add(label); add(field); add(button); button.addActionListener(this); } public void actionPerformed(ActionEvent e) { String text = field.getText(); showStatus("You entered: " + text); } }

यहाँ showStatus() method Applet के status bar में message दिखाने के लिए use किया गया है।

Advantages of Swing Integration in JApplet

  • Lightweight और platform-independent components।
  • Better look and feel (Cross-platform consistency)।
  • More flexibility than AWT components।
  • Rich GUI controls जैसे JTable, JComboBox, JTree आदि।
  • Custom rendering और themes का support।

Limitations of JApplet

  • Modern browsers अब Applets को directly support नहीं करते।
  • Security restrictions काफी होती हैं।
  • Performance web-based GUI frameworks से कम होती है।
  • Deployment में Java Plugin dependency होती है।

Practical Example: Login GUI Using JApplet

import javax.swing.*; import java.awt.*; import java.awt.event.*; public class LoginApplet extends JApplet implements ActionListener { JTextField userField; JPasswordField passField; JButton loginBtn; public void init() { setLayout(new GridLayout(3,2)); add(new JLabel("Username:")); userField = new JTextField(10); add(userField); add(new JLabel("Password:")); passField = new JPasswordField(10); add(passField); loginBtn = new JButton("Login"); add(loginBtn); loginBtn.addActionListener(this); } public void actionPerformed(ActionEvent e) { String user = userField.getText(); String pass = new String(passField.getPassword()); if(user.equals("admin") && pass.equals("1234")) { showStatus("Login Successful!"); } else { showStatus("Invalid Credentials!"); } } }

यह example Applet के अंदर एक simple login form बनाता है जिसमें username और password verify किए जाते हैं।

Important Points to Remember

  • JApplet हमेशा javax.swing package से import करें।
  • init() method GUI initialization के लिए use करें।
  • getParameter() method से HTML parameters access करें।
  • Applet run करने के लिए HTML file बनाना जरूरी है।
  • Event handling से interactive GUI बनाई जा सकती है।

Exam-Oriented Notes

  • JApplet Swing-based Applet class है जो GUI components handle करती है।
  • init() method — GUI initialization के लिए।
  • getParameter() — HTML से data receive करने के लिए।
  • showStatus() — Applet status bar में message display करने के लिए।
  • Event Handling — User interaction manage करने के लिए।
  • Common layouts — FlowLayout, BorderLayout, GridLayout।
  • Swing components lightweight और platform-independent होते हैं।
  • Applet HTML के साथ integrate होकर dynamic GUI बना सकता है।