Seach

Showing posts with label java. Show all posts
Showing posts with label java. Show all posts

simple progress bar in java swing

package application1;
import java.awt.HeadlessException;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
public class Application1 extends JFrame  implements ActionListener
{
JProgressBar jp;
JButton b;
 int i=5;
    public Application1()
    {
        jp=new JProgressBar();
        jp.setStringPainted(true);
        jp.setMinimum(0);
        jp.setMaximum(100);
        add(jp);
      jp.setBounds(30,40, 100,100);
        setLayout(null);
   b=new JButton("click to start");
   add(b);
   b.setBounds(40, 180, 180,80);
       b.addActionListener(this);
       //JOptionPane.showMessageDialog(null, "loading completed");
    }

    @Override
    public void actionPerformed(ActionEvent e)
    {
      
        i=i+5;
        jp.setValue(i);
        int t=jp.getValue();
        if(t==100)
        {
            JOptionPane.showMessageDialog(this, "loading completed");
        }
//     for(int i=0;i<=100;i++)
//     {
//         jp.setValue(i);
//     }
    }
   
    public static void main(String[] args)
    {
        Application1 a=new Application1();
        a.setVisible(true);
        a.setSize(500, 500);
        a.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
       
    }
}

jcheckbox control and associated event

package checkbox_control_and_event;

import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import javax.swing.*;

public class Checkbox_control_and_event extends JFrame implements ItemListener {

    JCheckBox jc1, jc2, jc3;
    JTextField jt;

    public Checkbox_control_and_event() {
        jt = new JTextField(40);
        jc1 = new JCheckBox("c++");
        jc2 = new JCheckBox("java");
        jc3 = new JCheckBox("c");
        add(jc1);
        add(jc2);
        add(jc3);
        add(jt);
        setLayout(null);
        jc1.setBounds(20, 20, 50, 30);
        jc2.setBounds(80, 20, 50, 30);
        jc3.setBounds(140, 20, 50, 30);
        jt.setBounds(20, 80, 120, 40);
        jc1.addItemListener(this);
        jc2.addItemListener(this);
        jc3.addItemListener(this);
    }

    @Override
    public void itemStateChanged(ItemEvent e) {
        if (e.getSource() == jc1)
        {
            jc3.setSelected(false);
           jc2.setSelected(false);
            jt.setText(jc1.getText());
        }
        else if (e.getSource() == jc2)
        {
            jc1.setSelected(false);
            jc3.setSelected(false);
            jt.setText(jc2.getText());
        }
        else if(e.getSource()==jc3)
        {
            jc1.setSelected(false);
            jc2.setSelected(false);
            jt.setText(jc3.getText());
        }
    }

    public static void main(String[] args) {
        Checkbox_control_and_event c = new Checkbox_control_and_event();
        c.setVisible(true);
        c.setSize(400, 400);
    }
}

simple jtextfield and jtextarea


package jframe;
import java.awt.event.*;
import javax.swing.*;

public class Jframe
{
    JTextField jt;
    JTextArea ja;
    public Jframe()
    {
        JFrame j=new JFrame();
        j.setVisible(true);
        j.setSize(500, 500);
        ja=new JTextArea();
        j.add(ja);
        ja.setBounds(200, 100, 100, 40);
        jt=new JTextField(20);
        j.add(jt);
        jt.setBounds(40,100, 100, 40);
       
    }
    public static void main(String[] args)
    {
       
      Jframe j=new Jframe();
       //j.setSize(400, 400);
       //j.setVisible(true);
    }
}

create a frame withou extending jframe class


package jframe;
import java.awt.event.*;
import javax.swing.*;

public class Jframe
{
   
    public Jframe()
    {
        JFrame j=new JFrame();
        j.setVisible(true);
        j.setSize(500, 500);
       
    }
    public static void main(String[] args)
    {
       
      Jframe j=new Jframe();
      
    }
}

simple jradiobutton with its associated event


package jframe;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
import javax.swing.event.*;

