Saturday, 8 June 2013

Talha First Program of Java Swing

Talha a very intelligent student who makes the first program in Java when he started the Java Swing ,


I hope he will happy to see this shared code ...
package talha;
import java.awt.*;
/**
 *
 * @author Saeed
 */
public class Talha {
Frame f;
Button bok,bcancel;
    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        // TODO code application logic here
        new Talha();
       
    }
    Talha(){
    f=new Frame("My Talha Frame");
    bok=new Button("Ok");
    bcancel=new Button("Cancel");
    f.setLayout(null);
    bok.setBounds(30, 70, 120, 40);
    bcancel.setBounds(200, 70, 120, 40);
    bok.setBackground(new Color(194,78,194));
    f.add(bok);
    f.add(bcancel);
    f.setVisible(true);
    f.setSize(400,400);
    }
}

To Convert the Units of Measurement of Length

This program , I need to convert the length of wood sheets and veneer paper and was eagerly willing a calculator. In those days the internet was not easily available here . So i decided to make a conversion calculator for the measurements appropriate conversion.

So i share all of my friends here

/*
 * Length.java
 *
 * Created on July 21, 2008, 9:40 AM
 */

package length;

import javax.swing.JOptionPane;
/**
 *
 * @author Saeed Ahmad
 */
public class Length extends javax.swing.JFrame {
 private Object JRootPane;


 /** Creates new form Length */
 public Length() {
 initComponents();
 }

 /** This method is called from within the constructor to
 * initialize the form.
 * WARNING: Do NOT modify this code. The content of this method is
 * always regenerated by the Form Editor.
 */
 @SuppressWarnings("unchecked")
 //
 private void initComponents() {

 cmOption = new javax.swing.JComboBox();
 jLabel1 = new javax.swing.JLabel();
 jLabel2 = new javax.swing.JLabel();
 jLabel3 = new javax.swing.JLabel();
 jLabel4 = new javax.swing.JLabel();
 jLabel5 = new javax.swing.JLabel();
 jLabel6 = new javax.swing.JLabel();
 txtValue = new javax.swing.JTextField();
 jButton1 = new javax.swing.JButton();

 setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
 setTitle("Length Calc Beta Ver.");
 setResizable(false);
 addWindowListener(new java.awt.event.WindowAdapter() {
 public void windowActivated(java.awt.event.WindowEvent evt) {
 formWindowActivated(evt);
 }
 public void windowOpened(java.awt.event.WindowEvent evt) {
 formWindowOpened(evt);
 }
 });

 cmOption.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "MM", "CM", "Inch", "Feet", "Meter", "Kilo Meter" }));
 cmOption.addItemListener(new java.awt.event.ItemListener() {
 public void itemStateChanged(java.awt.event.ItemEvent evt) {
 cmOptionItemStateChanged(evt);
 }
 });
 cmOption.addActionListener(new java.awt.event.ActionListener() {
 public void actionPerformed(java.awt.event.ActionEvent evt) {
 cmOptionActionPerformed(evt);
 }
 });

 jLabel1.setText("Your Choice");

 jLabel2.setText("jLabel2");

 jLabel3.setText("jLabel2");

 jLabel4.setText("jLabel2");

 jLabel5.setText("jLabel2");

 jLabel6.setText("jLabel2");

 txtValue.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
 txtValue.setText("0");
 txtValue.addActionListener(new java.awt.event.ActionListener() {
 public void actionPerformed(java.awt.event.ActionEvent evt) {
 txtValueActionPerformed(evt);
 }
 });

 jButton1.setFont(new java.awt.Font("Tahoma", 1, 10)); // NOI18N
 jButton1.setText("Ok");
 jButton1.addActionListener(new java.awt.event.ActionListener() {
 public void actionPerformed(java.awt.event.ActionEvent evt) {
 jButton1ActionPerformed(evt);
 }
 });

 javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
 getContentPane().setLayout(layout);
 layout.setHorizontalGroup(
 layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
 .addGroup(layout.createSequentialGroup()
 .addContainerGap()
 .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
 .addGroup(layout.createSequentialGroup()
 .addComponent(jLabel6, javax.swing.GroupLayout.DEFAULT_SIZE, 156, Short.MAX_VALUE)
 .addGap(42, 42, 42))
 .addGroup(layout.createSequentialGroup()
 .addComponent(jLabel5, javax.swing.GroupLayout.DEFAULT_SIZE, 156, Short.MAX_VALUE)
 .addGap(42, 42, 42))
 .addGroup(layout.createSequentialGroup()
 .addComponent(jLabel4, javax.swing.GroupLayout.DEFAULT_SIZE, 156, Short.MAX_VALUE)
 .addGap(42, 42, 42))
 .addGroup(layout.createSequentialGroup()
 .addComponent(jLabel3, javax.swing.GroupLayout.PREFERRED_SIZE, 155, javax.swing.GroupLayout.PREFERRED_SIZE)
 .addContainerGap())
 .addGroup(layout.createSequentialGroup()
 .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
 .addComponent(jLabel2, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
 .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
 .addComponent(txtValue, javax.swing.GroupLayout.PREFERRED_SIZE, 113, javax.swing.GroupLayout.PREFERRED_SIZE)
 .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
 .addComponent(jButton1, 0, 0, Short.MAX_VALUE))
 .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
 .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 75, javax.swing.GroupLayout.PREFERRED_SIZE)
 .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
 .addComponent(cmOption, 0, 109, Short.MAX_VALUE)))
 .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))))
 );
 layout.setVerticalGroup(
 layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
 .addGroup(layout.createSequentialGroup()
 .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
 .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 18, javax.swing.GroupLayout.PREFERRED_SIZE)
 .addComponent(cmOption))
 .addGap(1, 1, 1)
 .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
 .addComponent(txtValue, javax.swing.GroupLayout.PREFERRED_SIZE, 20, javax.swing.GroupLayout.PREFERRED_SIZE)
 .addComponent(jButton1))
 .addGap(18, 18, 18)
 .addComponent(jLabel2)
 .addGap(15, 15, 15)
 .addComponent(jLabel3)
 .addGap(14, 14, 14)
 .addComponent(jLabel4)
 .addGap(18, 18, 18)
 .addComponent(jLabel5)
 .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
 .addComponent(jLabel6)
 .addContainerGap())
 );

 pack();
 }//

