Have you wondered how to show a panel(form) as a dialog in java applications as done in .NET applications by using the form.showDialog() method?  For example, when a user selects “Add New” from a Customer combobox, the customer panel shows as a dialog for a new entry before the user can continue. If you have been searching for this, here is your stop.

We will be making use of one JDialog, and your FORMS(JPanels in this sense).

To implement this add a JDialog to your application. Declare a variable contentPane and add the constructor like the one specified below.

private Container contentPane = null;

public popupsjdialog(java.awt.Frame parent, JPanel panel, boolean modal) {
//super(parent, modal);
contentPane = this.getContentPane();
this.setSize(panel.getSize().width + 30, panel.getSize().width + 50);
panel.setLocation(10, 30);


Your are now done. Now to show any JPanel as a dialog add the following code to your event.

helpdetailpanel hd = new helpdetailpanel(helpContent);
hd.setSize(700, 700);
popupsjdialog d = new popupsjdialog(null, hd, true);

Helpdetailpanel is a Jpanel (form) you want to show as a dialog and the popupsjdialog is an instance of the Jdialog we created its constructot above.

Cheers !!!!!!!!!!!!!!!!!


Have you wondered about how to read excel files(.xls,xlsx), in Java Applications without using any API, then here is your stop. With this you can read excel 2003 and also 2007.We will be going through simple steps in reading the content of excel files and populating it into a Jtable. We will be working in the Microsoft Windows environment.

First, you have to create a datasource pointing to where your excel file is located. To do this, Go to Control panel ——— >   Administrative Tools ———- > Data Sources (ODBC). Below is an interface you are likely to see.

datasource face

Select the “User DSN” tab and click on the add button and select the driver “Microsoft Excel Driver (.xls,xlsx)” . You will be presented with the interface below, enter the name of the data source (Note, this is the name you will reference from you Java Application) and click on the “Select Workbook” button to select the excel file you want to read. You are now done with setting up the datasource.

choose driverselect workbook

Let’s now go to our Jave IDE (Netbeans) and do some small coding.  We will be using the JDBC-ODBC driver for reading our data. You don’t need to add the driver to your application as Windows OS comes with the Driver already installed.

We are creating a simple application where the user enters the datasource name and the sheet name, then on click of a button the data if populated into a JTable


We do this by creating a method

private void GetData(String excelDSN, String sheetname) {

        Connection connection = null;

        Statement st;

        ResultSet rsltSet;

        ResultSetMetaData rsltSetMetaData;

        String excelURL = "jdbc:odbc:" + excelDSN;

        String query = "Select * from [" + sheetname + "$]";


        try {


            connection = DriverManager.getConnection(excelURL);

            //System.out.println("ReadExcel connection = " + connection);

            st = connection.createStatement();

            //System.out.println("ReadExcel connection.createStatement() = " + st);

            rsltSet = st.executeQuery(query);

            //System.out.println("ReadExcel rsltSet = " + rsltSet);

            rsltSetMetaData = rsltSet.getMetaData();


          //creating a datamodel for our JTable

            DefaultTableModel aModel = new DefaultTableModel() {


                //setting the JTable uneditalble

                public boolean isCellEditable(int row, int column) {

                    return false;



            //adding the excel column names to the JTable

            Object[] tableColumnNames = new Object[rsltSetMetaData.getColumnCount()];

            for (int i = 1; i <= rsltSetMetaData.getColumnCount(); i++) {

                tableColumnNames[i-1] = rsltSetMetaData.getColumnName(i);



            //adding the rows to the JTable

            Object[] objects = new Object[rsltSetMetaData.getColumnCount()];

            while ( {

                for (int i = 1; i <= rsltSetMetaData.getColumnCount(); i++) {

                    objects[i-1] = rsltSet.getObject(i);




            // setting the table model of the jtable


        } catch (Exception ex) {

            JOptionPane.showMessageDialog(null, ex.toString() );



Note , In reading records from excel, the index starts from one not zero(0). Once you starting getting this exception “[Microsoft][ODBC Driver Manager] Invalid descriptor index.” Get the index , its 1 not zero

Sample interface of the Application

the app


 This is a tutorial on how to create charts (Pie and bar Charts) in Jasper Reports using  a Java Object ( with fields, cname and mark)  as a datasoure

We will be using ireport 3.0 and Netbeans 6.1  as the development environment. Make sure to add the jasper report libraries to your projects.

Design your report by creating the necessary fields (cname, mark) in the ireport.  Make sure the names of the field are the same as they appear in the entity class (the datasource) including capitalization

Drag a chart (using the chart tool on the tool bar) onto your report (make sure to place a bar chart at the summary band on the report designer, a pie chart can be placed in the pageHeader band). For a bar chart , below is a picture showing how to set the properties (the series expression uses the field that should be on the horizontal axis, the category expression is a field to group the fields on the horizontal axis, eg if subjects are on the horizontal axis, but you want to group them into 1st term , 2nd term, then a field “term”  will be used as the category expression. The value expression will be the field to be displayed vertically and the label expression will be the label names for your bars)








 With a pie Chart, for example if you are to create a pie chart with these values (maths:30, science:20), the key expression is the name field and the value expression been the values field. Below is a picture showing this.

 Setting pie chart properties

Then you go to netbeans to do some small coding.

Create a class with field cname and mark, where cname as of type sting and mark is double

Write the code below under an actionevent

    Map<String, Object> param = new HashMap<String, Object>();

    List<CourseMarks> clist = new ArrayList<CourseMarks>();

 //CourseMArks  is the class with the two files   

   CourseMarks c1 = new CourseMarks();





    c1 = new CourseMarks();

    c1.setCname(“Social  Studies”);




    c1 = new CourseMarks();





    c1 = new CourseMarks();





    c1 = new CourseMarks();




 generateReports(“charting”, clist, param);


Below is the detail of the generateReports Method

private void generateReports(String name, Collection data, Map param) {

        try {

            String source = “C:/sabonay/jasperreports/” + name + “.jrxml”;

            if (new File(source).exists() == false) {

                xputils.showMessage(“Please go to setting and Choose report Source”);



            JRBeanCollectionDataSource jrbc = new JRBeanCollectionDataSource(data);

            JasperReport jasperReport = JasperCompileManager.compileReport(source);

            JasperPrint jasperPrint = JasperFillManager.fillReport(jasperReport, param, jrbc);

            JasperViewer.viewReport(jasperPrint, false);

        } catch (Exception e) {


            System.out.println(“reports Error  ” + e.toString());



Below is a sample report when the app is run

After running the App
After running the App


If you want to generate jasper reports in your JAVA applications without using a data source, then here is your stop. For example printing receipts directly from user inputs, where all you need are parameters. Also this piece talks about how to insert images from the file system ( hard drives pen dirves) into jasper reports at runtime.It assumed you know how to generate reports with jasper reports. This help was created using Netbeans 6.7 and iReport 3.5.2

Relax and Enjoy!!!!!!!!!!!!!!!

In this our example, we are creating a report that requires a name, hometown and a picture from the user at runtime to be displayed on our report. Design your report by creating the parameters; name as string, hometown as string and picture as object. With the name and hometown parameters, you can drag them onto the report interface, but for the image, drag  the image item from the tool bar and bind it to the picture parameter. Below is a picture of the report

the report designed in iReport

After this, go to Netbeans and do some simple coding. Below are the methods needed to run your application. Make sure to add the Jasper jar files to your application

private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {

// a method to generate the report, by assigning the prameter to their value, and calling te generate report method

Map<String, Object> param = new HashMap<String, Object>();

param.put("name", this.tfName.getText());

param.put("hometown", this.tfHometown.getText());

param.put("photo", photo);

generateReports("report1", ls, param);


private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {

// a mehod t browse for an image to be inserted into te report

//photo is a variable of type image

fc.setFileFilter(new FileNameExtensionFilter("Images", "jpg", "gif", "bmp"));

if (fc.showOpenDialog(this) == JFileChooser.APPROVE_OPTION) {


ImageIcon icon = new ImageIcon(fc.getSelectedFile().getAbsolutePath());

icon = new ImageIcon(icon.getImage().getScaledInstance(350, 350, Image.SCALE_DEFAULT));

photo = icon.getImage();



private void generateReports(String name, Collection data, Map param) {

//a method to generate th report

try {

String source = "C:/sabonay/jasperreports/" + name + ".jrxml";

if (new File(source).exists() == false) {

xputils.showMessage("Please go report file does not exst");



JasperReport jasperReport = JasperCompileManager.compileReport(source);

JasperPrint jasperPrint = JasperFillManager.fillReport(jasperReport, param, new    JREmptyDataSource());

JasperViewer.viewReport(jasperPrint, false);

} catch (Exception e) {


System.out.println("reports Error  " + e.toString());



Below are pictures after running theappliation

using entering parameter values
Sample report generated