Wednesday, 17 July 2013

how to use a scrollbar in java

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

class MyFrame extends Frame implements AdjustmentListener
{
 Scrollbar sbr,sbg,sbb;
 Panel p;

 MyFrame()
 {
  setTitle("Scrollbar Demo");
  setSize(400,300);
  setBackground(new Color(200,200,255));

  sbr=new Scrollbar(Scrollbar.VERTICAL,255,10,0,265);
  sbg=new Scrollbar(Scrollbar.VERTICAL,255,10,0,265);
  sbb=new Scrollbar(Scrollbar.VERTICAL,255,10,0,265);

  p=new Panel();
  p.setBackground(Color.black);

  setLayout(new GridLayout(1,2,20,20));
 
  Panel p1=new Panel();
  p1.setLayout(new GridLayout(1,4,15,5));
  p1.add(sbr);
  p1.add(sbg);
  p1.add(sbb);
  p1.add(new Label());

  add(p1);
  add(p);
  
  sbr.addAdjustmentListener(this);
  sbg.addAdjustmentListener(this);
  sbb.addAdjustmentListener(this);

  addWindowListener(new MyWndAdapter(this)); 
 }

 public Insets getInsets()
 {
  return new Insets(50,20,20,20);
 }

 public void adjustmentValueChanged(AdjustmentEvent ae)
 {
  int r,g,b;
  r=255-sbr.getValue();
  g=255-sbg.getValue();
  b=255-sbb.getValue();

  p.setBackground(new Color(r,g,b));
 }
}


class MyWndAdapter extends WindowAdapter
{
 MyFrame mf;

 MyWndAdapter(MyFrame mf)
 {
  this.mf=mf;
 }

 public void windowClosing(WindowEvent we)
 {
  mf.dispose();
  System.exit(0);
 }
}

class SBDemo
{
 public static void main(String arg[])
 {
  MyFrame mf=new MyFrame();
  mf.setVisible(true);
 }
}
---------------------------------------------------------------------------------------------------------------

steps to run the Demo program of Scrollbar
step1: save the file as frm.java file in the bin and refresh it.

step2: after that open ur command prompt and write the path of ur jdk
       
       like my jdk path is
       C:\java\jdk1.7.0_10\bin
step3: if u not know how to make path then follow the steps below





step4: after that to execute the program do the following

C:\java\jdk1.7.0_10\bin>javac SBDemo.java and press enter

step5: if the step4 is successfull then perform this step

C:\java\jdk1.7.0_10\bin>java SBDemo



and enjoy!!  ;)

if u have any problem then msg me at
rawlot007bhati@gmail.com

program for a form window in java

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

class MyFrame extends Frame implements ActionListener
{
 Checkbox cMl,cFl,cCs,cIt,cEc;
 Checkbox cJava,cPython,cRuby,cNet;
 Button bOk,bCancle,bExit;

 CheckboxGroup cbg1,cbg2;