private void cmOptionActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
}

private void formWindowActivated(java.awt.event.WindowEvent evt) {



}

private void cmOptionItemStateChanged(java.awt.event.ItemEvent evt) {
 Double value=(Double) Double.parseDouble(txtValue.getText());
 String x= (String) cmOption.getSelectedItem();
 int result= x.compareTo("MM");
 if (result==0){
 jLabel2.setText("CM: "+value/10);
 jLabel3.setText("Inch: "+value/25.4);
 jLabel4.setText("Feet: "+value/2.54*1/12.0);
 jLabel5.setText("Meter: "+value/1000);
 jLabel6.setText("Kilo Meter: "+value/1000000);
 }
 result= x.compareTo("CM");
 if (result==0){
 jLabel2.setText("MM: "+value*10.0);
 jLabel3.setText("Inch: "+value/2.54);
 jLabel4.setText("Feet: "+value/2.54*1/12);
 jLabel5.setText("Meter: "+value/100);
 jLabel6.setText("Kilo Meter: "+value/100000);
 }
 result= x.compareTo("Inch");
 if (result==0){
 jLabel2.setText("MM: "+value*25.4);
 jLabel3.setText("CM: "+value*2.54);
 jLabel4.setText("Feet: "+value/12.0);
 jLabel5.setText("Meter: "+value*0.0254);
 jLabel6.setText("Kilo Meter: "+value*0.0000254);
 }
 result= x.compareTo("Feet");
 if (result==0){
 jLabel2.setText("MM: "+value*304.8);
 jLabel3.setText("CM: "+value*30.48);
 jLabel4.setText("Inch: "+value*12.0);
 jLabel5.setText("Meter: "+value*0.3048);
 jLabel6.setText("Kilo Meter: "+value*0.0003048);
 }
result= x.compareTo("Meter");
 if (result==0){
 jLabel2.setText("MM: "+value*1000.0);
 jLabel3.setText("CM: "+value*100.0);
 jLabel4.setText("Inch: "+value*39.3700788);
 jLabel5.setText("Feet: "+value*3.2808399);
 jLabel6.setText("Kilo Meter: "+value/1000.0);
 }
result= x.compareTo("Kilo Meter");
 if (result==0){
 jLabel2.setText("MM: "+value/1000000.0);
 jLabel3.setText("CM: "+value/100000.0);
 jLabel4.setText("Inch: "+value*3280.8399);
 jLabel5.setText("Feet: "+value*273.403325);
 jLabel6.setText("Meter: "+value/1000.0);
 }
}

