Monday, April 14, 2008

The power of GUI

Let me now demostrate how to build up a simple GUI for a simple decimal to binary converter.

What do we need in our GUI?
1) A simple base window of customized size and attributes.
2)An input field
3)An output field
4)A Result button
5)An Exit button.

How to do?

i) Click on File-> New Project-> Java-> Java Application,and click next.
ii) Give project name as Coverter, and select the basic application shell. We'll deal with databases later. Click on finish to find the following screen.

iii)We'll now add from the swing Controls two text fields for input and output respectively, and our two buttons. With little rearranging after drag and drop and changing sizes our window looks like this.

Sleek and slim like we require.
iv)now to the coding part. Right click on the exit button and select Events->Action->Action performed
The following code is generated
private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {
//TODO code here
}

In the area of the comment type System.exit(0); to complete the exit button functionality.
v) Now select the action for the Convert button. Here's the code for the conversion BTW

private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
Integer num;
int x,y=0,noofbits=0,z=0;
num=Integer.parseInt(jTextField1.getText());

x=num;
for(;x!=0;x=x>>1) {y++;}
noofbits=y;
int i=0;
for(int j=noofbits-1;j>=0;j--)
{
z=((z*10)+((num>>j)&01));
}
jTextField2.setText(String.valueOf(z));
}


vi)GOTO the converterAboutBox.java and design it as you please.
vii) Press F6 to check out your fully functional Desktop application.
Cool to have something functional isn't it ;)

No comments: