Monday, February 13, 2012

Append the File

Before Append:
import java.io.*;

public class Append
{
    public static void main(String[] args) 
    {
  try 
  {
   DataInputStream in = new DataInputStream(System.in);
   BufferedWriter out = new BufferedWriter(new FileWriter("Buffered.java", true));
   System.out.println("How many lines?");
   int numberOfLines = Integer.parseInt(in.readLine());
   String line = "";
    
   for(int i = 0; i < numberOfLines; i++)
   {
    line = in.readLine();
    out.write("\n"+ line ); // throws IOException
   }
   out.close();
  } 
  catch (IOException e) 
  {
  }
    }
}
output:
After this check the file

Saturday, February 4, 2012

Progress Bar

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

class ProgressBarr extends JFrame implements ActionListener
{
 JButton jb;
 JProgressBar jpb1;
 int value =0;
 ProgressBarr()
 {
  Container contentPane=getContentPane();
  contentPane.setLayout(new FlowLayout());

  jb = new JButton("Click");
  contentPane.add(jb);
  jb.addActionListener(this);

  jpb1 = new JProgressBar(0,5000);
  contentPane.add(jpb1);
  jpb1.setStringPainted(true);
 }

 public static void main(String args[])
 {
  ProgressBarr pb1 = new ProgressBarr();
  pb1.setSize(300,80);
  pb1.setTitle("Progress Bar");
  pb1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  pb1.setVisible(true);
 }

 public void actionPerformed(ActionEvent ae)
 {
  while(value <= 1000)
  {
   value = value+10;
   jpb1.setValue(value);
  }
 }
}
Output:-

Table

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

public class Table extends JFrame
{
 Object[][] cells={{"ABC",999999},{"XYZ",999900},{"ABC",999999},{"XYZ",999900},{"ABC",999999}};
 String[] columnName={"Name","Employee ID"};
 JTable table=new JTable(cells,columnName);
 JScrollPane pane=new JScrollPane(table);
 Container con;
  
 Table()
 {
  super("LOG IN");con=getContentPane();
  setSize(200,200);
  setLayout(null);
        pane.setBounds(20,20,150,100);
  con.add(pane);
        setVisible(true);
  setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            
        }
 public static void main(String s[])
 {
  Table obj=new Table();
 }
}
Output:-

Tree

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

public class Tree1 extends JFrame
{
 public static void main(String args[])
 {
  Tree1 t1 = new Tree1("A Tree");
  t1.setSize(200,200);
  t1.setVisible(true);
  t1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
 }
 public Tree1(String title)
 {
  setTitle(title);
  DefaultMutableTreeNode root=new DefaultMutableTreeNode ("Engineering");
  DefaultMutableTreeNode style=new DefaultMutableTreeNode ("Information Technology");
  root.add(style);
  style=new DefaultMutableTreeNode("Electronics");
  root.add(style);
  style=new DefaultMutableTreeNode ("Computer Science");
  root.add(style);
  style=new DefaultMutableTreeNode ("Mechanical");
  root.add(style);
  style=new DefaultMutableTreeNode ("Electrical");
  root.add(style);
  style=new DefaultMutableTreeNode ("Sound");
  root.add(style);

  JTree jt=new JTree(root);
  Container contentPane=getContentPane();
  contentPane.add(new JScrollPane(jt));
 }
}
Output:-

Package

Main.java
import pack.*;
import javax.swing.*;
import java.awt.*;

class Main extends JFrame
{
 public static void main(String[] s)
 {  
  JFrame f1=new JFrame();
  f1.setSize(200,200);
  f1.setBackground(Color.blue);
  f1.setLayout(null);
  f1.setVisible(true);
  f1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  
  Pan d1=new Pan(); 
  f1.add(d1);
 }
}
-------------------------
Pan.java
package pack;
import javax.swing.*;
import java.awt.*;

public class Pan extends JPanel
{
 JLabel l1;
 public Pan()
 {
  setSize(100,100);
  setLayout(null);
  setBackground(Color.red);
  setVisible(true);
  setBounds(50,50,80,40);
  l1=new JLabel("PANEL");
  l1.setBounds(15,15,50,10);
  add(l1);
 }
}  
Output:-

Friday, February 3, 2012

Timer

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

public class JavaSimpleTimer extends JPanel implements ActionListener  
{  
 int miliseconds=0;  
 int seconds=0;  
 int minutes=0;  
   
 Timer myTimer;  
 Font timerFont=new Font("Verdana",Font.BOLD,24);  
   
 public JavaSimpleTimer()  
 {  
  myTimer=new Timer(10,this);    
  JFrame myFrame=new JFrame("Simple Timer");    
  myFrame.add(this);  
  myFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);  
  myFrame.setSize(200,100);  
  myFrame.setVisible(true);  
  myTimer.setInitialDelay(0);  
  myTimer.start();  
 }  
  
 public void paint(Graphics g)  
 {  
  super.paint(g);  
  String mili=Integer.toString(miliseconds);  
  String sec=Integer.toString(seconds);  
  String min=Integer.toString(minutes);  
    
  if(mili.length()==1)  
  {  
   mili="0"+mili;  
  }  
  if(sec.length()==1)  
  {  
   sec="0"+sec;  
  }  
  if(min.length()==1)  
  {  
   min="0"+min;  
  }  
    
  g.setFont(timerFont);  
    
  g.drawString(min+" :",20,20);  
  g.drawString(sec+" :",80,20);  
  g.drawString(mili,140,20);  
 }  
   
 public void actionPerformed(ActionEvent event)  
 {  
  miliseconds=miliseconds+1;  
  if(miliseconds==100)  
  {  
   miliseconds=0;  
   seconds=seconds+1;  
  }  
  if(seconds==60)  
  {  
   seconds=0;  
   minutes=minutes+1;  
  }  
  repaint();  
 }  
   
 public static void main(String[]args)  
 {  
  JavaSimpleTimer myTest=new JavaSimpleTimer();  
 }  
}
Output:-