Programs1

Display The records with navigation using JQuery
download jquery-datatables.js 
download    jquery-min.js

home.html
----------

<!DOCTYPE HTML>
<html>
<head>
<meta charset="utf-8">
<title>Tracking System</title>
<link href="style.css" rel="stylesheet" type="text/css">

<script type="text/javascript" src="js/jquery-min.js"></script>
<script type="text/javascript" src="js/jquery-datatables.js"></script>

 <script type="text/javascript" ></script>
  <script>
$(document).ready(function() {
   $('table.display').dataTable( {

"bJQueryUI": true,
"sDom": '<"H"Tfr>t<"F"ip>',
"bFilter": false, // to hide search bare
"sPaginationType": "full_numbers",
"bPaginate": true,
"iDisplayLength": 10

} );
} );
</script>

<div id="main">
  <div id="banner">
    <h1>Tracking Gadget</h1>
  </div>
  <div id="content">
 
    <!-- scroll -->

          <div class="cleaner_h30"></div>
          <div id="searchresult">
            <table id="search" cellpadding="0" cellspacing="0" border="0"  class="display">
              <thead>
                <tr>
                  <th>id</th>
                  <th>Vehicle No</th>
                  <th>Date</th>
                  <th>Status</th>
                  <th>Actions</th>
                </tr>
              </thead>
           
              <tbody>
                <tr class="odd gradeX">
                  <td><a href="#">12345</a></td>
                  <td>AP 20 0911</td>
                  <td>22-03-2012</td>
                  <td>Status</td>
                  <td>view | edit</td>
               
                </tr>
                <tr class="odd gradeX">
                  <td><a href="#">54678</a></td>
                  <td>AP 20 0911</td>
                  <td>22-03-2015 </td>
                  <td>My Status</td>
                  <td>View | edit</td>
                </tr>
                 <tr class="odd gradeX">
                  <td><a href="#">12345</a></td>
                  <td>AP 20 0911</td>
                  <td>22-03-2012</td>
                  <td>Status</td>
                  <td>view | edit</td>
               
                </tr>
                <tr class="odd gradeX">
                  <td><a href="#">54678</a></td>
                  <td>AP 20 0911</td>
                  <td>22-03-2015 </td>
                  <td>My Status</td>
                  <td>View | edit</td>
                </tr>
                 <tr class="odd gradeX">
                  <td><a href="#">12345</a></td>
                  <td>AP 20 0911</td>
                  <td>22-03-2012</td>
                  <td>Status</td>
                  <td>view | edit</td>
               
                </tr>
                <tr class="odd gradeX">
                  <td><a href="#">54678</a></td>
                  <td>AP 20 0911</td>
                  <td>22-03-2015 </td>
                  <td>My Status</td>
                  <td>View | edit</td>
                </tr>
                 <tr class="odd gradeX">
                  <td><a href="#">12345</a></td>
                  <td>AP 20 0911</td>
                  <td>22-03-2012</td>
                  <td>Status</td>
                  <td>view | edit</td>
               
                </tr>
                <tr class="odd gradeX">
                  <td><a href="#">54678</a></td>
                  <td>AP 20 0911</td>
                  <td>22-03-2015 </td>
                  <td>My Status</td>
                  <td>View | edit</td>
                </tr>
                 <tr class="odd gradeX">
                  <td><a href="#">12345</a></td>
                  <td>AP 20 0911</td>
                  <td>22-03-2012</td>
                  <td>Status</td>
                  <td>view | edit</td>
               
                </tr>
                <tr class="odd gradeX">
                  <td><a href="#">54678</a></td>
                  <td>AP 20 0911</td>
                  <td>22-03-2015 </td>
                  <td>My Status</td>
                  <td>View | edit</td>
                </tr>
                 <tr class="odd gradeX">
                  <td><a href="#">12345</a></td>
                  <td>AP 20 0911</td>
                  <td>22-03-2012</td>
                  <td>Status</td>
                  <td>view | edit</td>
               
                </tr>
                <tr class="odd gradeX">
                  <td><a href="#">54678</a></td>
                  <td>AP 20 0911</td>
                  <td>22-03-2015 </td>
                  <td>My Status</td>
                  <td>View | edit</td>
                </tr>
              <tbody>
            </table>
          </div>
        </div>
   
 
    <div id="footer"> Copyright © 2012 <a href="#">Asmansoft Pvt. Ltd.</a> | <a href="http://www.asmansoft.com" target="_parent">www.asmansoft.com</a> </div>
    <!-- end of footer -->
 
  </div>
  <!-- end of main -->