private void txtValueActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
}

private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
 Double value=(Double) Double.parseDouble(txtValue.getText());
 String x= (String) cmOption.getSelectedItem();
 int result= x.compareTo("MM");
 if (result==0){
 jLabel2.setText("CM: "+value/10);
 jLabel3.setText("Inch: "+value/25.4);
 jLabel4.setText("Feet: "+value/2.54*1/12.0);
 jLabel5.setText("Meter: "+value/1000);
 jLabel6.setText("Kilo Meter: "+value/1000000);
 }
 result= x.compareTo("CM");
 if (result==0){
 jLabel2.setText("MM: "+value*10.0);
 jLabel3.setText("Inch: "+value/2.54);
 jLabel4.setText("Feet: "+value/2.54*1/12);
 jLabel5.setText("Meter: "+value/100);
 jLabel6.setText("Kilo Meter: "+value/100000);
 }
 result= x.compareTo("Inch");
 if (result==0){
 jLabel2.setText("MM: "+value*25.4);
 jLabel3.setText("CM: "+value*2.54);
 jLabel4.setText("Feet: "+value/12.0);
 jLabel5.setText("Meter: "+value*0.0254);
 jLabel6.setText("Kilo Meter: "+value*0.0000254);
 }
 result= x.compareTo("Feet");
 if (result==0){
 jLabel2.setText("MM: "+value*304.8);
 jLabel3.setText("CM: "+value*30.48);
 jLabel4.setText("Inch: "+value*12.0);
 jLabel5.setText("Meter: "+value*0.3048);
 jLabel6.setText("Kilo Meter: "+value*0.0003048);
 }
result= x.compareTo("Meter");
 if (result==0){
 jLabel2.setText("MM: "+value*1000.0);
 jLabel3.setText("CM: "+value*100.0);
 jLabel4.setText("Inch: "+value*39.3700788);
 jLabel5.setText("Feet: "+value*3.2808399);
 jLabel6.setText("Kilo Meter: "+value/1000.0);
 }
result= x.compareTo("Kilo Meter");
 if (result==0){
 jLabel2.setText("MM: "+value/1000000.0);
 jLabel3.setText("CM: "+value/100000.0);
 jLabel4.setText("Inch: "+value*3280.8399);
 jLabel5.setText("Feet: "+value*273.403325);
 jLabel6.setText("Meter: "+value/1000.0);
 }
}

