Friday, 6 January 2017

GridLayout: fill the form with gridlayout

Swing controls are of two types they are Containers and components. components are arranged on a containers. example components are JLabel, JButton etc and example containers are JPanel, JFrame and JDialog etc.
these components are arranged on a container with the help of layout managers. Few layout managers available in swing toolkit are

1. FlowLayout [Jframe's Default layout manager]
2. GridLayout
3. BorderLayout [JPanel's Default layout manager]
4. GridBagLayout.
5. Custom Layout manager

The custom layout amanger is useful to design programmer's own layout manager. Usually this is useful when any of the available layout managers does not give the needy result.
apart of these above all layout managers, few third party libraries are also available they are

1. DesignGridLayout
2. Mig Layout etc

                                          Designgrid Layout

Gridlayout is one of the most simple and flexible layout managers. layout manager is used to arrange the components on a container with proper margins, gap etc.

Gridlayout is suitable when the number of components are even numbered in each row of the container.

Swing program without any layout manager:

import java.awt.GridLayout;
import java.lang.reflect.InvocationTargetException;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextField;
import javax.swing.SwingUtilities;

public class GridLayoutExample extends JFrame {

private JLabel label1 = null;
private JLabel label2 = null;
private JLabel label3 = null;
private JTextField tf1 = null;
private JTextField tf2 = null;
private JTextField tf3 = null;
private JButton button1 = null;
private JButton button2 = null;

public GridLayoutExample() {

private void initComponents() {

label1 = new JLabel("Name: ");
tf1 = new JTextField();

label2 = new JLabel("Qualification: ");

tf2 = new JTextField();
label3 = new JLabel("Profile: ");

tf3 = new JTextField();

button1 = new JButton("Submit");
button2 = new JButton("Cancel");

public static void main(String[] args) throws InvocationTargetException, InterruptedException {

GridLayoutExample ex = new GridLayoutExample();

ex.setSize(300, 200);
SwingUtilities.invokeAndWait(new Runnable() {
public void run() {


 the output

Swing program after adding a Gridlayout. replace the initComponents() method [ Highlighted] of the above program with the below method........

private void initComponents() {
GridLayout gl = new GridLayout();


label1 = new JLabel("Name: ");
tf1 = new JTextField();

label2 = new JLabel("Qualification: ");

tf2 = new JTextField();
label3 = new JLabel("Profile: ");

tf3 = new JTextField();

button1 = new JButton("Submit");
button2 = new JButton("Cancel");

the output after applying layout manager.....

Tuesday, 27 December 2016

WMI - Windows Management Instrumentaion

WMI is a windows Library used to get the information like no of processes running currently in the computer and their event notifications like process creation modification and deletion notifications, cumputer's name, version, Win32 processes list etc

The information above mentioned can be fetched not only from local PC but also the remote machine too..

WMI can be accessed through a query langauge in two ways they are:

1. WBEMTEST.exe [GUI Tool]
2  WMIC- stands for WMI Commandline tool
3. Programatically [using Powershell and VBScript]

1. wbemtest.exe:

      How to start

         1. go to start
         2.  go to Run
         3. Type wbemtest.exe
         4.  the following window opens up.

Queries that can be run with this tool

1. Usual queries
2. Notification queries

Usual queries are used to query the processes and other system information etc. the query fetches the information from repository and ends the task.

Example Query:

SELECT * From Win32_Process

the above syntax fetches the current running Win32 process.

Notification queries are those which runs continuously and gives the notification of Process creation, modification and deletion and returns the object through which process information can be fetched accordingly.

Example Query:

SELECT * FROM __InstanceDeletionEvent WITHIN 1 WHERE TargetInstance ISA 'Win32_Process'

the above query notifies us whenever an existing process is deleted. WITHIN clause checks the condition for every 1 second. this query needs to be executed in Notification Query window. please refer the above figure.

 SELECT * FROM __InstanceCreationEvent WITHIN 1 WHERE TargetInstance ISA 'Win32_Process'

This above query notifies when new process is created

2. WMIC.exe

The second approach for accessing WMI is WMIC.exe which is a command line tool.

1. goto start
2. goto Run
3. open the CMD.exe
4. type wmic.exe

the above steps opens up the wmic command line tool where we can fetch OS, Processes, memory information and many more :) details of a PC.

Example command

 Process List

the Process list command lists the all the processes running currently in local machine.

Apart of these tools we can access the WMI through programs too using VBScript and Powershell
you can find this in the below post/link


Monday, 26 December 2016

Apache Commons Exec API for executing exe files from java program

In my previous post    you have seen using traditional java API to execute exe files from java program.

but it is proved that it is not an ideal solution for  programs which handles IO operations and for async executables. you can find more advantages of Apache Commons Exec library over traditional API in the below link.


Simple example that uses the Exec library:

// class to pass the command ex: cmd.exe and its arguments...

CommandLine command = new CommandLine("ping");

// arguments to the command object can be passed in 2 ways...

command.addArguments("localhost") ;
command.addArguments("-t") ;


command.addArguments(new String[]{"-t", "-count"});

//handling the IO operations gracefully using LogOutputStream abstract class

LogOutputStream in = new LogOutputStream() {

protected void processLine(String line, int exitvalue) {

                       //handle the output which was produced by the process....
PumpStreamHandler streamHandler = new PumpStreamHandler(in, in);

// Executing the command along with its arguments

DefaultExecutor executor = new DefaultExecutor();

//attaching the created shreamhandler to the executor...


// execution...

the above program executes the ping command and displays the output using the standard out.

Executing the interactive executables can be found in the below link...


API for executing exe files from java program.

The traditional API for calling the executable in java program is using the Runtime, Process and their methods like exec(command), waitFor() methods respectively.

a simple example to execute a notepad.exe from java program using the above API is given below.

Process process = Runtime.getRuntime().exec("notepad.exe");

the above code is used to launch the notepad.exe executable from java program.

if there are multiple commands needs to be passed then the code would be....

String[] command = {"cmd.exe", "/C", "echo ", "%TEMP%"}; 
Process process = Runtime.getRuntime().exec(command);

the above snippet is used to execute multiple commands in a java program. this code prints the environment variable TEMP on to the console.

Handling standard output produced by the above code:

BufferedReader input = new BufferedReader(new InputStreamReader(process.getInputStream()));
String output = "";
String line = "";

while((line = input.readLine()) != null){



This Procedure is ideal when there is an executable to run without waiting for its completion of execution. despite the a method called p.waitFor() which waits until the process execution is completed, it is difficult to handle the process's standard Outputs and Inputs gracefully. and the no of lines of code also increases drastically.

the alternative to this traditional API is Apache Commons Exec  library.