0% found this document useful (0 votes)
20 views37 pages

Advanced Java Applet Examples

The document contains code examples demonstrating the use of various Java Swing components including JLabels, JButtons, JTextFields, JPasswordFields, JCheckBoxes, JComboBoxes, JTabbedPanes, JTables, JTrees, JRadioButtons, and examples of using RMI for remote method invocation.

Uploaded by

Ramakant Singh
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
20 views37 pages

Advanced Java Applet Examples

The document contains code examples demonstrating the use of various Java Swing components including JLabels, JButtons, JTextFields, JPasswordFields, JCheckBoxes, JComboBoxes, JTabbedPanes, JTables, JTrees, JRadioButtons, and examples of using RMI for remote method invocation.

Uploaded by

Ramakant Singh
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

1. import [Link].*; import [Link].*; import [Link].

*;

public class JLabel_ex extends JApplet {

public void init() { Container cp = getContentPane(); [Link](new FlowLayout());

ImageIcon ic = new ImageIcon("[Link]"); JLabel jl = new JLabel("Sunset is always beautiful",ic,[Link]); [Link](jl);

/* <applet code=JLabel_ex.class height=200 width=200> </applet> */

2. import [Link].*; import [Link].*; import [Link].*; import [Link].*;

public class JButton_ex extends JApplet implements ActionListener { //JButton jb = new JButton();

JTextField jtf; public void init() { Container cp = getContentPane(); [Link](new FlowLayout());

JButton jb = new JButton("Submit"); [Link]("Sunset"); [Link](this); jtf = new JTextField(10); [Link](jtf); [Link](jb);

} public void actionPerformed(ActionEvent ae) { [Link]([Link]()); }

} /* <applet code=JButton_ex.class height=200 width=200> </applet> */

3. import [Link].*; import [Link].*; import [Link].*; import [Link].*;

public class Jtxtfield_ex extends JApplet { JTextField jtf; public void init() { Container cp = getContentPane(); [Link](new FlowLayout());

jtf = new JTextField(10); [Link](jtf);

} }

/* <applet code=Jtxtfield_ex.class height=200 width=200> </applet> */

4. import [Link].*; import [Link].*; import [Link].*; import [Link].*;

public class Login_ex extends JApplet implements ActionListener { JLabel jl1; JTextField jtf1;

JLabel jl2; JPasswordField jpf; JButton jb1; JButton jb2; JTextArea jta; public void init() { Container cp = getContentPane(); [Link](new FlowLayout());

jl1=new JLabel("Login ID jtf1= new JTextField(10);

: ");

jl2=new JLabel("Password : "); jpf=new JPasswordField("",10); jb1 = new JButton("Submit"); jb2 = new JButton("Clear");

[Link](jl1); [Link](jtf1); [Link](jl2); [Link](jpf); [Link](jb1); [Link](jb2); jta=new JTextArea(10,10); [Link](jta);

[Link](this); [Link](this);

} public void actionPerformed(ActionEvent ae) { //[Link]("Hello"); //[Link]([Link]());

if([Link]()==jb1) { if([Link]().equals([Link]())) { [Link]("Welcome "); [Link]([Link]());

} else { [Link]("Invalid Login"); } }

if([Link]()==jb2)

{ [Link](""); [Link](""); [Link](""); }

/* <applet code=Login_ex.class height=200 width=200> </applet> */

5. import [Link].*; import [Link].*; import [Link].*;

public class chkbox extends JApplet implements ItemListener { JTextField jtf; public void init() { jtf = new JTextField(10);

Container cp = getContentPane(); [Link](new FlowLayout());

ImageIcon normal = new ImageIcon("[Link]"); ImageIcon rollover = new ImageIcon("[Link]"); ImageIcon selected = new ImageIcon("[Link]");

JCheckBox jcb1 = new JCheckBox("jcb1"); JCheckBox jcb2 = new JCheckBox("jcb2",rollover); JCheckBox jcb3 = new JCheckBox("jcb3");

[Link](new ImageIcon("[Link]")); [Link](jcb1);

[Link](jcb2); [Link](jcb3); [Link](jtf); [Link](this); [Link](this); [Link](this); } public void itemStateChanged(ItemEvent ie) { JCheckBox cb = (JCheckBox)[Link](); [Link]([Link]()); }

/* <applet code=[Link] width=200 height=200> </applet> */

6. import [Link].*; import [Link].*; import [Link].*;

public class combo extends JApplet implements ItemListener

{ JTextField jtf; JComboBox jcb; public void init() { jtf = new JTextField(10);

Container cp = getContentPane(); [Link](new FlowLayout());

jcb = new JComboBox(); [Link]("OFFICER"); [Link]("MANAGER"); [Link]("CLERK"); [Link]("SUPERVISOR"); [Link](this); [Link](jcb); [Link](jtf); } public void itemStateChanged(ItemEvent ie) { String s = (String)[Link](); [Link](s); //[Link]([Link]()); }

/* <applet code=[Link] height=200 width=200> </applet> */

7. import [Link].*; import [Link].*; import [Link].*;

public class jtabbedpane extends JApplet {

public void init() { JTabbedPane jtp= new JTabbedPane();

[Link]("cities",new citiesPane()); [Link]("colors",new colorsPane()); getContentPane().add(jtp); } } class citiesPane extends JPanel {

public citiesPane() {

JButton jb1 = new JButton("N.Y."); JButton jb2= new JButton("LONDON"); JButton jb3= new JButton("TOKYO"); add(jb1); add(jb2); add(jb3); } } class colorsPane extends JPanel implements ItemListener { JTextField jtf;

public colorsPane() {

jtf= new JTextField(10); JCheckBox jcb1=new JCheckBox("PINK"); JCheckBox jcb2=new JCheckBox("YELLOW"); JCheckBox jcb3=new JCheckBox("RED"); JCheckBox jcb4=new JCheckBox("GREEN"); add(jcb1); add(jcb2); add(jcb3); add(jcb4); add(jtf); [Link](this); [Link](this); [Link](this); [Link](this); } public void itemStateChanged(ItemEvent ie) { JCheckBox cb = (JCheckBox)[Link](); //[Link]([Link]()); [Link]([Link]()); }

/* <applet code=[Link] height=200 width=200> </applet> */

9. import [Link].*; import [Link].*; import [Link].*;

public class scrolldemo extends JApplet { //JButton jb = new JButton(); //JTextField jtf; public void init() {

Container cp = getContentPane(); [Link](new GridLayout());

JPanel jp = new JPanel(); [Link](new GridLayout(20,20)); int b=0; for(int i=0;i<25;i++) for(int j=0;j<25;j++) { [Link](new JButton("Button : "+b)); b++; } int v= ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED; int h= ScrollPaneConstants.HORIZONTAL_SCROLLBAR_AS_NEEDED; JScrollPane jsp=new JScrollPane(jp,v,h); [Link](jsp,[Link]);

/* <applet code=[Link] height=400 width=400> </applet> */

10. import [Link].*; import [Link].*; import [Link].*;

public class tabledemo extends JApplet { //JButton jb = new JButton(); //JTextField jtf; public void init() { Container cp = getContentPane(); [Link](new BorderLayout(20,20));

final String[] colHeads={"Name","No","Amount"}; final Object[][] data={{"Ram","123","54654"},{"Shyam","456","89784"},{"Sita","789","25488"}}; JTable jt = new JTable(data,colHeads); int v= ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED; int h= ScrollPaneConstants.HORIZONTAL_SCROLLBAR_AS_NEEDED; JScrollPane jsp=new JScrollPane(jt,v,h); [Link](jsp,[Link]);

/* <applet code=[Link] height=200 width=400> </applet> */

11. import [Link].*; import [Link].*; import [Link].*; import [Link].*;

public class treedemo extends JApplet { JTree jt; JTextField jtf; public void init() { Container cp = getContentPane(); [Link](new BorderLayout(20,20));

DefaultMutableTreeNode top = new DefaultMutableTreeNode("Options"); DefaultMutableTreeNode a = new DefaultMutableTreeNode("A"); DefaultMutableTreeNode a1 = new DefaultMutableTreeNode("A1"); DefaultMutableTreeNode a2 = new DefaultMutableTreeNode("A2"); [Link](a); [Link](a1); [Link](a2); DefaultMutableTreeNode b = new DefaultMutableTreeNode("B"); DefaultMutableTreeNode b1 = new DefaultMutableTreeNode("B1"); DefaultMutableTreeNode b2 = new DefaultMutableTreeNode("B2");

[Link](b); [Link](b1); [Link](b2); jt=new JTree(top); int v= ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED; int h= ScrollPaneConstants.HORIZONTAL_SCROLLBAR_AS_NEEDED; JScrollPane jsp=new JScrollPane(jt,v,h); [Link](jsp,[Link]); jtf=new JTextField(20); [Link](new MouseAdapter() { public void mouseClicked(MouseEvent me) {doMouseClicked(me);} }

); } void doMouseClicked(MouseEvent me) { TreePath tp= [Link]([Link](),[Link]()); if(tp!=null) [Link]([Link]()); else [Link](" "); }

/* <applet code=[Link] height=200 width=400> </applet> */

12. import [Link].*; import [Link].*; import [Link].*;

public class radiodemo extends JApplet implements ActionListener { ButtonGroup bg; JTextField jtf; public void init() { Container cp = getContentPane(); [Link](new FlowLayout());

JRadioButton b1 = new JRadioButton("A");

[Link](this); [Link](b1); JRadioButton b2 = new JRadioButton("B"); [Link](this); [Link](b2); JRadioButton b3 = new JRadioButton("C"); [Link](this); [Link](b3); bg= new ButtonGroup(); [Link](b1); [Link](b2); [Link](b3); jtf = new JTextField(10); //[Link](bg); [Link](jtf);

} public void actionPerformed(ActionEvent ae) { [Link]([Link]()); }

/* <applet code=[Link] height=200 width=200> </applet> */

13. PROGRAM ONE : import [Link].*;

public interface AddServerIntf extends Remote { double add(double d1,double d2) throws RemoteException; }

PROGRAM TWO : import [Link].*; import [Link].*;

public class AddServerImpl extends UnicastRemoteObject implements AddServerIntf

{ public AddServerImpl() throws RemoteException {} public double add(double d1,double d2) throws RemoteException { return d1 + d2; } } PROGRAM THREE : import [Link].*; import [Link].*;

public class AddServer { public static void main(String args[]) { try { AddServerImpl asi=new AddServerImpl(); [Link]("AddServer",asi); } catch(Exception e) { [Link]("Exception : "+e); }

} } PROGRAM FOUR : import [Link].*;

public class AddClient { public static void main(String args[]) { try { String AddServerURL="rmi://"+args[0]+"/AddServer"; AddServerIntf asin = (AddServerIntf)[Link](AddServerURL); [Link]("First No : "+args[1]); [Link]("Second No : "+args[2]); double d1 =[Link](args[1]).doubleValue(); double d2 =[Link](args[2]).doubleValue(); [Link]("Sum is : "+[Link](d1,d2)); } catch(Exception e) { [Link]("Exception : "+e); } } }

14. import [Link].*; import [Link].*; import [Link].*; import [Link].*; public class ImageViewer extends JLabel { public ImageViewer() {} public void setFileName(String filename) { try{ file = new File("[Link]"); setIcon(new ImageIcon([Link](file))); } catch(IOException e) { file=null; setIcon(null); } } public String getFileName() { if(file==null) {

return null; } return [Link](); } public Dimension getPrefferedSize() { return new Dimension (XPSIZE,YPSIZE); } private File file=null; private static int XPSIZE=200; private static final int YPSIZE=200; }

15.

import [Link].*; import [Link].*; public class HelloServlet extends GenericServlet { public void service(ServletRequest req,ServletResponse res) throws ServletException,IOException { try{ [Link]("text/html"); PrintWriter pw = [Link](); [Link]("HelloWorld"); [Link](); } catch(Exception e) { [Link]("Eception : "+e); } } }

16. <html> <body>

<form name="form1" method="post" action="[Link]

<table> <tr><td><b>Employee<td> <td><input type=textbox name="e" size="10" value=""></td> </table>

<input type=submit name="Submit" value="Submit"> </form> </body

</html>

import [Link].*; import [Link].*; import [Link].*;

public class PostParameterServlet extends GenericServlet { public void service(ServletRequest req,ServletResponse res) throws ServletException,IOException { try{ PrintWriter pw = [Link](); Enumeration e = [Link](); while([Link]()) { String pname=(String)[Link](); [Link](pname+" = "); String pvalue=[Link](pname); [Link](pvalue); } [Link](); }

catch(Exception e) { [Link]("Eception : "+e); } } }

17. import [Link].*; import [Link].*; import [Link].*;

public class AddCookieServlet extends HttpServlet { public void doPost(HttpServletRequest req,HttpServletResponse res) throws ServletException,IOException { try{ String data=[Link]("papu"); Cookie cookie = new Cookie("MyCookies",data); [Link](cookie); [Link]("text/html"); PrintWriter pw = [Link](); [Link]("<b>MyCookies is : </b>"); [Link](data); [Link](); } catch(Exception e) { [Link]("Eception : "+e); } } }

import [Link].*; import [Link].*; import [Link].*;

public class GetCookieServlet extends HttpServlet { public void doGet(HttpServletRequest req,HttpServletResponse res) throws ServletException,IOException { try{ Cookie[] cookies=[Link](); [Link]("text/html"); PrintWriter pw = [Link](); [Link]("<b>"); for(int i=0;i<[Link];i++) { String name = cookies[i].getName(); String value = cookies[i].getValue(); [Link]("Name : "+name+"Value : "+value); }

[Link](); } catch(Exception e) {

[Link]("Eception : "+e); } } }

18. Desktop

You might also like