0% found this document useful (0 votes)
133 views

Advanced Java Program

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)
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
133 views

Advanced Java Program

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)
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 37

1. import java.awt.*; import java.awt.event.*; import javax.swing.

*;

public class JLabel_ex extends JApplet {

public void init() { Container cp = getContentPane(); cp.setLayout(new FlowLayout());

ImageIcon ic = new ImageIcon("Sunset1.jpg"); JLabel jl = new JLabel("Sunset is always beautiful",ic,JLabel.CENTER); cp.add(jl);

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

2. import java.awt.*; import java.awt.event.*; import javax.swing.*; import java.net.*;

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

JTextField jtf; public void init() { Container cp = getContentPane(); cp.setLayout(new FlowLayout());

JButton jb = new JButton("Submit"); jb.setActionCommand("Sunset"); jb.addActionListener(this); jtf = new JTextField(10); cp.add(jtf); cp.add(jb);

} public void actionPerformed(ActionEvent ae) { jtf.setText(ae.getActionCommand()); }

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

3. import java.awt.*; import java.awt.event.*; import javax.swing.*; import java.net.*;

public class Jtxtfield_ex extends JApplet { JTextField jtf; public void init() { Container cp = getContentPane(); cp.setLayout(new FlowLayout());

jtf = new JTextField(10); cp.add(jtf);

} }

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

4. import java.awt.*; import java.awt.event.*; import javax.swing.*; import java.net.*;

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(); cp.setLayout(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");

cp.add(jl1); cp.add(jtf1); cp.add(jl2); cp.add(jpf); cp.add(jb1); cp.add(jb2); jta=new JTextArea(10,10); cp.add(jta);

jb1.addActionListener(this); jb2.addActionListener(this);

} public void actionPerformed(ActionEvent ae) { //System.out.println("Hello"); //jta.setText(ae.getActionCommand());

if(ae.getSource()==jb1) { if(jtf1.getText().equals(jpf.getText())) { jta.setText("Welcome "); jta.append(jtf1.getText());

} else { jta.setText("Invalid Login"); } }

if(ae.getSource()==jb2)

{ jtf1.setText(""); jpf.setText(""); jta.setText(""); }

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

5. import java.awt.*; import java.awt.event.*; import javax.swing.*;

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

Container cp = getContentPane(); cp.setLayout(new FlowLayout());

ImageIcon normal = new ImageIcon("Sunset1.jpg"); ImageIcon rollover = new ImageIcon("Sunset1.jpg"); ImageIcon selected = new ImageIcon("sunset1.jpg");

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

jcb2.setRolloverIcon(new ImageIcon("Water.jpg")); cp.add(jcb1);

cp.add(jcb2); cp.add(jcb3); cp.add(jtf); jcb1.addItemListener(this); jcb2.addItemListener(this); jcb3.addItemListener(this); } public void itemStateChanged(ItemEvent ie) { JCheckBox cb = (JCheckBox)ie.getItem(); jtf.setText(cb.getText()); }

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

6. import java.awt.*; import java.awt.event.*; import javax.swing.*;

public class combo extends JApplet implements ItemListener

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

Container cp = getContentPane(); cp.setLayout(new FlowLayout());

jcb = new JComboBox(); jcb.addItem("OFFICER"); jcb.addItem("MANAGER"); jcb.addItem("CLERK"); jcb.addItem("SUPERVISOR"); jcb.addItemListener(this); cp.add(jcb); cp.add(jtf); } public void itemStateChanged(ItemEvent ie) { String s = (String)ie.getItem(); jtf.setText(s); //jtf.setText(jcb.getSelectedItem()); }

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

7. import java.awt.*; import java.awt.event.*; import javax.swing.*;

public class jtabbedpane extends JApplet {

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

jtp.addTab("cities",new citiesPane()); jtp.addTab("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); jcb1.addItemListener(this); jcb2.addItemListener(this); jcb3.addItemListener(this); jcb4.addItemListener(this); } public void itemStateChanged(ItemEvent ie) { JCheckBox cb = (JCheckBox)ie.getItem(); //jtf.setText(cb.getSelectedText()); jtf.setText(cb.getText()); }

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

9. import java.awt.*; import java.awt.event.*; import javax.swing.*;

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

Container cp = getContentPane(); cp.setLayout(new GridLayout());

JPanel jp = new JPanel(); jp.setLayout(new GridLayout(20,20)); int b=0; for(int i=0;i<25;i++) for(int j=0;j<25;j++) { jp.add(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); cp.add(jsp,BorderLayout.CENTER);

/* <applet code=scrolldemo.class height=400 width=400> </applet> */

10. import java.awt.*; import java.awt.event.*; import javax.swing.*;

public class tabledemo extends JApplet { //JButton jb = new JButton(); //JTextField jtf; public void init() { Container cp = getContentPane(); cp.setLayout(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); cp.add(jsp,BorderLayout.CENTER);

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

11. import java.awt.*; import java.awt.event.*; import javax.swing.*; import javax.swing.tree.*;

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

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

top.add(b); b.add(b1); b.add(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); cp.add(jsp,BorderLayout.CENTER); jtf=new JTextField(20); jt.addMouseListener(new MouseAdapter() { public void mouseClicked(MouseEvent me) {doMouseClicked(me);} }

); } void doMouseClicked(MouseEvent me) { TreePath tp= jt.getPathForLocation(me.getX(),me.getY()); if(tp!=null) jtf.setText(tp.toString()); else jtf.setText(" "); }

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

12. import java.awt.*; import java.awt.event.*; import javax.swing.*;

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

JRadioButton b1 = new JRadioButton("A");

b1.addActionListener(this); cp.add(b1); JRadioButton b2 = new JRadioButton("B"); b2.addActionListener(this); cp.add(b2); JRadioButton b3 = new JRadioButton("C"); b3.addActionListener(this); cp.add(b3); bg= new ButtonGroup(); bg.add(b1); bg.add(b2); bg.add(b3); jtf = new JTextField(10); //cp.add(bg); cp.add(jtf);

} public void actionPerformed(ActionEvent ae) { jtf.setText(ae.getActionCommand()); }

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

13. PROGRAM ONE : import java.rmi.*;

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

PROGRAM TWO : import java.rmi.*; import java.rmi.server.*;

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 java.rmi.*; import java.net.*;

public class AddServer { public static void main(String args[]) { try { AddServerImpl asi=new AddServerImpl(); Naming.rebind("AddServer",asi); } catch(Exception e) { System.out.println("Exception : "+e); }

} } PROGRAM FOUR : import java.rmi.*;

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

14. import java.awt.*; import java.io.*; import javax.imageio.*; import javax.swing.*; public class ImageViewer extends JLabel { public ImageViewer() {} public void setFileName(String filename) { try{ file = new File("Sunset.jpg"); setIcon(new ImageIcon(ImageIO.read(file))); } catch(IOException e) { file=null; setIcon(null); } } public String getFileName() { if(file==null) {

return null; } return file.getPath(); } 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 java.io.*; import javax.servlet.*; public class HelloServlet extends GenericServlet { public void service(ServletRequest req,ServletResponse res) throws ServletException,IOException { try{ res.setContentType("text/html"); PrintWriter pw = res.getWriter(); pw.println("HelloWorld"); pw.close(); } catch(Exception e) { System.out.println("Eception : "+e); } } }

16. <html> <body>

<form name="form1" method="post" action="http://localhost:8080/examples/PostParameterServlet">

<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 java.io.*; import javax.servlet.*; import java.util.*;

public class PostParameterServlet extends GenericServlet { public void service(ServletRequest req,ServletResponse res) throws ServletException,IOException { try{ PrintWriter pw = res.getWriter(); Enumeration e = req.getParameterNames(); while(e.hasMoreElements()) { String pname=(String)e.nextElement(); pw.println(pname+" = "); String pvalue=req.getParameter(pname); pw.println(pvalue); } pw.close(); }

catch(Exception e) { System.out.println("Eception : "+e); } } }

17. import java.io.*; import javax.servlet.*; import javax.servlet.http.*;

public class AddCookieServlet extends HttpServlet { public void doPost(HttpServletRequest req,HttpServletResponse res) throws ServletException,IOException { try{ String data=req.getParameter("papu"); Cookie cookie = new Cookie("MyCookies",data); res.addCookie(cookie); res.setContentType("text/html"); PrintWriter pw = res.getWriter(); pw.println("<b>MyCookies is : </b>"); pw.println(data); pw.close(); } catch(Exception e) { System.out.println("Eception : "+e); } } }

import java.io.*; import javax.servlet.*; import javax.servlet.http.*;

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

pw.close(); } catch(Exception e) {

System.out.println("Eception : "+e); } } }

18. Desktop

You might also like