Wednesday, February 27, 2008


Swing Tutorials : Swing Toolbar Example

Have you see some small icons on top in Microsoft Word, Excel or other application. It is known as shortcut toolbar. Here is the example how to create toolbar using Java Swing :

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

public class ToolbarExample{
public static void main(String[] args) {
JFrame frame = new JFrame("Toolbar Example Using Java Swing");
JToolBar toolbar = new JToolBar("toolBar", JToolBar.HORIZONTAL);
JButton cutbutton = new JButton(new ImageIcon("cut.gif"));
toolbar.add(cutbutton);
JButton copybutton = new JButton(new ImageIcon("copy.gif"));
toolbar.add(copybutton);
JButton pastebutton = new JButton(new ImageIcon("paste.gif"));
toolbar.add(pastebutton);
frame.getContentPane().add(toolbar,BorderLayout.NORTH);
frame.setUndecorated(true);
frame.getRootPane().setWindowDecorationStyle(JRootPane.PLAIN_DIALOG);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(500,400);
frame.setVisible(true);
}
}


Monday, February 25, 2008


Java Example : Sum of Two Numbers

A very simple example to add 2 numbers. Use Integer.parseInt to convert the value in integer. By default it will consider the value as string.

public class AddNumbers{
public static void main(String[] args) {
System.out.println("Addition of two numbers!");
int a = Integer.parseInt(args[0]);
int b = Integer.parseInt(args[1]);
int sum = a + b;
System.out.println("Sum: " + sum);
}
}