Archive for the ‘Java SE’ Category

Summary row in jTable (Swing/Java)

Monday, August 31st, 2009

Summary row is used to present a total sum at the bottom of the jTable.

I have developed a grid component consists from the following components: JPanel is a basic container, jScrollPaneBasic is used to display horizontal scrollbar to handle a summary table and a data table. A data table (zebraJTable1) has another scrollPabe called jScrollPaneTabel used to display vertical scrollbar.

Picture below presents the summary row from the application.

Do not hesitate to run a demo at http://www.javanus.com/AssistWeb. Here are the steps needed to display summary rows in the application:

Manual: 
1.start the demo  
2.login as a GUEST username, password is empty 
3.select Invoices in the menu and after that select the menu item Review of cash accounts (Invoices->Review of cash accounts) 
4.click Search button to display data

PivotTable in java (Swing)

Sunday, June 21st, 2009

I was looking for a PivotTable in java(swing) for a long time. I found out three different libraries for PivotTables. Lets check out each one:

1.JIDE – http://www.jidesoft.com/products/pivot.htm

JIDE software looks amazing but it is not an open source project. At the moment I am strictly “opensource” oriented so the JIDE is not the right choice. I would be very glad if they will decided to opensource their libraries. The screenshots looks great.

2.JMAGALLANES – http://sourceforge.net/projects/jmagallanes

It is an opensource project but the project is not active at the moment. The last update is from 2006. I have written an email to the authors and I have got a reply. They have told me they are planning the next release in a few months.

3.OPENSWING – http://oswing.sourceforge.net/

It is an opensource project. The project is active. I have downloaded the opensource library and the pivotTable worked fine. After a while I found a bug (http://sourceforge.net/forum/forum.php?thread_id=3296825&forum_id=572329) but Mcarniel did his job fast and well. The bug was corrected in the next week.

Below is a picture from my application which presents the use of pivotTable.

The above screen shot is from the hotel reservation software. You are free to launch a demo from http://www.javanus.com/AssistWeb/. Below are the steps used to test a PivotTable:

1.Select “Reports->Reservations” in the menu

2.Select “Search” button

Serialization, Timezones and Java Dates

Saturday, June 6th, 2009

Timezone has not been kept while transfering over http.

The data are stored in the tableModel on the client side. TableModel is serialized and sent over the http to the server. The servlet on the server deserializes the data into the tableModel. TableModel is stored into the database.

My client is located in the central Europe and the server is located in the USA. The client time was Jun 06 00:00:00 CEST 2009. The data was transferred over the http and the same date on the server was Jun 05 17:00:00 CDT 2009.

Solution:

I set GMT timezone on both sides.

TimeZone tz = TimeZone.getDefault();
tz.setDefault(TimeZone.getTimeZone(“GMT”));

The client and the server date time are the same after setting default timezone: Jun 06 00:00:00 GMT 2009.

 Do not hesistate to check out a hotel reservation software using the described technology.
 

 

Java Swing application comunicates over http with application server and database

Sunday, May 3rd, 2009

This is the announcement for version 1.0 of Hotel Reservation software written in java. Application is multilanguage but it is still poorly translated at the moment. Main functionality has been translated into English and some other languages. It could be translated to other languages of course if somebody needs it. Application was originally developed in Slovenian language so you could expect to find out some fields which has not been translated yet.

I mixed different technologies in this application: swing, servlets, tomcat and mysql. The entire application is written in swing, it comunicates over http using servlets with the application server, servlets communicate over the jdbc with the database. I plan to describe particular parts of the application in more details in the following days.

You can launch a demo at http://javanus.com/AssistWeb/. The picture below presents basic steps to create your first reservation.

Application works fine as a desktop application which communicate with the database using jdbc at the moment. Please send me an email to info at javanus.com if you would like to test it a desktop version.

Would you like to use an advanced grid object instead of a classic jTable?

Thursday, September 18th, 2008

I started programming in java one year ago. I decided to convert my visual basic code to Swing. I was very unpatienced at the beginning, i did not read the swing manual, I just started to programming.

Today I will try to describe my experience with jtable and swing. In the second part I will present my grid object.

1.It is very easy to start programmic in Swing using Netbeans. It is very similar to all other RAD(Rapid application development) tools. 

2.It is very difficult to develop a more complex application in Swing on the other side. I think the basic element of every business application is a grid object. We can use jTable in Java. I was very happy, when I first saw a jTable a year ago. Ohhh, what a powerful object!!! But I got very disappointed after a few days. I realized that it is very complex to extend jTable for more serious use. And I was an absolute beginner…

3.Have anyone used PowerBuilder in the past. PB has a very powerful datagrid. I tried to develop something similar. I would like to have the following functionallity: present data in grid using SQL statement, easy editing of data…..

4.jTable is very powerful when you learn how to use rendering, cell editors, drag and drop, etc…

Ok, now it is time to move to the second part.

I have developed a grid object. It has the following features:

a) data is displayed in a grid according to sql