</div>
</body>
</html>










Send the one textfield value as hidden parameter value to action classes
<s:form>
<td><s:textfield name="gadgetStatus.gdstatusId"></s:textfield></td>

//This is displaying some value like 100. this value goes to our action classes through below using hidden field. .
    <s:hidden name="statusId" value="%{gadgetStatus.gdstatusId}"></s:hidden>
  <td><s:submit value="update" action="updategadgetvehicle" method="upadateGadgetVehicle"></s:submit></td>
</s:form>

In ur Action classes just created setter and getter method s for varaible "statusId".


How to convert number to word in java, Number to Word

Just use String().subString() method and you can develop java code to convert number to word. Like 12345678 to
one Crore twenty three Lakh forty five thousand six hundred seventy eight.


import java.text.DecimalFormat;

public class NumberToWord {
public static void main(String[] args) {
System.out.println(convert(12345678));
}
private static final String[] tensNames = { "", " ten", " twenty", " thirty", " forty", " fifty", " sixty", " seventy", " eighty", " ninety" };

private static final String[] numNames = { "", " one", " two", " three", " four", " five", " six", " seven", " eight", " nine", " ten", " eleven", " twelve", " thirteen", " fourteen", " fifteen", " sixteen", " seventeen", " eighteen", " nineteen" };

private static String convertLessThanOneThousand(int number) {
String soFar;
if (number % 100 < 20){ soFar = numNames[number % 100]; number /= 100; } else { soFar = numNames[number % 10]; number /= 10;
soFar = tensNames[number % 10] + soFar; number /= 10; } if (number == 0) return soFar; return numNames[number] + " hundred" + soFar; }
public static String convert(long number) {
// 0 to 999 999 999 999
if (number == 0) { return "zero"; }
String snumber = Long.toString(number);
// pad with "0"
String mask = "000000000000"; DecimalFormat df = new DecimalFormat(mask); snumber = df.format(number);
int hyndredCrore = Integer.parseInt(snumber.substring(3,5));
int hundredLakh = Integer.parseInt(snumber.substring(5,7));
int hundredThousands = Integer.parseInt(snumber.substring(7,9));
int thousands = Integer.parseInt(snumber.substring(9,12));
String tradBillions;
switch (hyndredCrore) { case 0: tradBillions = ""; break; case 1 : tradBillions = convertLessThanOneThousand(hyndredCrore) + " Crore "; break; default : tradBillions = convertLessThanOneThousand(hyndredCrore) + " Crore "; }

String result = tradBillions;
String tradMillions;
switch (hundredLakh) { case 0: tradMillions = ""; break; case 1 : tradMillions = convertLessThanOneThousand(hundredLakh) + " Lakh "; break; default : tradMillions = convertLessThanOneThousand(hundredLakh) + " Lakh "; }
result = result + tradMillions;
String tradHundredThousands;

switch (hundredThousands) { case 0: tradHundredThousands = ""; break; case 1 : tradHundredThousands = "one thousand "; break; default : tradHundredThousands = convertLessThanOneThousand(hundredThousands) + " thousand "; }
result = result + tradHundredThousands;

String tradThousand;
tradThousand = convertLessThanOneThousand(thousands);
result = result + tradThousand;return result.replaceAll("^\\s+", "").replaceAll("file://b//s%7B2,%7D//b", " "); }
}

How To UnZip the File in java
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;