public class Jframe extends JFrame implements ActionListener
{
JFrame jf;
JButton jb;
JRadioButton jr1,jr2;
    public Jframe()
    {
        jr1=new JRadioButton("yes");
        jr2=new JRadioButton("no");
       setLayout(null);
//       jb=new JButton();
//       jb.setText("click me");
//       jb.setBounds(20, 40, 100, 100);
      // add(jb);
       jr1.setBounds(40, 40, 80, 80);
      add(jr1);
      jr2.setBounds(40, 100, 80, 80);
      add(jr2);
      jr1.addActionListener(this);
      jr2.addActionListener(this);
      // jb.addActionListener(this);
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        System.out.println("event generated");
        if(e.getSource()==jr1)
       
        {System.out.println("yes");
        jr2.setSelected(false);
           
        }
        else
        {
            System.out.println("no");
            jr1.setSelected(false);
        }
    }
   
   

       
   
    public static void main(String[] args)
    {
      Jframe j=new Jframe();
       j.setSize(400, 400);
       j.setVisible(true);
    }
}

simple jframe with jbutton and associated event


package jframe;
import java.awt.event.*;
import javax.swing.*;

public class Jframe extends JFrame implements ActionListener
{
JFrame jf;
JButton jb;
JRadioButton jr1,jr2;
    public Jframe()
    {
       setLayout(null);
       jb=new JButton();
       jb.setText("click me");
       jb.setBounds(20, 40, 100, 100);
      // add(jb);
     
       jb.addActionListener(this);
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        System.out.println("event generated");
    }

   
    public static void main(String[] args)
    {
      Jframe j=new Jframe();
       j.setSize(400, 400);
       j.setVisible(true);
    }
}

list control in java example

import java.applet.*;
import java.awt.*;
import java.awt.event.*;
public class list_ extends Applet
{
List l;
TextField t;
public void init()
{
l=new List();
t=new TextField();
add(t);
add(l);
}



}

simple menu program in java

import java.awt.*;
import java.awt.event.*;
public class menu_ extends Frame implements ActionListener
{
MenuBar mb;
Menu m;
MenuItem m1,m2,m3,m4;
public menu_()
{
mb=new MenuBar();
setMenuBar(mb);
m=new Menu("file");
mb.add(m);
m1= new MenuItem("1");
m2=new MenuItem("2");
m3=new MenuItem("3");
m4=new MenuItem("4");
m.add(m1);
m.add(m2);
m.add(m3);
m.add(m4);
m1.addActionListener(this);
m2.addActionListener(this);
m3.addActionListener(this);
m4.addActionListener(this);
addWindowListener(new c());
}
public void actionPerformed(ActionEvent e)
{
if(e.getSource()==m1)
System.out.println("m1 is clicked");
else if(e.getSource()==m2)
System.out.println("m2 is clicked");
else if(e.getSource()==m3)
System.out.println("m3 is clicked");
else if(e.getSource()==m4)
System.out.println("m4 is clicked");
}
public static void main(String[] s)
{
menu_ me=new menu_();
me.setVisible(true);
me.setSize(400,400);
}
}
class c extends WindowAdapter
{


    public void windowClosing(WindowEvent e)
    {
        System.exit(0);
    }
   
}

abstract class example in java

abstract class a
{
abstract public void show();
}
public class b extends a
{
public void show()
{
System.out.println("overriding successfuly");
}
public static void main(String[] s)
{
b obj=new b();
obj.show();

}
}

interface in java a simple example

interface i
{
public void show();
}
public class x implements i
{
public void show()
{
System.out.println("Interface method has been overrided");
}
public static void main(String[] s)
{
x obj=new x();
obj.show();
}
}

how to read and write to a file

import java.io.*;
public class file
{
public static void main(String[] s)
{
BufferedReader b;
FileReader r=null;
FileWriter w=null;
try
{
System.out.println("Enter the data to Write to a file:");
b=new BufferedReader(new InputStreamReader(System.in));
w=new FileWriter("data.txt");
w.write(b.readLine());
b.close();
w.close();
r=new FileReader("data.txt");
b=new BufferedReader(r);
System.out.println("the data stored on the data.txt");
System.out.println(b.readLine());
b.close();
r.close();
}
catch(Throwable t)
{
System.out.println("occured exception is="+t);
}
}
}