b) editing strarted in the next cell when user press enter or tab

c) export data to excel

d) grouping data in jtable

e) store data to database with save method

f) easy to use

Below is a picture which presents group data in a jTable.

Do you think I should start an open source project to develop this object further? Is there any other similar object with described functionalities? I am very intersting in your opinion. Please do not hesitate to answer me.

It will take me a lot of time if I would like to rewrite the object once again. At the moment it is more similar to spaghetti code. 

Ok, what is the purpose of this article? Please encourage me or give me some good arguments like :”Do not waste your time, there are thousands of similar projects….” or even better: “Do that!!! Newbies are waiting for something like this, give Swing a chance!!”

Do you find this post useful? Do not hesitate to leave a reply! Go to the bottom of this page.

jtable selection from one cell to another cell in different column

Monday, September 1st, 2008

This blog presents custom cell selection in jtable. I spent a lot of time searching with google but i did not find nothing similar. I hope this blog will help you.

I have to implemented a custom selection in jtable. I have a table with 8 rows and 4 columns. When I select a cell c2 and end the selection in cell e3 the following cells should be selected (c2,d2,e2,f2,g2,h2,a3,b3,c3,d3,e3). Please see the picture below.

And how the Swing works? I start selection in cell c2. When I move to cell e3 default swing funcionality selects cells (c2,d2,e2,c3,d3,e3). If I would move to cell d3 only cells (c2,d2,c3,d3) would be selected.

How to solve this problem? See the code below. I am sure the code could be optimized a little bit. I will be very glad if you give me some ideas to improve the code. Would you like to have selection like this ?

Run the code below!

import java.awt.*;
import java.util.HashSet;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
import javax.swing.ListSelectionModel;
import javax.swing.*;
 
public class CustomTableSelection extends JFrame {
 
 HashSet cellSelection = new HashSet();
 JTable table;
 int iRowMin;
 int iColMin;
 
    public CustomTableSelection() {
        super(“Custom Table Selection”);
 
        table = new JTable(10, 10) {
            public void changeSelection(
                int row, int column, boolean toggle, boolean extend)
            {
                super.changeSelection(row, column, toggle, extend);
            }
           
            public boolean isCellSelected(int row, int column)
            {
                boolean isSel = cellSelection.contains(new Point(row, column));
                return isSel;
            }
 
            public boolean isColumnSelected(int column)
            {
                return false;
            }
 
            public boolean isRowSelected(int row)
            {
                return false;
            }
        };
       
        table.getSelectionModel().addListSelectionListener (new ListSelectionListener()
        {
            public void valueChanged (ListSelectionEvent e)
            {

             iColMin = table.getColumnModel().getSelectionModel().getMinSelectionIndex();
    if(iColMin == table.getColumnModel().getSelectionModel().getLeadSelectionIndex()) {             
                 iRowMin = table.getSelectionModel().getMinSelectionIndex();
             }
                int li_rowMax = table.getSelectionModel().getLeadSelectionIndex();
                int li_colMax = table.getColumnModel().getSelectionModel().getMaxSelectionIndex();
                selectCells(iRowMin, iColMin, li_rowMax, li_colMax);
            }
        });

        table.getColumnModel().getSelectionModel().addListSelectionListener (new ListSelectionListener()
        {
            public void valueChanged (ListSelectionEvent e)
            {
                iColMin = table.getColumnModel().getSelectionModel().getMinSelectionIndex();
    if(iColMin == table.getColumnModel().getSelectionModel().getLeadSelectionIndex()) {             
                 iRowMin = table.getSelectionModel().getMinSelectionIndex();
             }
                int li_rowMax = table.getSelectionModel().getLeadSelectionIndex();
                int li_colMax = table.getColumnModel().getSelectionModel().getMaxSelectionIndex();
                selectCells(iRowMin, iColMin, li_rowMax, li_colMax);
            }
        });

        table.setRowSelectionAllowed(true);
        table.setColumnSelectionAllowed(true);
       
        getContentPane().setLayout(new BorderLayout());
        getContentPane().add(new JScrollPane(table), BorderLayout.CENTER);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setSize(800, 500);
    }