public class MakeUnZip {
public static void main(String argv[]) {
String zipFileName = "src\\myZip.zip";
unZip(zipFileName);
}

public static void unZip(String zipFileName) {
int BUFFER = 2048;
try {
BufferedOutputStream dest = null;
FileInputStream fileInputStream = new FileInputStream(zipFileName);
ZipInputStream zipInputStream = new ZipInputStream(new BufferedInputStream(fileInputStream));
ZipEntry zipEntry;
int count=0;
while ((zipEntry = zipInputStream.getNextEntry()) != null) {
System.out.println("Extracting File Name :: " + zipEntry);
count++; int length;
byte data[] = new byte[BUFFER];
FileOutputStream fileOutputStream = new FileOutputStream(zipEntry.getName());
dest = new BufferedOutputStream(fileOutputStream, BUFFER);
while ((length = zipInputStream.read(data, 0, BUFFER)) != -1) {
dest.write(data, 0, length);
}

dest.flush();
dest.close();
}

zipInputStream.close();
System.out.println("Total "+count+ " Files Unziped Successfully ");
} catch (Exception e) { e.printStackTrace(); }
}
}
Get the content from zip file
import java.io.File;
import java.io.IOException;
import java.util.Enumeration;
import java.util.zip.ZipFile;

public class ZipContents {
public static void main(String[] args) {
File zipFileName = new File("src\\myZip.zip");
getContentsInZip(zipFileName);
}

public static void getContentsInZip(File zipFileName){
try{ ZipFile zipFile = new ZipFile(zipFileName);
Enumeration em = zipFile.entries();
for (Enumeration enumer = zipFile.entries(); enumer.hasMoreElements();) {
System.out.println(enumer.nextElement());
}
}catch(IOException e){ e.printStackTrace(); }
}
} 

How to make Zip file using Java, Creating a ZIP file in Java

To run this tutorial
1. Create one folder src
2. Put test1.txt and test2.txt in src folder.
3. After run this code, you will get myZip.zip file in src folder.

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;

public class MakeZip {
public static void main(String[] args) {
String[] filesToZip = {"src\\test1.txt","src\\test2.txt"};
//String[] filesToZip = {"src\\test1.txt"};
String ZipedFileName = "src\\myZip.zip";
zipConversion(filesToZip, ZipedFileName);
}

public static void zipConversion(String[] files, String ZipedFileName){
byte[] buffer = new byte[1024];
try{
FileOutputStream outputFile = new FileOutputStream(ZipedFileName);
ZipOutputStream zipFile = new ZipOutputStream(outputFile);
for(int i=0;i<files.length;i++){
FileInputStream inFile = new FileInputStream(files[i]);
zipFile.putNextEntry(new ZipEntry(files[i]));
int length;
while ((length = inFile.read(buffer)) > 0) {
zipFile.write(buffer, 0, length); }

zipFile.closeEntry();
inFile.close();
}

zipFile.close();
System.out.println("Files Ziped Successfully");
}catch(IOException e){ e.printStackTrace(); }
}
} 

XML to PDF tutorial

To transfer xml information to pdf (create pdf file from xml file)
FOP : Formatting Objects Processor is an open source Java API that can convert your XML data into reports in PDF format.
1. Download fop from apache binary version
2. Unzip file and copy any drive say c:\fop-0.95-bin and add this path to your system environment path.
3. student.xml (c:\pdf\student.xml)
4. student.xsl (c:\pdf\student.xsl)
5. use command (c:\pdf> fop -xml student.xml -xsl student.xsl -pdf student.pdf)
You will get one student.pdf file. Very easy

Student.xml 

<?xml version="1.0" encoding="ISO-8859-1"?>
<StudentRecord>
<Student>
<name>Manish</name>
<roll>130</roll>
<country>India</country>
<company>Infosys</company>
</Student>
<Student>
<name>Pramod Kumar</name>
<roll>120</roll>
<country>India</country>
<company>Patni Computers</company>
</Student>
<Student>
<name>Binod Kumar Suman</name>
<roll>110</roll>
<country>India</country>
<company>Satyam Computers</company>
</Student>
</StudentRecord>

student.xsl
<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:fo="http://www.w3.org/1999/XSL/Format%22&gt;
<xsl:output method="xml" indent="yes"/>
<xsl:template match="/">

<fo:root xmlns:fo="http://www.w3.org/1999/XSL/Format%22&gt;
<fo:simple-page-master master-name="first"
page-height="29.7cm"
page-width="21cm"
margin-top="1cm"
margin-bottom="2cm"
margin-left="2.5cm"
margin-right="2.5cm">