reading data from key board


package readfromkeybord;
import java.io.*;
public class Readfromkeybord {

    public static void main(String[] args) {
       // System.out.println("enter the message:");s
        try
        {
        BufferedReader b=new BufferedReader(new InputStreamReader(System.in));
        // x=b.readLine();
        // TODO code application logic here
        Readfromkeybord r=new Readfromkeybord();
        String ss[]={"hithin","chandran"};

        System.out.println("enter the string");
        String s="";
       
        s.concat(ss[0]);
       
    }
        catch(Exception e)
        {
            System.out.println("error due to="+e);
           
        }
    }
}

reading and writing to a file in java

import java.io.*;
class fr
{
 public static void main(String args[])
  {
  try{
  // Create file
  FileWriter fstream = new FileWriter("h:\\readx.txt");
  BufferedWriter out = new BufferedWriter(fstream);
System.out.println("enter the data to insert");
String sss="";
BufferedReader b=new BufferedReader(new InputStreamReader(System.in));
String s=b.readLine();
  out.write(s);
  //Close the output stream
FileReader fread=new FileReader("H:\\readx.txt");
BufferedReader br=new BufferedReader(fread);
String ss="";
int i;
String sx="";
do
{
i=br.read();
if(i!=-1)
//ss=br.readLine().toString();
System.out.println("data"+br.readLine().toString());
}
while(i!=-1);
//System.out.println("inputed String="+sx);
br.close();
fread.close();
  out.close();
//System.out.println("data in the file is:"+ss.leng);
  }catch (Exception e){//Catch exception if any
  System.err.println("Error: " + e.getMessage());
  }
  }
}

muti threading odd and even example in java


class odd extends Thread
{
    int i=10;
    public void ogen()
    {
        while(i!=0)
        {
            if(i%2!=0)
            {
                System.out.println(i);
            }
            i--;
        }
    }
    @Override
    public void run()
    {
        ogen();
    }
}
class even extends Thread
{
    public void egen()
    {
        int i=10;
        while(i!=0)
        {
            if(i%2==0)
            {
                System.out.println(i);
            }
            i--;
        }
    }
    public void run()
    {
        egen();
    }
}


public class Multi_threading
{

  
    public static void main(String[] args)
    {
       
    odd o=new odd();
    even e=new even();
    System.out.println("odd numbers :");
    o.start();
    System.out.println("even numbers:");
    e.start();
  
   
               
}
}

generic concept in java

class box<t>
{
t e;
public box(t t1)
{
this.e=t1;
}
public t show()
{
return e;
}
}
public class g1
{
public static void main(String s[])
{
box <Integer> i=new box<Integer>(42);
System.out.println("integer value="+i.show());
box<String>i2=new box<String>("hithin");
System.out.println("string value="+i2.show());
}
}

load an image to a applet

import java.awt.*;
import java.applet.*;
public class img extends Applet
{
Image i;
public void init()
{
i=getImage(getDocumentBase(),"002.jpg");//getcode base is used to findout the url of the code containing directory
}
public void paint(Graphics g)
{
g.drawImage(i,40,40,this);
}
}
note
pls first  copy the image to the applet class containing folder 

jdbc console program to demonstrate insertion,deletion,selection,updation