private void formWindowOpened(java.awt.event.WindowEvent evt) {
 setLocationRelativeTo(null);
 jLabel2.setText("CM:");
 jLabel3.setText("Inches:");
 jLabel4.setText("Foot:");
 jLabel5.setText("Meter:");
 jLabel6.setText("Kilo Meter:");
 JOptionPane.showMessageDialog(null,"Length Calculator Beta ver \n Designed By: \n Mr. Saeed Ahmad \n Cell#+92-301-7640931 \n Email: mbit.dbms@gmail.com", "Length Calculator", JOptionPane.INFORMATION_MESSAGE);
}

 /**
 * @param args the command line arguments
 */
 public static void main(String args[]) {
 java.awt.EventQueue.invokeLater(new Runnable() {
     @Override
 public void run() {
 new Length().setVisible(true);
 }
 });
 }

 // Variables declaration - do not modify
 private javax.swing.JComboBox cmOption;
 private javax.swing.JButton jButton1;
 private javax.swing.JLabel jLabel1;
 private javax.swing.JLabel jLabel2;
 private javax.swing.JLabel jLabel3;
 private javax.swing.JLabel jLabel4;
 private javax.swing.JLabel jLabel5;
 private javax.swing.JLabel jLabel6;
 private javax.swing.JTextField txtValue;
 // End of variables declaration

}
 

compareTo Method Demo Program

This is program to explain the method of class String and StringBuffer in java.lang Package
/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package filedemo;

/**
 *
 * @author Saeed
 */
public class StringDemo {
    public static void main(String args[]){
   
      String s1="saeed";
      String s2="saeed";
      System.out.println(s2.compareTo(s1));
     
   }
}

count Space in a text file

Just have a counter variable initialize with '0'
Then start to read the file where the character is space increase the value of counter variable ...

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package filedemo;

/**
 *
 * @author Saeed
 */
import java .io.*;

public class ReadSpaces {
    public static void main(String args[])throws IOException {
        char ch;
            
                char space=' ';
                int count = 0;
        BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
                System.out.println("Enter the File Path Only");
        String path =br.readLine();
        String file_name = br.readLine();
        File f = new File(path+file_name);
        System.out.println("Press zero to end");   
        try (FileWriter fw = new FileWriter(f)) { // File Writer to Write Data inside File f
            do{
             ch=(char)br.read(); // to read data character by character
             fw.write(ch);//get data from console and write in FileWriter(File f)
             if (ch==space )
                     count++;
             }while(ch!='q');
           
    
        }catch(Exception e){
    System.err.println("Error: " + e.getMessage());    }
                System.out.println("You Enter the Spaces as "+count);
    }
}

Read File Character By Character

To read the file character by character ,

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package filedemo;

import java.io.*;
/**
 *
 * @author Saeed
 */
public class FileDemo {
    public static void main(String args[]) throws IOException{
       char ch;
        BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
        // To get The Data from the Console
       
        File f=new File("talha.txt"); // File to Create
        try (FileWriter fw = new FileWriter(f)) { // File Writer to Write Data inside File f
            do{
             ch=(char)br.read(); // to read data character by character
             fw.write(ch);//get data from console and write in FileWriter(File f)
             }while(ch!='q');
           
        }
          
           int i;
           FileInputStream fin=new FileInputStream("talha.txt"); // to Open Stream in Read Mode
           do{
           i=fin.read();  // get the bytes and assign to integer
           if (i!=-1) System.out.write((char)i); // if not end of file convert in character and print
           }while(i!=-1);
                     
    }
}

Tuesday, 4 June 2013

First Program Using AWT

/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package talha;
import java.awt.*;
/**
*
* @author Saeed
*/
public class Talha {
Frame f;
Button bok,bcancel;
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
// TODO code application logic here
new Talha();

}
Talha(){
f=new Frame("My Talha Frame");
bok=new Button("Ok");
bcancel=new Button("Cancel");
f.setLayout(null);
bok.setBounds(30, 70, 120, 40);
bcancel.setBounds(200, 70, 120, 40);
bok.setBackground(new Color(194,78,194));
f.add(bok);
f.add(bcancel);
f.setVisible(true);
f.setSize(400,400);
}
}

Reading Console Input at Exec time

/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package demo;

/**
*
* @author saeed
*/
public class Demo {

/**
* @param args the command line arguments
*/
public static void main(String[] args) {
// TODO code application logic here
for(int i=0;i<args.length;i++)
System.out.println(args[i]);

}
}