 private void selectCells(int pRowMin, int pColMin, int pRowMax, int pColMax) {
        cellSelection = new HashSet();
        for(int i=pColMin;i<=pColMax;i++) {
            int fromRow = 0;
            int toRow = 0;
            if (i==pColMin) {
                fromRow=pRowMin;
                if (pColMin==pColMax) {
                    toRow=pRowMax;
                } else {
                    toRow=table.getRowCount()-1;
                }
            } else if (i==pColMax) {
                fromRow=0;
                toRow=pRowMax;
            } else if (pColMin<pColMax) {
                fromRow=0;
                toRow=table.getRowCount()-1;
            }
           
            for (int j=fromRow;j<=toRow;j++) {
                cellSelection.add(new Point(j, i));
            }
        }
        //refresh the table
        table.repaint();
    }
           
    public static void main(String[] args) {
        new CustomTableSelection().setVisible(true);
    }
}

Do you find this post useful? Do not hesitate to leave a reply! Go to the bottom of this page.

It is very easy to develop system tray in java

Saturday, August 23rd, 2008
I have found a very good example on google how to develop system tray functionallity in java. Please check it out Michael Nascimento blog

Unfortunatelly the code could not be compiled without additional imports. Missing imports are included into Michael code.

Here are some pictures:

1.System tray pop up after execution java -cp . SysTray

 System tray

2.System tray tooltip

3.System tray popup menu

I already see different useful situations.

Here is Michael code with missing imports. I do the following modifications: add another menu item an change sleep time to 2 seconds

import java.awt.HeadlessException;
import java.awt.Image;
import java.awt.MenuItem;
import java.awt.Panel;
import java.awt.PopupMenu;
import java.awt.SystemTray;
import java.awt.TrayIcon;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.image.BufferedImage;
import javax.swing.Icon;
import javax.swing.JOptionPane;
import javax.swing.plaf.metal.MetalIconFactory;

public class SysTray {
   public static void main(String[] args) throws Exception {
      TrayIcon icon = new TrayIcon(getImage(), “Java application as a tray icon”,
            createPopupMenu());
      icon.addActionListener(new ActionListener() {
         public void actionPerformed(ActionEvent e) {
            JOptionPane.showMessageDialog(null, “Hey, you activated me!”);
         }
      });
      SystemTray.getSystemTray().add(icon);

      Thread.sleep(2000);

      icon.displayMessage(“Attention”, “Please click here”,
            TrayIcon.MessageType.WARNING);
   }

   private static Image getImage() throws HeadlessException {
      Icon defaultIcon = MetalIconFactory.getTreeHardDriveIcon();
      Image img = new BufferedImage(defaultIcon.getIconWidth(),
            defaultIcon.getIconHeight(), BufferedImage.TYPE_4BYTE_ABGR);
      defaultIcon.paintIcon(new Panel(), img.getGraphics(), 0, 0);

      return img;
   }

   private static PopupMenu createPopupMenu() throws HeadlessException {
      PopupMenu menu = new PopupMenu();

      MenuItem run = new MenuItem(“Run”);
      menu.add(run);
     
      MenuItem exit = new MenuItem(“Exit”);
      exit.addActionListener(new ActionListener() {
         public void actionPerformed(ActionEvent e) {
            System.exit(0);
         }
      });
      menu.add(exit);

      return menu;
   }
}