Friday, December 28, 2012

Configuring JavaHelp in Eclipse

As promised in my previous feed, here are the steps to configure JavaHelp for any project in Eclipse.
1.       Download latest version of JavaHelp from http://javahelp.java.net/. As of now, available version for download is javahelp2_0_05.  In windows, extract zipfiles to a path of your choice from  javahelp2_0_05.zip
2.       In Eclipse IDE, Add the jars from lib
      [For instance: C:\javahelp2_0_05\jh2.0\javahelp\lib, if JavaHelp is saved in “C” directory] to your build path, through “Add External JARs
3.       Also specify the path of Helpset in your build path, to access the .hs file.
      [For Example: If path of HelpSet.hs -> “C:\Users\sharan\Desktop\MyJavaHelp\ HelpSet.hs”, then specify classpath as “C:\Users\sharan\Desktop\MyJavaHelp\” under “Add External Class Folder

Note: For Eclipse beginners, “Add External JARs” & “Class Folder” can be accessed by

·         Select your project, press ALT+Enter
·         Properties dialog should be shown.
·         Select Java Build Path option in left pane
·         Move to Libraries tab.

Your comments are always longed-for.


Monday, December 24, 2012

How to add Help menu to a Swing application



A good tester, don’t suppose customers to ramble for needy  material with application in hand. A simple help file should do the needful. Let’s jump to cram about Help file in Swing. JavaHelp is an Open Source help system. To Download JavaHelp, visit http://javahelp.java.net/. I have used Eclipse IDE for this snippet. Configuring JavaHelp in Eclipse and Creating a HelpSet will be covered in future posts.

Prerequisite
·         JavaHelp should be Downloaded.
·         HelpSet(.hs) should be available.
·         Path of HelpSet(.hs) should be configured in your current class path.



import java.awt.event.KeyEvent;

import javax.help.CSH;
import javax.help.HelpBroker;
import javax.help.HelpSet;
import javax.help.HelpSetException;

import javax.swing.Box;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;


public class SwingAppwithhelp
{
                JFrame appFrame;              
                JMenuBar bar;
                JMenu helpMenu ;
                JMenuItem helpMenuItem;
                HelpSet helpset;
        
               
   public static void main(String args[]){
      new SwingAppwithhelp();
   }

   public SwingAppwithhelp()
   {
                    createGUI();     
   }

                public void createGUI(){

                               
                                appFrame = new JFrame("Swing App with Help");
                                bar=new JMenuBar();        
                               
                                //Arrange components on contentPane and set Action Listeners to each component
                                arrangeComponents();
                               
                                appFrame.setSize(240,300);
                                appFrame.setResizable(false);
                                appFrame.setVisible(true);
                                appFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

                }
               
                public void arrangeComponents(){
                               
                               
                      // Move help menu to right side           
                    bar.add(Box.createHorizontalGlue());
                    helpMenu = new JMenu("Help");
                    helpMenu.setMnemonic(KeyEvent.VK_H);
                    helpMenuItem = new JMenuItem("Launch Help");
                    helpMenu.add(helpMenuItem);
                    bar.add(helpMenu);
                    appFrame.setJMenuBar(bar); 

                               
                                ClassLoader loader = null;
                    java.net.URL url = HelpSet.findHelpSet(loader, "HelpSet.hs");
                    System.out.println(url.getPath());
                    try {
         helpset = new HelpSet(loader, url);
       } catch (HelpSetException e) {
         System.err.println("Error loading");
         return;
       }
                    
                    HelpBroker helpbroker = helpset.createHelpBroker();                                                          
                               
                                helpMenuItem.addActionListener(new CSH.DisplayHelpFromSource(helpbroker));

                }   

   }

So this how your app should look like, if you followed the above code.Signing off, with this quote "We can't help everyone, but everyone can help someone"


Click Launch help menu item, to invoke Help file

Wednesday, December 12, 2012

Read any Row in Excel - HSSF POI


This function helps to read any row in excel using HSSF POI. Instantiate this class in your main class and call ReadRow (int, int) function by providing row & column value as parameters.
Anatomy of ReadRow () function
·         Parameters – Two integers, Former denoting row & Later for column to be referenced
·         Return value – String Value of the corresponding cell
Here we go with implementation..
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
public class ExcelRead
{
                                public ExcelRead()           {}
       public String ReadRow(int i ,int j)    {
                FileInputStream  inputStream=null;
                String cellvalue=null;     
                try
                {              //Input File to be read    
                    inputStream = new FileInputStream ("Input.xls");
                }
                catch (FileNotFoundException e)
                {
                System.out.println ("File not found in the specified path.");
                e.printStackTrace ();
                }
               
                try
                {
                HSSFWorkbook workbook = new HSSFWorkbook(inputStream);
              // Get the first sheet on the workbook.
              HSSFSheet sheet = workbook.getSheetAt(0);
              HSSFRow row=sheet.getRow(i);       
             HSSFCell myCell = row.getCell(j);            
             cellvalue=myCell.toString();        
                }
                catch(Exception e)
                {
              System.out.println ("IO Exception");
                e.printStackTrace ();
                }
                                return cellvalue;        
    }
  
    public static void main(String[] args) throws Exception {                            
                ExcelRead ob=new ExcelRead();   
         String value=null;
                value=ob.ReadRow(1,1);
                System.out.println(op);
    }   
}

Monday, November 26, 2012

Importing Images to Excel

Wondering how to Insert/Import images from any location to Excel using POI- HSSF                        [Source: http://poi.apache.org/spreadsheet/index.html], here is one solution. With the assistance of this java file, image formats like .DIB, .EMF, .PNG, .PICT, .JPEG and .WMF can be  to any excel file at the specified cell and sheet. This feed will be of immense comfort for JFreeChart users, to import the generated charts back to excel.

XLInsertImage.java

//set of header files to be included
import java.io.FileInputStream;
import java.io.FileOutputStream;
import org.apache.poi.hssf.usermodel.HSSFPatriarch;
import org.apache.poi.hssf.usermodel.HSSFPicture;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.ClientAnchor;
import org.apache.poi.ss.usermodel.CreationHelper;
import org.apache.poi.util.IOUtils;

public class XlInsertImage {
      
       void write(String filename,String sheetname,int row, int col){
             
        try
        {          
               HSSFWorkbook wb = new HSSFWorkbook(new FileInputStream("C:\\Output.xls"));  
                
                     FileInputStream fis=new FileInputStream(filename);
                     byte[] img_bytes=IOUtils.toByteArray(fis);
                    
                     //specify the format of picture to be imported
                     int index=wb.addPicture(img_bytes,HSSFWorkbook.PICTURE_TYPE_JPEG);
                     fis.close();
                      
            // This will embed the picture from (1,1) cell to (row,col) cell of   
            // excel sheet.     
 
                     CreationHelper helper = wb.getCreationHelper();
                     ClientAnchor anchor = helper.createClientAnchor();
                             
                     anchor.setCol1((short)col);       
                     anchor.setRow1((short)row);
                                 
                     //sepecify the sheet name to copy the picture
                     HSSFSheet sheet=wb.getSheet(sheetname);
                     HSSFPatriarch patriarch=sheet.createDrawingPatriarch();
                     HSSFPicture pict= patriarch.createPicture(anchor,index);     
                     pict.resize();
                    
                     //"Writing data to the xls file";
                     FileOutputStream fileOut = null;   
                     fileOut = new FileOutputStream("C:\\Output.xls");  
                     wb.write(fileOut);   
                     fileOut.close();

        }
        catch(Exception e)
        {
              e.printStackTrace();
        }
    } 
        public static void main(String args[])
              
            {
                new XlInsertImage();
            }
}


Syntax for calling XlInsertImage function in another class


public class MasterClass {

       public static void main(String args[]) throws Exception
   
    {
       
        
         XlInsertImage img= new XlInsertImage();      
        //Embeds image at row-15 col-0 in sheet name passed as argument
        img.write("C:\\chart.jpg",”sheetname”,15,0);
}