Tuesday, May 28, 2019

Event Handing-list


 program to show message on applet status according to selected option of list

import java.applet.*;
import java.awt.*;
import java.awt.event.*;

public class pst10 extends Applet implements ItemListener
{
       List l;
       String msg;
       public void init()
       {
              l=new List();
              l.add("apple");
              l.add("mango");
              l.add("banana");
              add(l);
              l.addItemListener(this);
             
       }
       public void itemStateChanged(ItemEvent ie)
       {
              repaint();
       }
       public void paint(Graphics g)
       {
              msg="your selection string";     
       msg+=l.getSelectedItem();//this method is use to specify which item is currentely selected
              g.drawString(msg,20,200);
       }
}
/*
<applet code="pst10.java" width=100 height=200>
</applet>
*/
output:
 

Event handling -Radio button

Program to change background color of frame  according to radio button.


import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
/*
<applet code="pst19.java" width=100 height=200>
</applet>

*/
public class pst19 extends JApplet implements ActionListener
{
       Container contentPane=getContentPane();
      
       public void init()
       {
             
              contentPane.setLayout(new FlowLayout());
      
              JTextField jtf=new JTextField(17);
              contentPane.add(jtf);
             
              JRadioButton rb=new JRadioButton("Red");
              rb.addActionListener(this);
              contentPane.add(rb);
              JRadioButton rb1=new JRadioButton("Yellow");
              rb1.addActionListener(this);
              contentPane.add(rb1);
              JRadioButton rb2=new JRadioButton("Blue");
              rb2.addActionListener(this);
              contentPane.add(rb2);
      
              ButtonGroup bg=new ButtonGroup();
              bg.add(rb);
              bg.add(rb1);
              bg.add(rb2);
       }
       public void actionPerformed(ActionEvent ae)
       {
              String str;
              str=ae.getActionCommand();
      
              if(str=="Red")
              {
             
              contentPane.setBackground(Color.red);
              showStatus(str);
              }
                    
              else if(str=="Yellow")
              {
             
              contentPane.setBackground(Color.yellow);
              showStatus(str);
              }
             
              else if(str=="Blue")
              contentPane.setBackground(Color.blue);
       showStatus(str);
             
             
       }
}

Monday, May 27, 2019

Java Program(Network)


Java program of URL connection to get Port ,get Content Type, get Last Modified etc.
import java.net.*;
import java.io.*;
import java.util.Date;

class pst15
{
       public static void main(String str[])throws Exception
       {
              URL u=new URL("http://presidentofindia.nic.in/speeches.html");
             
              URLConnection uc=u.openConnection();
              System.out.println("Date:"+ new Date(uc.getDate()));
              System.out.println("port is"+u.getPort());
              System.out.println("type of content:"+ uc.getContentType());
              System.out.println("Expire:"+ uc.getExpiration());
              System.out.println("date of last modified:"+ new Date(uc.getLastModified()));
              int len=uc.getContentLength();
              System.out.println("length"+len);

       }
      
}
/*output:
payal@payal-HP-Notebook:~$ javac pst15.java
payal@payal-HP-Notebook:~$ java pst15
Date:Thu Jan 01 05:30:00 IST 1970
type of content:null
Expire:0
date of last modified:Thu Jan 01 05:30:00 IST 1970
length-1
*/


java program to perform to understand the InetAddress in Advance java
import java.net.InetAddress;
import java.net.UnknownHostException;

public class pst27
{
       public static void main(String arg[])
       {
              try
              {
                     InetAddress local=InetAddress.getLocalHost();
                     String address=local.getHostAddress();
                     String hostName=local.getHostName();
                    
                     System.out.println("local host IP address:"+address);
                     System.out.println("local host name:"+hostName);
              }
              catch(UnknownHostException e)
              {
                     e.printStackTrace();
              }
       }
}
/*
payal@payal-HP-Notebook:~$ javac pst27.java
payal@payal-HP-Notebook:~$ java pst27
local host IP address:127.0.1.1
local host name:payal-HP-Notebook
*/     



 Java Program to perform some of the function of URL class

import java.net.*;
import java.io.*;

public class pst28
{
       public static void main(String obj[])
       {
              try
              {
                     URL u=new URL("http://www.tutorialpoint.com/java8/index.htm");
                     System.out.println("URL is"+u.toString());
                     System.out.println("protocol is"+u.getProtocol());
                     System.out.println("authority is"+u.getAuthority());
                     System.out.println("file name is"+u.getFile());
                     System.out.println("host is"+u.getHost());
                     System.out.println("path is"+u.getPath());
                     System.out.println("port is"+u.getPort());
                     System.out.println("default port"+u.getDefaultPort());
                           System.out.println("query is"+u.getQuery());
              System.out.println("ref is"+u.getRef());
System.out.println("External form:"+u.toExternalForm());
              }
              catch(Exception e)
              {
                     e.printStackTrace();
              }
}
/*output
payal@payal-HP-Notebook:~$ javac URLDemo.java
payal@payal-HP-Notebook:~$ java URLDemo
URL ishttp://www.tutorialpoint.com/java8/index.htm
protocol ishttp
authority iswww.tutorialpoint.com
file name is/java8/index.htm
host iswww.tutorialpoint.com
path is/java8/index.htm
port is-1
default port80
query isnull
ref isnull
*/ 
   

}     
                    
 


java program (Tree)