import java.sql.*;
import java.io.*;
public class c
{
public static void main(String arg[])
{
System.out.println("\n1:INSERT\n2:DELETE\n3:SELECT\n4:UPDATE\n5:EXIT");
int n;
try

{
    BufferedReader bx=new BufferedReader(new InputStreamReader(System.in));
    Connection con =null;
    String URL="jdbc:mysql://localhost/hithin";
    Class.forName("com.mysql.jdbc.Driver");
    con=DriverManager.getConnection(URL,"root","");
    do
    {
    System.out.println("\n1:INSERT\n2:DELETE\n3:SELECT\n4:UPDATE\n5:EXIT");
    System.out.println("\nENTER YOUR CHOICE");
    n=Integer.parseInt(bx.readLine());
    if(n==1)
    {System.out.println("Insertion option is selected");
        System.out.println("Enter the values you want to insert");
        int a=Integer.parseInt(bx.readLine());
        int b=Integer.parseInt(bx.readLine());
        int c=Integer.parseInt(bx.readLine());
        try{
            Statement stmt=con.createStatement();
            String createString = "insert into orderdetails1(ono,bid,qty) values("+a+","+b+","+c+")";
            stmt.executeUpdate(createString);
            ResultSet rs=stmt.executeQuery("select * from orderdetails1");    
                       System.out.println("MySQL JDBC driver loaded ok.");
            System.out.println("\n\t***************");   
            System.out.println("\t  ORDERDETAILS");
            System.out.println("\t***************");
            System.out.println("\n\tORDER NO    BOOK ID   QUANTITY");
                        while(rs.next())
                          {    System.out.println("\n");
                System.out.println("\t");
                                System.out.print("\t"+rs.getString(1));
                System.out.print("\t\t");
                                System.out.print(rs.getString(2));
                System.out.print("\t");
                System.out.print(rs.getString(3));
                           }
                    
        }catch (Exception e)
        {
                   
        System.out.println("Exception: "+e);

        }
    }
    else if(n==2)
    {
System.out.println("Deletion option is selected");
        try
        { int s;
System.out.println("enter the id to delete:");
s=Integer.parseInt(bx.readLine());

            Statement stmt=con.createStatement();
            String createString = "delete from orderdetails1 where ono="+s+" ";
            stmt.executeUpdate(createString);
            ResultSet rs=stmt.executeQuery("select * from orderdetails1");    
                       System.out.println("Records Deleted.");
       
        }
        catch(Exception e)
        {
System.out.println("occured error is:"+e);
}
    }
    else if(n==3)
    {
System.out.println("Update option is selected:");
        try
        {
            Statement stmt=con.createStatement();
            //String createString = "insert into orderdetails1(ono,bid,qty) values("+a+","+b+","+c+")";
            //stmt.executeUpdate(createString);
                      ResultSet rs=stmt.executeQuery("select * from orderdetails1");    
                       System.out.println("MySQL JDBC driver loaded ok.");
            System.out.println("\n\t***************");   
            System.out.println("\t  ORDERDETAILS");
            System.out.println("\t***************");
                        while(rs.next())
                          {    System.out.println("\n");
                System.out.println("\t");
                                System.out.print("\t"+rs.getString(1));
                System.out.print("\t");
                                System.out.print(rs.getString(2));
                System.out.print("\t");
                System.out.print(rs.getString(3));
                           }                       
       
        }
        catch(Exception e)
        {}
    }
    else if(n==4)
    {
        try
        {
            System.out.println("Enter the book id of the record you want to update(bid integer value)");
            int x=Integer.parseInt(bx.readLine());
            System.out.println("Enter the field you want to update:order no or quantity");
            String s=bx.readLine();
            System.out.println("enter the value to be updated");
            int y=Integer.parseInt(bx.readLine());
            String s1=new String("order no");
            Statement stmt;
            String createString;
            if(s=="orderno")
            {   
                stmt=con.createStatement();           
                createString ="update orderdetails1 set ono="+y+" where bid="+x+"";
stmt.executeUpdate(createString);
            }
            else
            {
                stmt=con.createStatement();
                createString ="update orderdetails1 set qty="+y+" where bid="+x+"";
stmt.executeUpdate(createString);
            }
            stmt.executeUpdate(createString);
            ResultSet rs=stmt.executeQuery("select * from orderdetails1");    
                       System.out.println("MySQL JDBC driver loaded ok.");
            System.out.println("\n\t***************");   
            System.out.println("\t  ORDERDETAILS");
            System.out.println("\t***************");
                        while(rs.next())
                          {    System.out.println("\n");
                System.out.println("\t");
                                System.out.print("\t"+rs.getString(1));
                System.out.print("\t");
                                System.out.print(rs.getString(2));
                System.out.print("\t");
                System.out.print(rs.getString(3));
                           }
        }
        catch(Exception e)
        {}
    }
    else if(n==5)
    {
    break;
    }
    }
    while(n!=5);               
    }
catch(Exception e)
{
System.out.println("occured exception:"+e);
}

}
}