 MyFrame()
 {
  super("FORM");
  setSize(400,350);
  setBackground(new Color(200,200,255));

  Label lSex=new Label("Sex");
  lSex.setFont(new Font("lucida console",Font.PLAIN,18));

  Label lBranch=new Label("Branch");
  lBranch.setFont(new Font("lucida console",Font.PLAIN,18));

  Label lTech=new Label("Technologies");
  lTech.setFont(new Font("lucida console",Font.PLAIN,18));

  cJava=new Checkbox("Java");
  cJava.setFont(new Font("lucida console",Font.PLAIN,18));
  cPython=new Checkbox("Python");
  cPython.setFont(new Font("lucida console",Font.PLAIN,18));
  cRuby=new Checkbox("Ruby");
  cRuby.setFont(new Font("lucida console",Font.PLAIN,18));
  cNet=new Checkbox(".Net");
  cNet.setFont(new Font("lucida console",Font.PLAIN,18));
 
  cbg1=new CheckboxGroup();
  cMl=new Checkbox("Male",cbg1,true);
  cMl.setFont(new Font("lucida console",Font.PLAIN,18));
  cFl=new Checkbox("Female",cbg1,false);
  cFl.setFont(new Font("lucida console",Font.PLAIN,18));

  cbg2=new CheckboxGroup();
  cCs=new Checkbox("CS",cbg2,true);
  cCs.setFont(new Font("lucida console",Font.PLAIN,18));
  cIt=new Checkbox("IT",cbg2,false);
  cIt.setFont(new Font("lucida console",Font.PLAIN,18));
  cEc=new Checkbox("EC",cbg2,false);
  cEc.setFont(new Font("lucida console",Font.PLAIN,18));

  bOk=new Button("OK");
  bOk.setFont(new Font("lucida console",Font.PLAIN,18));
  bOk.setBackground(new Color(200,255,200));
 
  bCancle=new Button("Cancle");
  bCancle.setFont(new Font("lucida console",Font.PLAIN,18));
  bCancle.setBackground(new Color(255,200,200));

  bExit=new Button("Exit");
  bExit.setFont(new Font("lucida console",Font.PLAIN,18));
  bExit.setBackground(new Color(220,220,255));

  Panel p1=new Panel();
  p1.setLayout(new BorderLayout(57,10));

   Panel p11=new Panel();
   p11.setLayout(new GridLayout(1,2,10,10));
   p11.add(cMl);
   p11.add(cFl);

  p1.add(lSex,BorderLayout.WEST);
  p1.add(p11,BorderLayout.CENTER);


  Panel p2=new Panel();
  p2.setLayout(new BorderLayout(30,10));

   Panel p21=new Panel();
   p21.setLayout(new GridLayout(1,3,10,10));
   p21.add(cCs);
   p21.add(cIt);
   p21.add(cEc);

  p2.add(lBranch,BorderLayout.WEST);
  p2.add(p21,BorderLayout.CENTER);

  Panel p3=new Panel();
  p3.setLayout(new BorderLayout(10,10));
  
   Panel p31=new Panel();
   p31.setLayout(new GridLayout(1,4,10,10));
   p31.add(cJava);
   p31.add(cPython);
   p31.add(cRuby);
   p31.add(cNet);
 
  p3.add(lTech,BorderLayout.NORTH);
  p3.add(p31,BorderLayout.CENTER);

  Panel p4=new Panel();
  p4.setLayout(new BorderLayout(10,10));

   Panel p41=new Panel();
   p41.setLayout(new GridLayout(1,3,20,20));
   p41.add(bOk);
   p41.add(bCancle);
   p41.add(bExit);

  p4.add(new Label(),BorderLayout.NORTH);
  p4.add(p41,BorderLayout.CENTER);
  
  setLayout(new GridLayout(4,1,10,10));
  add(p1);
  add(p2);
  add(p3);
  add(p4);

  addWindowListener(new MyWndAdapter(this));

  bOk.addActionListener(this);
  bCancle.addActionListener(this);
  bExit.addActionListener(this);
 
 }

 public Insets getInsets()
 {
  return new Insets(50,20,20,20);
 }

 public void actionPerformed(ActionEvent ae)
 {
  if(ae.getSource()==bOk)
  {
   String str="Sex          : ";
   str=str+cbg1.getSelectedCheckbox().getLabel();
  
   str=str+"\nBranch       : "+cbg2.getSelectedCheckbox().getLabel();

   str=str+"\nTechnologies : ";
   if(cJava.getState())
    str=str+"Java  ";
   if(cPython.getState())
    str=str+"Python  ";
   if(cRuby.getState())
    str=str+"Ruby  ";
   if(cNet.getState())
    str=str+".Net  ";

   System.out.println(str);

  }
  else
  if(ae.getSource()==bCancle)
  {
  
  }
  else
  if(ae.getSource()==bExit)
  {
   dispose();
   System.exit(0);
  }
 }

}


class MyWndAdapter extends WindowAdapter
{
 MyFrame mf;

 MyWndAdapter(MyFrame mf)
 {
  this.mf=mf;
 }

 public void windowClosing(WindowEvent we)
 {
  mf.dispose();
  System.exit(0);
 }
}

class frm
{
 public static void main(String arg[])
 {
  MyFrame mf=new MyFrame();
  mf.setVisible(true);
 }

-------------------------------------------------------------------------------------------------------------
steps to run the Demo program of Form

step1: save the file as frm.java file in the bin and refresh it.

step2: after that open ur command prompt and write the path of ur jdk
       
       like my jdk path is
       C:\java\jdk1.7.0_10\bin
step3: if u not know how to make path then follow the steps below






step4: after that to execute the program do the following

C:\java\jdk1.7.0_10\bin>javac frm.java and press enter



step5: if the step4 is successfull then perform this step

C:\java\jdk1.7.0_10\bin>java frm

then a window appears like this




and enjoy!!  ;)

if u have any problem then msg me at
rawlot007bhati@gmail.com

program to copy paste in java

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

class TextDemo extends Frame implements ActionListener,KeyListener,WindowListener
{
 TextField tSrc,tDst;
 Button bCpy,bMov,bSel,bClr;

