Friday, May 16, 2008

Taking input into batch file(sh file) from Java code

Most of the time when we start server, we write lot of code to optimize the condition. Most of those things are tough to write in Java but easy for batch file or sh file to work on. Like, go into a folder, set JAVA_HOME, setting some heap size and then fire the server. Things are not always start forward, sometime we need to pass some message or some path or some value from Java code to batch file.

Here I tried to write one. Say my JAVA_HOME is dynamic and on some condition I decide what's going to be JAVA_HOME and further on that what java and javac going to run(means setting path).

public class batchCheck {
public static void main(String[] args) {
//All JavaHomes
String javaHome[] = {"E:\\Program Files\\Java\\j2sdk1.4.2_05", "E:\\Program Files\\Java\\jdk1.6.0"};
String path;
String line = "";
String pathFile = "E:\\Program Files\\Java\\jdk1.6.0\\bin\\JavaOutput";
String whichJDK = "";
String decisionMaker = "142";
if (decisionMaker.equals("142")) {
whichJDK = javaHome[0];
} else {
whichJDK = javaHome[1];
try {
path = whichJDK + "\\bin";
String cmds[] = {"check.bat", pathFile, whichJDK, path};
Runtime runtime = Runtime.getRuntime();
Process proc = runtime.exec(cmds);
InputStream inputstream = proc.getInputStream();
InputStreamReader inputstreamreader = new InputStreamReader(inputstream);
BufferedReader bufferedreader = new BufferedReader(inputstreamreader);
while ((line = bufferedreader.readLine()) != null) {
} catch (IOException e) {

Now, you can see my check.bat file:

cd %1
DEL HelloWorld.class
set PATH=%3
set JAVA_HOME=%2
echo %JAVA_HOME%
java HelloWorld

So, see how we can access the values from java file into batch file from %1, %2, %3 ... ($1,$2... in sh). Simply I moved on to the path where resides. I have deleted the old class file and the set the path, set the JAVA_HOME, compiled with javac of new JAVA_HOME and ran the code + exit :D.

Lot many things have been done from a very simple code.

Monday, May 12, 2008

Nimbus Look And Feel !

Good news for all the user, using JDK6. Though I am little late in writing blog on this but its OK ;). JDK6 current update comes with a new L&F(Look And Feel) called Nimbus.

Nimbus provide more lively look and feel in Swing UI. Here are some examples:

Default Look And Feel(Click to see enlarged mode)
Nimbus Look And Feel((Click to see enlarged mode)

Since the image is little small and related to my Online Java Project(which I can't change), so I provide another example here from my last blog code.

Default Look And Feel
Nimbus Look And Feel
Something more interesting is focus traversal on components. For Default L&F, you can see that there is a dotted rectangle on Button 3, which says "Focus is here" whereas in Nimbus you can see Button 1 with little bluish highlight which says "Look at my focus" :)

Quickest way to try, command line:

Run my previous (or any UI code) with the following option:

java LayoutCheck

Off course, you can do it with code :
Use try, catch and respect Exception handling as well.

Sunday, May 11, 2008

Layout use in Java

Setting the right layout is one of the major concerns in Java Programming. Swing Package provides number of classes and API's for setting border and layout but using the right one will make our UI lively and error free. First of all, when we are worrying about Layout just worry for Panel and content Panes nothing else.
Now here we will check some of the common Layout style and difference between them. Default Layout for JPanel is FlowLayout but what I use most commonly is GridLayout and BorderLayout. Lets first talk about the grid layout. Here is a simple code:

import java.awt.*;
import javax.swing.*;
import javax.swing.border.*;

public class LayoutCheck extends JPanel {

public LayoutCheck() {
JButton button1 = new JButton("Button 1");
JButton button2 = new JButton("Button 2");
JButton button3 = new JButton("Button 3");
JButton button4 = new JButton("Button 4");

setLayout(new GridLayout(2, 2));
setBorder(new TitledBorder(new EtchedBorder(EtchedBorder.LOWERED), "All Buttons"));
public static void main(String[] args) {
LayoutCheck lc = new LayoutCheck();
JFrame frame = new JFrame("Checking Layouts");

So, 4 buttons with grid layout of 2 X 2. Output will be definitely like this:

Playing on these 2 lines will tell us more:

setLayout(new GridLayout(2, 2));
setBorder(new TitledBorder(new EtchedBorder(EtchedBorder.LOWERED), "All Buttons"));

is place of (2,2) if we do (4,1) or (1,4) it will give vertical and horizontal look. What if we want to put some gaps in between the buttons. We need to use:

setLayout(new GridLayout(2, 2, 5, 5));

It will provide horizontal and vertical gap of 5. Second line in code is setting the recgaular box around the buttons "All Buttons". There are some options in EtcherBorder, please check the API page for more detail.

GridLayout divides the whole panel into grid, so maximizing or minimizing will going to increase or decrease the size of button, unlike other layout.

From Sun Java Document, I am just copying that in which case which layout need to be used. Follow this religiously, we can make a rich UI based application:

Scenario: You need to display a component in as much space as it can get. If it is the only component in its container, use GridLayout or BorderLayout. Otherwise, BorderLayout or GridBagLayout might be a good match. If you use BorderLayout, you will need to put the space-hungry component in the center. With GridBagLayout, you will need to set the constraints for the component so that fill=GridBagConstraints.BOTH. Another possibility is to use BoxLayout, making the space-hungry component specify very large preferred and maximum sizes.

Scenario: You need to display a few components in a compact row at their natural size. Consider using a JPanel to group the components and using either the JPanel's default FlowLayout manager or the BoxLayout manager. SpringLayout is also good for this.

Scenario: You need to display a few components of the same size in rows and columns. GridLayout is perfect for this.

Scenario: You need to display a few components in a row or column, possibly with varying amounts of space between them, custom alignment, or custom component sizes. BoxLayout is perfect for this.

Scenario: You need to display aligned columns, as in a form-like interface where a column of labels is used to describe text fields in an adjacent column. SpringLayout is a natural choice for this. The SpringUtilities class used by several Tutorial examples defines a makeCompactGrid method that lets you easily align multiple rows and columns of components.

Scenario: You have a complex layout with many components. Consider either using a very flexible layout manager such as GridBagLayout or SpringLayout, or grouping the components into one or more JPanels to simplify layout. If you take the latter approach, each JPanel might use a different layout manager.

We will talk about some of the common scenario in our next blog session.

Thursday, May 08, 2008

Filter file(s) in JFileChooser

JFileChooser is one of the most important components when we talk about Swing application from small to big size applications. Most of the time we write the code of JFileChooser on a button listener aka open a file on button name Open, so code will go :

open.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
int returnVal = fc.showOpenDialog(FileChooserFrame);
//do action according to the value of returnVal

But its always better to put a filter on a specific type of file which is required. Most of the time user don't required all types of files. Say, he need only .avi files or .mov files. Thens its a good idea to put a filter which will give only .avi or .mov files in the file chooser option. Month back, I had written one small code which do filtering for .java and .sh files and here it is:

import javax.swing.*;
import javax.swing.filechooser.FileFilter;

class JtregFilter extends FileFilter {

public boolean accept(File f) {
if (f.isDirectory())
return true;
String s = f.getName();
int i = s.lastIndexOf('.');

if (i > 0 && i < s.length() - 1)
if (s.substring(i + 1).toLowerCase().equals("java" ) || s.substring(i + 1).toLowerCase().equals("sh" ))
return true;

return false;

public String getDescription() {
return "*.java, *.sh";

Now nothing need to do in listener except adding one more line :

open.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
fc.addChoosableFileFilter(new JtregFilter());
int returnVal = fc.showOpenDialog(FileChooserFrame);
//do action according to the value of returnVal

and we are done. Now JFileChooser will only show me folders and file with extension .java or .sh. Worthwhile to write a small code for user ease.