tcp client server program in java (console)

myserver.java

import java.io.*;
import java.net.*;
public class myserver
{
public static void main(String arg[])
{
try
{
while(true)//used for lisening multiple clients so we use an infinit loop
{
ServerSocket ss=new ServerSocket(6666);//creating a tcp serversocket to establish connection
Socket s=ss.accept(); //used to accept client
DataInputStream d=new DataInputStream(s.getInputStream());//reading the sended data from the client
String str=(String)d.readUTF();//read into str
System.out.println(str);//writting to console
ss.close();//connection closing
}
}
catch(Throwable t)
{
System.out.println("occured exception :"+t);
}
}
}

insert data to database throug textfield in java

import java.awt.*;
import java.awt.event.*;
import java.sql.*;
public class fdemo extends Frame implements ActionListener
{
Label l1,l2;
TextField t1,t2;
Button b1,b2;
public fdemo()
{
t1=new TextField(10);
t1.setVisible(true);
t2=new TextField(10);
l1=new Label("name");
l2=new Label("id");
l1.setBounds(10,10,20,20);
l2=new Label("age");
b1=new Button("Exit");
b2=new Button("insert");
add(l1);
add(t1);
add(l2);
add(t2);
add(t2);
add(b1);
add(b2);
setLayout( new FlowLayout());
b1.addActionListener(this);
b2.addActionListener(this);
}
public void add_()
{
try
{
//'"s1"'
Class.forName("com.mysql.jdbc.Driver");
      Connection c=DriverManager.getConnection("jdbc:mysql://localhost/rocks","root","");
      Statement s=c.createStatement();
s.executeUpdate("insert into _data values("+t1.getText()+")");

}
catch(Throwable t)
{
System.out.println(t);
}
System.out.println("executed");
}
public void actionPerformed(ActionEvent e)
{
String s=e.getActionCommand();
switch(s)
{
case "Exit":
System.exit(0);
break;
case "insert":
add_();
break;
}

}

public static void main( String s[])
{
fdemo f=new fdemo();
f.setVisible(true);
f.setSize(200,200);
}
}

check box and button demo


package bxxc;
import java.awt.*;
import java.awt.Event.*;
import java.awt.event.*;
public class Bxxc extends Frame implements ItemListener,ActionListener
{

    Checkbox c1,c2;
    Button b1;
public  Bxxc()
{
    Frame f=new Frame("demo");
    f.setSize(100, 100);
    c1=new Checkbox();
    c1.setLabel("disable");
    c1.setLocation(20, 20);
    c2=new Checkbox("enable");
    c2.setLocation(20, 40);
    b1=new Button("show");
    b1.setLocation(20, 60);
    b1.addActionListener(this);
    Panel p=new Panel();
    p.add(b1);
    p.add(c1);
    p.add(c2);
    c1.addItemListener(this);
    f.add(p);
    c2.addItemListener(this);
    p.setSize(100, 100);
    f.setVisible(true);
    p.setVisible(true);
   
    f.addWindowListener(new WindowAdapter() {

          //  @Override
            public void windowClosing(WindowEvent e) {
               
                System.exit(0);
                //super.windowClosing(e);
            }
   
    });
}

     public void itemStateChanged(ItemEvent e) {
 
      // Object o[]=new Object[20];
       //o[0]=e.getItem();
//String s=o[0].toString();
       if(e.getSource()==c1)
       {
           b1.setVisible(false);
       }
       else if(e.getSource()==c2)
       {
           b1.setVisible(true);
       }
       
    }
  
    public void actionPerformed(ActionEvent e) {
      
    Frame fx=new  Frame("redirected form");
    fx.setSize(200, 200);
    fx.addWindowListener(new WindowAdapter() {

            //@Override
            public void windowClosing(WindowEvent e) {
             
                System.exit(0);// super.windowClosing(e);
            }
           
   
           
    });
    //System.out.println("clicked");
    fx.setVisible(true);
  
    }
   

    public static void main(String[] args)
    {
        Bxxc d= new Bxxc();
    }
}