<fo:region-body margin-top="3cm"/>
<fo:region-before extent="3cm"/>
<fo:region-after extent="1.5cm"/>
</fo:simple-page-master>
</fo:layout-master-set>

<fo:page-sequence master-reference="first">
<fo:static-content flow-name="xsl-region-before">
<fo:block line-height="14pt" font-size="10pt"
text-align="end">By Binod</fo:block>
</fo:static-content>
<fo:static-content flow-name="xsl-region-after">
<fo:block line-height="14pt" font-size="10pt"
text-align="end">Page <fo:page-number/>
</fo:block>
</fo:static-content>

<fo:flow flow-name="xsl-region-body">
<fo:block font-size="18pt"
font-family="sans-serif"
line-height="24pt"
space-after.optimum="15pt"
background-color="blue"
color="white"
text-align="center"
padding-top="3pt">
STUDENT EMPLYOEE REPORT
</fo:block>

<fo:block font-size="16pt"
font-family="sans-serif"
space-after.optimum="15pt"
text-align="center">
ADMS JAVA AND CS DEPARTMENT
</fo:block>

<fo:block text-align="start">This is based on the performance. </fo:block>

<fo:table table-layout="fixed" width="100%" border-collapse="separate">
<fo:table-column column-width="50mm"/>
<fo:table-column column-width="50mm"/>
<fo:table-column column-width="50mm"/>
<fo:table-body>
<fo:table-row color="red">
<fo:table-cell><fo:block>NAME</fo:block></fo:table-cell> <fo:table-cell><fo:block>ROLL</fo:block></fo:table-cell> <fo:table-cell><fo:block>COMPANY</fo:block></fo:table-cell> </fo:table-row>
<fo:table-row color="red">
<fo:table-cell><fo:block>------------------</fo:block></fo:table-cell> <fo:table-cell><fo:block>------------------</fo:block></fo:table-cell> <fo:table-cell><fo:block>------------------</fo:block></fo:table-cell> </fo:table-row>

<xsl:for-each select="StudentRecord/Student">
<xsl:sort select="name"/>
<fo:table-row>
<fo:table-cell><fo:block><xsl:value-of select="name"/></fo:block></fo:table-cell>
<fo:table-cell><fo:block><xsl:value-of select="roll" /></fo:block></fo:table-cell>
<fo:table-cell><fo:block><xsl:value-of select="company" />
</fo:block>
</fo:table-cell>
</fo:table-row>
</xsl:for-each>
</fo:table-body>
</fo:table>
</fo:flow>
</fo:page-sequence>
</fo:root>
</xsl:template>
</xsl:stylesheet>


Bubble sort algorithm




import java.io.*;


/**
 * This class demonstrates the bubble sort.
 * @author SANTHOSH REDDY MANDADI
 * @version 1.0
 * @since 04th September 2009
 */
public class BubbleSort
{
  public static void main(String[] args) throws Exception
  {
    int values[]=new int[5];
    BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
    System.out.println("Enter "+values.length+" values to sort using bubble sort");
    for(int i=0;i<values.length;i++)
    {
      try{
        values[i]=Integer.parseInt(br.readLine());
      }catch(NumberFormatException e)
      {
        e.printStackTrace();
        values[i]=0;
      }
    }
    bubbleSort(values);
    for(int i=0;i<values.length;i++)
    {
      System.out.print(values[i]+" ");
    }
  }


  /**
   * This method will sort the passed array
   * @author SANTHOSH REDDY MANDADI
   * @since 4-Sep-2009
   * @param an array of the list of values to be sorted
   */
  public static void bubbleSort(int[] values)
  {
    boolean swapped=false;
    do
    {
      //When initializing the each loop assing false to swapped flag
      swapped = false;
      //Looping through the list
      for(int i=0; i<values.length-1; i++)
      {
        //Comparing the adjecent elements
        if(values[i]>values[i+1])
        {
          //Swapping
          int temp=values[i];
          values[i]=values[i+1];
          values[i+1]=temp;
          //Swapped is true
          swapped=true;
        }
      }
    }while (swapped);
  }
}





Notepad program through Java - Swings



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


