Pradeep's Blog

Google

Monday, May 29, 2006

Customizing JTree

The default look and feel that the JTree provides might not be reasonable for certain applications. For example, the tree structure in eclipse has its own custom icons to represent different things (packages, java files, text files, etc). Let’s have a look at how to customise the JTree, so that we can get to a level of customization as shown below.

There are actually two ways of doing the customization. The first method is straight-forward and this is all that you have to do:

1. Create the tree structure.
2. Create an object of DefaultTableCellRenderer and set the icons as shown below:


ImageIcon customLeafIcon = new ImageIcon(“class.gif”);
DefaultTreeCellRenderer renderer = new DefaultTreeCellRenderer(); renderer.setLeafIcon(customLeafIcon);
tree.setCellRenderer(renderer);

The tree would look something like this now.

Similarly you can change lots of other things. For more details have a look at the API. There are some drawbacks to this method such as, all leaf node icons being the same. So for those of you who would like to tweak a bit more, method two would serve as the better alternative.


The second method is not as simple as the first one. This is what you have to do.

1. Create the tree structure.
2. Create a class that extends the DefaultTableCellRenderer class.
3. Override the method getTreeCellRendererComponent
4. Do all the customization in this method.
5. Create an object of the class that we just created, and set it as the new tree cell renderer.

TreeRenderer renderer = new TreeRenderer();

tree.setCellRenderer(renderer);

The code for the class that extends DefaultTableCellRenderer would look something like this:

And the resulting tree would look something like this.


Monday, May 22, 2006

Session Façade:

Session façade is one of the design patterns used in enterprise applications. Before I explain how it’s actually implemented lets have a look at the various components that make up the enterprise system.
1. Client: Either a browser or a custom software.
2. Session beans: This is where all the business logic takes place in the enterprise system.
3. Entity Bean: It represents the database in object form.
4. And lastly the database.

Now let’s see how session façade can be implemented for a banking application. Let’s consider that the client can access his account via a browser or ATM. If the client wants to transfer money, from his account to another account, there are few things that have to be done by the bank; check his authorization and his balance.

In session façade the client software would never talk directly to the data base (i.e. the entity bean representing the database) instead it would talk to the session bean where, all the business logic like checking his credit balance and his password would be done. It’s the session bean that talks to the entity bean to get the required data to validate and perform the transfer.

The diagram below shows what I have explained.

Monday, May 15, 2006

Creating Custom Charts in Java

I am working on an application that required me to draw custom charts. This is what I was required to do: Draw a chart that consists of smaller graphs (which should each have tool tips) and the chart itself should be scrollable. This blog explains how this can be done and the problems I am facing.

This is what I had as a plan before I started coding,
1. For the chart, it has to extend JComponent, so that we can add the graphs to it and to make it scrollable depending on the number of graphs added.
2. The graphs too should extend JComponent so that we can have tool tips on them.

So the code came up something like this


The most important thing in this is to overload the getPreferredSize() method in the graph Component. If this is not done then the graph are given a default size of (0, 0) and the graphs don’t appear. So it’s important to calculate the size of the graph and return it in that method.
The main problem that I am facing is, in deciding the layout manager and the size of the graphs (which is being worked on).

Monday, May 08, 2006

Progress bar for your application:

Majority of applications need progress bars to show the progress of time consuming operations. Most people have common questions like: Do I need Threads? How to integrate progress bar into my application? This blog answers these common questions.

As for the first question, yes, you need Threads. Without Threads you cannot multitask, meaning you cannot update the progress bar while your time consuming task is running.

The next question is how to integrate progress bar using Threads? Let’s assume that you have a time consuming task in class A. In order to use thread make this class extend Thread. Put the time consuming task in the run method (This enables the task to run in a separate thread by calling the start () method with an object of class A). In this class have an integer variable that you increment according to the progress of your task.

Most applications make the progress bar to popup in a new window. So have it in another class that extends JDialog or JFrame. Add a timer to this class with an action listener added to it. In the action performed method get the integer from class A and set the progress bar’s value to it.

Now that we have both the classes, you can start the task & the progress bar by calling the start () function in class A, followed immediately by a function that starts the timer in the progress bar class.

Monday, May 01, 2006

Finding Class Dependencies in Java

A requirement for our project, CyVis, was to find the classes that a particular class is dependent on. Finding the class dependencies for a particular class can be done as follows

1. Get the type of all the variables declared in the class. This includes the global variables and local variables in all the methods.
2. Get the return type for all the methods.
3. The type of exception thrown in all the methods.
4. The class that the particular class extends.
5. The interface that the particular class implements.

These five would give us a list of classes, that the particular class is dependent upon. Given below is a code snippet that shows, the things you have to look for in order to find the dependent classes. How you find that is up to you. I am using ASM to get this information directly from the class files.

The import statements do not necessarily give you the actual list of dependent classes. There may be some imports that are actually not needed. For the above class the dependent classes can be found from the fields that I have circled.

Using Derby from Command Line

Is there a way to add tables into Derby from command line? The answer is yes. Let’s have a look at how it can be done. Derby provides us tools (java programs), that enable us to create, delete, select and update tables in the database from the command line. Given below is a batch file that enables us to access the database.

***********Batch File *****************
@echo off
rem -- Run Derby ij --
set LIBPATH=C:\Sun\AppServer\derby\lib
java -classpath
"%LIBPATH%\derbytools.jar;%LIBPATH%\derby.jar;%LIBPATH%\derbyclient.jar" org.apache.derby.tools.ij %1.
**********End of Batch File ************
This batch file actually sets that classpath and runs a java program with the argument that we pass along. This argument is a file containing the SQL command that we would like to run. Given below is a text file containing a SQL create statement.
*********Text File ********************
connect 'jdbc:derby://localhost/testdb;create=true' user 'app' password 'app';
CREATE TABLE test_table (
col1 VARCHAR(24) NOT NULL,
col2 VARCHAR(24),
col3 VARCHAR(24),
CONSTRAINT pk_test_table PRIMARY KEY (col1)
);
disconnect;
********* End Of Text File **************

The first and last line of the text file enable us to connects and disconnect with Derby. Any SQL statements that you want to run can be written in the middle. Considering that you have saved the batch file as runDerby.bat & the text file as sql.txt. You can run the SQL statement in the text file on Derby as follows.

Prompt> runDerby sql.txt

For more information look here.