Saturday, 9 July 2011

DesignGridLayout: Simple, yet powerful Layoutmanager for arranging swing components

Swing toolkit comes with few standard Layout Managers where none of them serves the need of arranging components in a way that usually developers require. some of them does, but it takes lot of coding time to achieve it. GridBagLayout is the most flexible layout manager available in swing toolkit but their are so many variables that developer has to look after.

DesignGridLayout can be a useful LayoutManager to arrange components in less time with very small snippet of code and can still get proper alignment. Layouting with DesignGridLayout is as easy as coding with various GUI Builder tools available in varoius IDEs.

Simple program which demonstrates DesignGridLayout:

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JSeparator;
import javax.swing.JTextField;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import net.java.dev.designgridlayout.DesignGridLayout;
import net.java.dev.designgridlayout.Tag;

public class DesignGridLayoutDemo extends JPanel{

  public DesignGridLayoutDemo () {

      DesignGridLayout layout = new DesignGridLayout(this);
      layout.row().grid().add(new JLabel("Username: ")).add(new JTextField("Enter user name "), 2);

      layout.row().grid().add(new JLabel("Password: ")).add(new JTextField("Enter password "), 2);
      layout.emptyRow();layout.emptyRow();
      layout.row().center().fill().add(new JSeparator());
      layout.emptyRow();layout.emptyRow();
      layout.row().bar().add(new JButton(" Login "), Tag.OK).add(new JButton("Cancel"), Tag.CANCEL);
  }


public static void main(String[] args) throws ClassNotFoundException,
                                InstantiationException,
                                IllegalAccessException,
                                UnsupportedLookAndFeelException {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
     SwingUtilities.invokeLater(new Runnable() {

         @Override
    public void run() {
          JFrame frame = new JFrame("Login Form");
          frame.getContentPane().add(new DesignGridLayoutDemo ());
          frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

          frame.pack();
          frame.setVisible(true);
         }
     });
  }
}
Output:
                                                            
This project has been hosted in java.net site where we can find Tutorial, download links and feature etc.

Sunday, 3 July 2011

Java IDE for Learners:


           Simple editors like Notepad, Gedit, edit+  definitely allows us to write a program, but it is always difficult to code, debug and manage even smaller applications. and profession IDEs like Eclipse, Netbeans JDeveloper are definitely not suitable for beginners.[They are pretty advanced to students]

           What if we have an IDE which allows the learners to understand the basic coding principles of java, Fundamentals of Object Oriented Programming[OOP] with the pictorial representation along with syntax highlighting, scope highlighting and other cool features?.. sounds good right.
         
                   
           BlueJ is a simple Java based IDE developed by Kent university for the beginners and students of Java programming language. The coolest thing about BlueJ is it not only allows us to interact[compile, execute] with complete application or program but also single objects.Hence we can create Objects and execute its methods graphically.

Important features are:
  • Create classes and interfaces through wizards and diagrams
  • Create directly object of a class and execute its methods rather than executing complete application.
  • Debugger, Terminal etc
Editor Features:
  •      Syntax Highlighting
  •      Scope Highlighting
  •      Code completion
  •      Navigation View
  •      Switch between source code and Documentation 




Controlling an executable from Java program


Windows tools like Notepad, internet explorer and other executable files can be started from java program through its core API.


Procedure to start a Notepad application from java program:

 Create an object for Runtime class
                         Runtime runtime = Runtime.getRuntime();
       
Create the command string
                         String command = "notepad.exe";        
Call exec()
                         try{
                             Process process = runtime.exec(command);
                         }catch(IOException e){
                     
                         }
         For starting Notepad application does not require Process object, but few executables requires I/O operation. In that case we will have to use the process object exclusively.

Simple program which starts Notepad:


                                                                         Fig-1.01


Procedure to start Browser in java Program:

We have seen starting a notepad application from java program, now let us start Internet Explorer with some URL.

The command string varies depending upon the OS and  Browser we are using.

1. Identify current Operating System and Browser:
                                               
        public boolean isWindowsPlatform() {
     
          String OS = System.getProperty("os.name");
          if(OS != null && OS.startsWith(WIN_ID)) {
              return true;
          }else{
              return false;
          }
    }

2. Create FLAG and PATH:
     
        Windows OS:
               Flag is used to display the URL:
                         String WIN_FLAG = "url.dll,FileProtocolHandler";
               Path is used to open the default browser:
                         String WIN_PATH = "rundll32"
                URL:
                          http://www.java.com
         Unix OS:
               Flag to display the URL:
                        String UNIX_FLAG = "-remote openURL";
               To open Netscape Navigator:
                        String UNIX_PATH = "netscape"
               URL:
                         http://www.java-gui.blogspot.com
3. Create Command String:
     
         String command = WIN_PATH+" "+WIN_FLAG+" "+URL; [windows platform]

4.  Create Runtime object and execute the command:
        Runtime runtime = Runtime.getRuntime();
          try {
                 runtime.exec(command);
          } catch(Exception e){
              e.printStackTrace();
          }

Complete program to start Browser:
/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

public class StartBrowser {

    public StartBrowser(String URL) {

       String command = null;
        // check OS
       if(isWindowsPlatform()) {

         // create command string
         command = WIN_PATH+" "+WIN_FLAG+" "+URL;
         //pass command to the exec()
         Runtime runtime = Runtime.getRuntime();
         try {
              runtime.exec(command);
         } catch(Exception e){e.printStackTrace();}
       }
    }

    public boolean isWindowsPlatform() {
        String OS = System.getProperty("os.name");
        if(OS != null && OS.startsWith(WIN_ID)) {
            return true;
        }else{
            return false;
        }
    }

    public static void main(String[] args) {
         new StartBrowser("http://www.java-gui.blogspot.com");
    }

    private static final String WIN_ID = "Windows";
    // default browser start from windows.
    private static final String WIN_PATH = "rundll32";
    // The flag to display a url.
    private static final String WIN_FLAG = "url.dll,FileProtocolHandler";
}

Snake Game - Just for fun..!

Snake Game