/**
* Notepad class
* @author SANTHOSH REDDY MANDADI
* @since 20-April-2007
* @version 1.0
*/


public class Notepad extends JFrame implements ActionListener,ItemListener,MenuListener,KeyListener
{
   FileOutputStream fos;
   FileInputStream fis;
   JFileChooser dialog;
   JOptionPane msgbox;
   boolean modified=false;
   String str;
   JMenuBar menubar;
   JMenu file,edit,help;
   JMenuItem newfile,savefile,openfile,exit,cut,copy,paste,about,selall;
   JTextArea disp;
   JScrollPane scrlpane;
   public Notepad()
   {
       disp=new JTextArea();
       scrlpane=new JScrollPane(disp);//,ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED,ScrollPaneConstants.HORIZONTAL_SCROLLBAR_AS_NEEDED);
       menubar=new JMenuBar();
       dialog=new JFileChooser();
       msgbox=new JOptionPane();
       file=new JMenu("File"); file.setMnemonic('F');
       edit=new JMenu("Edit"); edit.setMnemonic('E');
       help=new JMenu("Help"); help.setMnemonic('H');
       newfile=new JMenuItem("New"); newfile.setMnemonic('N');
       openfile=new JMenuItem("Open"); openfile.setMnemonic('O');
       savefile=new JMenuItem("Save"); savefile.setMnemonic('S');
       exit=new JMenuItem("Exit"); exit.setMnemonic('x');
       cut=new JMenuItem("Cut"); cut.setMnemonic('C');
       copy=new JMenuItem("Copy"); copy.setMnemonic('o');
       paste=new JMenuItem("Paste"); paste.setMnemonic('P');
       selall=new JMenuItem("Select All"); selall.setMnemonic('A');
       about=new JMenuItem("About"); about.setMnemonic('A');
       cut.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_X,InputEvent.CTRL_MASK,true));
       copy.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_C,InputEvent.CTRL_MASK,true));
       paste.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_P,InputEvent.CTRL_MASK,true));
       openfile.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_O,InputEvent.CTRL_MASK,true));
       savefile.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_S,InputEvent.CTRL_MASK,true));
       newfile.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_N,InputEvent.CTRL_MASK,true));
       file.add(newfile);
       file.add(openfile);
       file.add(savefile);
       file.addSeparator();
       file.add(exit);
       edit.add(cut);
       edit.add(copy);
       edit.add(paste);
       edit.addSeparator();
       edit.add(selall);
       help.add(about);
       menubar.add(file);
       menubar.add(edit);
       menubar.add(help);
       Container c=getContentPane();
       getRootPane().setJMenuBar(menubar);
       c.add(scrlpane);
       setSize(500,500);
       setDefaultCloseOperation(EXIT_ON_CLOSE);
       setVisible(true);
       setTitle("Untitled - Notepad");
       savefile.addActionListener(this);
       openfile.addActionListener(this);
       exit.addActionListener(this);
       cut.addActionListener(this);
       copy.addActionListener(this);
       paste.addActionListener(this);
       selall.addActionListener(this);
       newfile.addActionListener(this);
       edit.addMenuListener(this);
       disp.addKeyListener(this);
   }
   public void keyPressed(KeyEvent e)
   {
       //System.out.println("Press");
   }
   public void keyReleased(KeyEvent e)
   {
       //System.out.println("Release");
   }
   public void keyTyped(KeyEvent e)
   {
       String str=KeyEvent.getKeyModifiersText(InputEvent.CTRL_MASK);
       if(!str.equals("Ctrl"))
           modified=true;
   }
    public void menuSelected(MenuEvent ml)
   {
       //System.out.println("Sele");
       if(ml.getSource()==edit)
       {
           if(disp.getSelectedText()==null)
           {
               cut.setEnabled(false);
               copy.setEnabled(false);
           }
           else
           {
               cut.setEnabled(true);
               copy.setEnabled(true);
           }
       }
   }
   public void menuDeselected(MenuEvent ml)
   {
       //System.out.println("DeSele");
   }
   public void menuCanceled(MenuEvent ml)
   {
       //System.out.println("Can");
   }
   public void actionPerformed(ActionEvent ae){
       if(ae.getSource()==savefile)
       {
           if(getTitle().equals("Untitled - Notepad"))
               saveFile(0);
           else if(modified)
               saveFile(1);
       }
       else if(ae.getSource()==openfile)
       {
           int x;
           if(modified)
           {
               x=isModified();
               if(x==1)
               {
                   if(getTitle().equals("Untitled - Notepad"))
                       saveFile(0);
                   else
                       saveFile(1);
                   openFile();
               }
               else if(x!=3) openFile();
           }
           else openFile();
       }
       else if(ae.getSource()==exit)
       {
           int x;
           if(modified)
           {
               x=isModified();
               if(x==1)
               {
                   if(getTitle().equals("Untitled - Notepad"))
                       saveFile(0);
                   else
                       saveFile(1);
               }
               else if(x==2) System.exit(0);
           }
           else System.exit(0);
       }
       else if(ae.getSource()==newfile)
       {
           int x;
           if(modified)
           {
               x=isModified();
               if(x==1)
               {
                   if(getTitle().equals("Untitled - Notepad"))
                       saveFile(0);
                   else
                       saveFile(1);
               }
               else if(x!=3){
                   disp.setText("");
                   setTitle("Untitled - Notepad");
                   modified=false;
               }
           }
           else{
               disp.setText("");
               setTitle("Untitled - Notepad");
               modified=false;
           }
       }
       else if(ae.getSource()==cut)
       {
           disp.cut();
       }
       else if(ae.getSource()==copy)
       {
           disp.copy();
       }
       else if(ae.getSource()==paste)
       {
           disp.paste();
       }
       else if(ae.getSource()==selall)
       {
           disp.selectAll();
       }
   }
   public void saveFile(int saveflag)
   {
       File f;
       try
       {
           if(saveflag==0)
           {
               int x=dialog.showSaveDialog(this);
               if(x==0){
                   f=dialog.getSelectedFile();
                   fos=new FileOutputStream(f);
                   setTitle(f.getPath());
                   PrintStream ps=new PrintStream(fos);
                   ps.print(disp.getText());
                   modified=false;
               }
           }
           else
           {
               f=new File(getTitle());
               fos=new FileOutputStream(f);
               setTitle(f.getPath());
               PrintStream ps=new PrintStream(fos);
               ps.print(disp.getText());
               modified=false;
           }
       }catch(Exception e){}
   }
   public void openFile()
   {
       int x=dialog.showOpenDialog(this);
       if(x==0){
       try
       {
           File f=dialog.getSelectedFile();
           fis=new FileInputStream(f);
           setTitle(f.getPath());
           disp.setText("");
           BufferedReader br=new BufferedReader(new InputStreamReader(fis));
           while((str=br.readLine())!=null)
           disp.append(str+"\n");
           disp.replaceRange("",disp.getText().length()-1,disp.getText().length());
           modified=false;
       }catch(Exception e){}
       }
   }
   public int isModified()
   {
       int x=JOptionPane.showConfirmDialog(this, "The text in the "+getTitle()+" has changed\nDo you want to save it", "Notepad", JOptionPane.YES_NO_CANCEL_OPTION,JOptionPane.WARNING_MESSAGE);
       if(x==JOptionPane.YES_OPTION) return 1;
       else if(x==JOptionPane.NO_OPTION) return 2;
       else return 3;
   }
   public void itemStateChanged(ItemEvent ie){}
   public static void main(String[] args)
   {
       new Notepad();
   }
}



