Advanced Java Program
Advanced Java Program
*;
public class JButton_ex extends JApplet implements ActionListener { //JButton jb = new JButton();
JButton jb = new JButton("Submit"); jb.setActionCommand("Sunset"); jb.addActionListener(this); jtf = new JTextField(10); cp.add(jtf); cp.add(jb);
public class Jtxtfield_ex extends JApplet { JTextField jtf; public void init() { Container cp = getContentPane(); cp.setLayout(new FlowLayout());
} }
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());
: ");
jl2=new JLabel("Password : "); jpf=new JPasswordField("",10); jb1 = new JButton("Submit"); jb2 = new JButton("Clear");
jb1.addActionListener(this); jb2.addActionListener(this);
if(ae.getSource()==jb2)
public class chkbox extends JApplet implements ItemListener { JTextField jtf; public void init() { jtf = new JTextField(10);
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");
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()); }
{ JTextField jtf; JComboBox jcb; public void init() { jtf = new JTextField(10);
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()); }
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()); }
public class scrolldemo extends JApplet { //JButton jb = new JButton(); //JTextField jtf; public void init() {
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);
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);
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(" "); }
public class radiodemo extends JApplet implements ActionListener { ButtonGroup bg; JTextField jtf; public void init() { Container cp = getContentPane(); cp.setLayout(new FlowLayout());
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 interface AddServerIntf extends Remote { double add(double d1,double d2) throws RemoteException; }
{ 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); }
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); } } }
</html>
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(); }
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); } } }
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