 TextDemo()
 {
  setTitle("TextDemo");
  setSize(550,350);
  setBackground(new Color(150,150,150));

  tSrc=new TextField();
  tSrc.setFont(new Font("lucida console",Font.PLAIN,18));
 
  tDst=new TextField();
  tDst.setFont(new Font("lucida console",Font.PLAIN,18));
  tDst.setEditable(false);

  bCpy=new Button("Copy");
  bCpy.setFont(new Font("lucida console",Font.PLAIN,18));
  bCpy.setBackground(new Color(255,190,190));
  bCpy.setEnabled(false);
 
  bMov=new Button("Move");
  bMov.setFont(new Font("lucida console",Font.PLAIN,18));
  bMov.setBackground(new Color(255,190,190));
  bMov.setEnabled(false);

  bSel=new Button("Copy Selected");
  bSel.setFont(new Font("lucida console",Font.PLAIN,18));
  bSel.setBackground(new Color(255,190,190));
  bSel.setEnabled(false);

  bClr=new Button("Clear");
  bClr.setFont(new Font("lucida console",Font.PLAIN,18));
  bClr.setBackground(new Color(190,255,190));

  Label l1=new Label("Source");
  l1.setFont(new Font("lucida console",Font.PLAIN,18));

  Label l2=new Label("Destination");
  l2.setFont(new Font("lucida console",Font.PLAIN,18));

  setLayout(new GridLayout(4,1,10,10));
 
  Panel p1=new Panel();
  p1.setLayout(new BorderLayout(43,10));
 
  p1.add(l1,BorderLayout.WEST);
  p1.add(tSrc,BorderLayout.CENTER);

  Panel p2=new Panel();
  p2.setLayout(new BorderLayout(10,10));
  p2.add(l2,BorderLayout.WEST);
  p2.add(tDst,BorderLayout.CENTER);

  Panel p3=new Panel();
  p3.setLayout(new GridLayout(1,4,5,10));
  p3.add(bCpy);
  p3.add(bMov);
  p3.add(bSel);
  p3.add(bClr);

  add(p1);
  add(p2);
  add(new Label());
  add(p3);
  
  bCpy.addActionListener(this);
  bMov.addActionListener(this);
  bSel.addActionListener(this);
  bClr.addActionListener(this);
  tSrc.addKeyListener(this);
  addWindowListener(new MyWndAdapter(this));
 }

 public Insets getInsets()
 {
  return new Insets(50,20,20,20);
 }



 public void actionPerformed(ActionEvent ae)
 {
  if(ae.getSource()==bClr)
  {
   tSrc.setText("");
   tDst.setText("");
   bCpy.setEnabled(false);
   bMov.setEnabled(false);
   bSel.setEnabled(false);
  }
  else
  if(ae.getSource()==bCpy)
   tDst.setText(tSrc.getText());
  else
  if(ae.getSource()==bMov)
  {
   tDst.setText(tSrc.getText());
   tSrc.setText("");
   bCpy.setEnabled(false);
   bMov.setEnabled(false);
   bSel.setEnabled(false);
  }
  else
  if(ae.getSource()==bSel)
  {
   tDst.setText(tSrc.getSelectedText());
  }
 }

 public void keyTyped(KeyEvent ke){}
 public void keyPressed(KeyEvent ke){}

 public void keyReleased(KeyEvent ke)
 {
  if(tSrc.getText().length()==0)
  {
   bCpy.setEnabled(false);
   bMov.setEnabled(false);
   bSel.setEnabled(false);
  }
  else
  {
   bCpy.setEnabled(true);
   bMov.setEnabled(true);
   bSel.setEnabled(true);
  }
 }

    @Override
    public void windowOpened(WindowEvent e) {
        throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    }

    @Override
    public void windowClosing(WindowEvent e) {
        throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    }

    @Override
    public void windowClosed(WindowEvent e) {
        throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    }

    @Override
    public void windowIconified(WindowEvent e) {
        throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    }

    @Override
    public void windowDeiconified(WindowEvent e) {
        throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    }

    @Override
    public void windowActivated(WindowEvent e) {
        throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    }

    @Override
    public void windowDeactivated(WindowEvent e) {
        throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    }
}
class MyWndAdapter extends WindowAdapter
{
  TextDemo td;
  MyWndAdapter(TextDemo td)
 {
  this.td=td;
 }
  public void windowClosing(WindowEvent we)
 {
  td.dispose();
  System.exit(0);
 }
}


class Text
{
public static void main(String arg[])
{
TextDemo td;
td=new TextDemo();
td.setVisible(true);
}
}



----------------------------------------------------------------------------------------------------------
steps to run the Demo program of Text

step1: save the file as textdemo.java file in the bin and refresh it.

step2: after that open ur command prompt and write the path of ur jdk
       
       like my jdk path is
       C:\java\jdk1.7.0_10\bin
step3: if u not know how to make path then follow the steps below











step4: after that to execute the program do the following




if there is no error then proceed to step5 otherwise correct the error

step5:after success of step4 perform the following



after this a window appear like this



enjoy!! ;)
if u have any queries then message me on
rawlot007bhati@gmail.com