Program to show web colors - Java Swings


import java.io.FileInputStream;
import java.awt.*;
import java.util.*;
import javax.swing.*;
import java.awt.event.*;

public class ColorsDemo extends JFrame implements ActionListener
{
 FileInputStream fis;
 Properties prop;
 Color colors[];
 JScrollPane s,s1;
 JPanel p;
 JTextArea ta;
 JButton clabels[];
 boolean backcolor;
 public ColorsDemo()
 {
  try{
   fis=new FileInputStream("props.properties");
   prop=new Properties();
   prop.load(fis);
  }catch(Exception e){}
  backcolor=true;
  p=new JPanel();
  s=new JScrollPane(p);
  p.setLayout(new GridLayout(20,7));
  colors=new Color[140];
  clabels=new JButton[140];
  Enumeration e=prop.propertyNames();
  int i=0;
  while(e.hasMoreElements())
  {
   String propname=(String)e.nextElement();
   String str=prop.getProperty(propname);
   int r=(Integer.decode("#"+str.substring(1,3))).intValue();
   int g=(Integer.decode("#"+str.substring(3,5))).intValue();
   int b=(Integer.decode("#"+str.substring(5,7))).intValue();
   clabels[i]=new JButton("<HTML><CENTER>"+propname+"<BR>"+str+"<BR>Red="+r+", Green="+g+", Blue="+b+"</HTML>");
   colors[i]=new Color(r,g,b);
   clabels[i].setBackground(colors[i]);
   clabels[i].addActionListener(this);
   p.add(clabels[i]);
   //clabels[i].setEditable(false);
   i++;
  }
  Container c=getContentPane();
  ta=new JTextArea("Using the following buttons you are able \n"+
      "to set the background and foreground colors\n"+
      "of this text area.\n Click the buttons,\n"+
      "It will automatically sets the background\n"+
      "and foreground colors.\n Remember one time\n"+
      "background color and another one foreground color\n"+
      "For the first time background color\n"+
      "------------------------------------\n"+
      "Designed and Devloped by\n"+
      "SANTHOSH REDDY MANDADI");
  s1=new JScrollPane(ta);
  ta.setFont(new Font("MS Sans Serif",Font.BOLD,20));
  c.setLayout(new GridLayout(2,1));
  c.add(s1);
  c.add(s);
  setDefaultCloseOperation(EXIT_ON_CLOSE);
  setSize(500,400);
  setTitle("Color Demo - you can able to select suitable colors for webpage");
  setVisible(true);
 }
 public void actionPerformed(ActionEvent ae)
 {
  String str=ae.getActionCommand();
  int x=str.indexOf('#');
  str=str.substring(x,x+8);
  int r=(Integer.decode("#"+str.substring(1,3))).intValue();
  int g=(Integer.decode("#"+str.substring(3,5))).intValue();
  int b=(Integer.decode("#"+str.substring(5,7))).intValue();
  Color c=new Color(r,g,b);
  if(backcolor)
  {
   ta.setBackground(c);
   backcolor=false;
   //System.out.println(ta.getText().lastIndexOf('\n'));
   //System.out.println(ta.getText().length());
   //ta.setSelectionStart(ta.getText().lastIndexOf('\n'));
   //ta.setSelectionEnd(ta.getText().length());
   //ta.select(ta.getSelectionStart(),ta.getSelectionEnd());
   //ta.selectAll();
   //System.out.println(ta.getSelectedText());
   //ta.revalidate();
  }
  else
  {
   ta.setForeground(c);
   backcolor=true;
  }
 }
 public static void main(String[] args) throws Exception
 {
  new ColorsDemo();
 }
}

props.perporites
-----------------
ALICEBLUE=#F0F8FF
ANTIQUEWHITE=#FAEBD7
AQUA=#00FFFF
AQUAMARINE=#7FFFD4
AZURE=#F0FFFF
BEIGE=#F5F5DC
BISQUE=#FFE4C4
BLACK=#000000
BLANCHEDALMOND=#FFEBCD
BLUE=#0000FF
BLUEVIOLET=#8A2BE2
BROWN=#A52A2A
BURLYWOOD=#DEB887
CADETBLUE=#5F9EA0
CHARTREUSE=#7FFF00
CHOCOLATE=#D2691E
CORAL=#FF7F50
CORNFLOWER=#6495ED
CORNSILK=#FFF8DC
CRIMSON=#D614EC
CYAN=#00FFFF
DARKBLUE=#00008B
DARKCYAN=#008B8B
DARKGOLDENROD=#B8860B
DARKGRAY=#A9A9A9
DARKGREEN=#006400
DARKKHAKI=#BDB76B
DARKMAGENTA=#8B008B
DARKOLIVEGREEN=#556B2F
DARKORANGE=#FF8C00
DARKORCHID=#9932CC
DARKRED=#8B0000
DARKSALMON=#E9967A
DARKSEAGREEN=#8FBC8B
DARKSLATEBLUE=#483D8B
DARKSLATEGRAY=#2F4F4F
DARKTURQUOISE=#00CED1
DARKVIOLET=#9400D3
DEEPPINK=#FF1493
DEEPSKYBLUE=#00BFFF
DIMGRAY=#696969
DODGERBLUE=#1E90FF
FIREBRICK=#B22222
FLORALWHITE=#FFFAF0
FORESTGREEN=#228B22
FUCHIA=#FF00FF
GIANSBORO=#DCDCDC
GHOSTWHITE=#F8F8FF
GOLD=#FFD700
GOLDENTROD=#DAA520
GRAY=#808080
GREEN=#008000
GREENYELLOW=#ADFF2F
HONEYDEW=#F0FFF0
HOTPINK=#FF69B4
INDIANRED=#CD5C5C
INDIGO=#4B0082
IVORY=#FFFFF0
KHAKI=#F0E68C
LAVENDER=#E6E6FA
LAVENDERBLUSH=#FFF0F5
LAWNGREEN=#7CFC00
LEMONCHIFFON=#FFFACD
LIGHTBLUE=#ADD8E6
LIGHTCORAL=#F08080
LIGHTCYAN=#E0FFFF
LIGHTGOLDENRODYELLOW=#FAFAD2
LIGHTGREEN=#90EE90
LIGHTGRAY=#D3D3D3
LIGHTPINK=#FFB6C1
LIGHTSALMON=#FFA074
LIGHTSEAGREEN=#20B2AA
LIGHTSKYBLUE=#87CEFA
LIGHTSLATEGRAY=#778899
LIGHTSTEELBLUE=#B0C4DE
LIGHTYELLOW=#FFFFE0
LIME=#00FF00
LIMEGREEN=#32CD32
LINEN=#FAF0E6
MAGENTA=#FF00FF
MAROON=#800000
MEDIUMAQUAMARINE=#66CDAA
MEDIUMBLUE=#0000CD
MEDUMORCHID=#BA55D3
MEDIUMPURPLE=#9370DB
MEDIUMSEAGREEN=#3CB371
MEDIUMSLATEBLUE=#7B68EE
MEDIUMSPRINGGREEN=#00FA9A
MEDIUMTURQUOISE=#48D1CC
MEDIUMVIOLETRED=#C71585
MIDNIGHTBLUE=#191970
MINTCREAM=#F5FFFA
MISTYROSE=#FFE4E1
MOCCASIN=#FFE4B5
NAVAJOWTHITE=#FFDEAD
NAVY=#000080
OLDLACE=#FDF5E6
OLIVE=#808000
OLIVEDRAB=#6B8E23
ORANGE=#FFA500
ORANGERED=#FF4500
ORCHID=#DA70D6
PALEGOLDENROD=#EEE8AA
PALEGREEN=#98FB98
PALETURQUOISE=#AFEEEE
PALEVIOLETRED=#DB7093
PAPAYAWHIP=#FFEFD5
PEACHPUFF=#FFDAB9
PERU=#CD853F
PINK=#FFC0CB
PLUM=#DDA0DD
POWDERBLUE=#B0E0E6
PURPLE=#800080
RED=#FF0000
ROSYBROWN=#BC8F8F
ROYALBLUE=#4169E1
SEASHELL=#FFF5EE
SALMON=#FA8072
SILVER=#C0C0C0
SEAGREEN=#2E8B57
SLATEBLUE=#6A5ACD
SIENNA=#A0522D
SNOW=#FFFAFA
SKYBLUE=#87CEEB     
STEELBLUE=#4682B4
SLATEGRAY=#708090
TEAL=#008080
SPRINGGREEN=#00FF7F
TOMATO=#FF6347
TAN=#D2B48C
VIOLET=#EE82EE
THISTLE=#D8BFD8
WHITE=#FFFFFF
TURQUOISE=#40E0D0
YELLOW=#FFFF00
WHEAT=#F5DEB3
SADDLEBROWN=#8B4513
WHITESMOKE=#F5F5F5
SANDYBROWN=#F4A460
YELLOWGREEN=#9ACD32

No comments:

Post a Comment