0% found this document useful (0 votes)
20 views

Advanced Programming Chapter One

Uploaded by

BelayAbeneh
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
20 views

Advanced Programming Chapter One

Uploaded by

BelayAbeneh
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 9

Chapter One

Graphical User Interface and Java Database


connectivity
Creating Graphical User Interface (GUI) using NetBeans IDE
This chapter provides an introduction to Graphical User Interface (GUI) programming in java
with Swing components (Textfield, buttons, Labels, check box, etc) using NetBeans IDE. The
NetBeans IDE is a free, open-source, cross-platform integrated development environment with
built-in support for Java programming language.

The goal of this lesson is to introduce the Swing API (Application program Interface) by
designing a simple application that accept values from one textfield and display in second
textfield. Its GUI will be basic, focusing on only a subset of the available Swing components.
We will use the NetBeans IDE GUI builder, which makes user interface creation a simple matter
of drag and drop. Its automatic code generation feature simplifies the GUI development process,
letting you focus on the application logic instead the underlying infrastructure.

Because this lesson is a step-by-step checklist of specific actions to take, we recommend that you
run the NetBeans IDE and perform each step as you read along. This will be the quickest and
easiest way to begin programming with Swing.

Add Jframeform in your project and drag and drop four textfields, four labels and two buttons.
Let the name of four textfields are txt1, txt2, txt3 and txt4. Let also the caption of the labels are
String Input , Number Input ,Value 1 and Value 2. Let also the name of buttons are btndisplay
and btnclear. Make the caption of the two buttons as Display and Clear as shown below.

To give caption/label right click on the button or label or text field and select Edit Text.

To give name right click on textfield or button or label and select Change Variable Name.

1
Data validations for txt1 and txt2 textfield

Let txt1 must accept only letters and txt2 accept only numbers (0-9). Use the following codes to
do these validations.

//import javax.swing.JOptionPane; at the top of the class

//Letter validation,Right click on txt1,then point to Events then point to Key finally select KeyPessed

private void txt1KeyPressed(java.awt.event.KeyEvent evt) {

if( (evt.getKeyChar() >= 'a' && evt.getKeyChar() <= 'z')||(evt.getKeyChar() >= 'A' && evt.getKeyChar() <=
'Z'))

txt1.setEditable(true);
else {
txt1.setEditable(false);
JOptionPane.showMessageDialog(null," please enter only letter");
txt1.setEditable(true);
}
}
//Number validation
private void txt2KeyPressed(java.awt.event.KeyEvent evt) {
if (evt.getKeyChar() >= '0' && evt.getKeyChar() <= '9')
txt2.setEditable(true);

else {
txt2.setEditable(false);
JOptionPane.showMessageDialog(null," please enter only numeric digits(0-9)");
txt2.setEditable(true);
}
}

2
//Code for btndisplay button
// Right click on btndisplay and point to Events then point to Action and then Actionperformed
private void btndisplayActionPerformed(java.awt.event.ActionEvent evt)
{
String s1=txt1.getText();
txt3.setText(s1);
String s2=txt2.getText();
txt4.setText(s2);
}

private void btnclearActionPerformed(java.awt.event.ActionEvent evt)


{
txt1.setText(“ ”);
txt2.setText(“ “);
txt3.setText(“ “);
txt4.setText(“ “);
}

Second Example: GUI Calculator

Design the following form and write for +,-,*,/,%,= and clear buttons action performed event.
Write also action performed event code for 0,1,2,3,4,5,6,7,8,9 and .(dot) buttons.

3
The operators +,-,*,/,% shoud be set at operator textfield and 1,2,3,4,5,6,7,8,9,0 and . should be
set at Num1 or Num2 textfield.Num1 textfield should be filled first and then the operator
textfield and finally the Num2 textfield.

//code for each button


private void btn1ActionPerformed(java.awt.event.ActionEvent evt) {
String str;
if(txtop.getText().equals(""))
{ str = txtnum1.getText() + btn1.getText();
txtnum1.setText(str);
} else
{ str= txtnum2.getText() + btn1.getText();
txtnum2.setText(str); } }
private void btn2ActionPerformed(java.awt.event.ActionEvent evt) {
String str;
if(txtop.getText().equals(""))
{ str = txtnum1.getText() + btn2.getText();
txtnum1.setText(str);
} else
{ str= txtnum2.getText() + btn2.getText();
txtnum2.setText(str); } }
private void btn3ActionPerformed(java.awt.event.ActionEvent evt) {
String str;
if(txtop.getText().equals(""))
{ str = txtnum1.getText() + btn3.getText();

4
txtnum1.setText(str);
} else
{ str= txtnum2.getText() + btn3.getText();
txtnum2.setText(str); } }
private void btn4ActionPerformed(java.awt.event.ActionEvent evt) {
String str;
if(txtop.getText().equals(""))
{ str = txtnum1.getText() + btn4.getText();
txtnum1.setText(str);
} else
{ str= txtnum2.getText() + btn4.getText();
txtnum2.setText(str); } }
private void btn5ActionPerformed(java.awt.event.ActionEvent evt) {
String str;
if(txtop.getText().equals(""))
{ str = txtnum1.getText() + btn5.getText();
txtnum1.setText(str);
} else
{ str= txtnum2.getText() + btn5.getText();
txtnum2.setText(str); } }
private void btn6ActionPerformed(java.awt.event.ActionEvent evt) {
String str;
if(txtop.getText().equals(""))
{ str = txtnum1.getText() + btn6.getText();
txtnum1.setText(str);
} else
{ str= txtnum2.getText() + btn6.getText();
txtnum2.setText(str); } }
private void btn7ActionPerformed(java.awt.event.ActionEvent evt) {
String str;
if(txtop.getText().equals(""))
{ str = txtnum1.getText() + btn7.getText();
txtnum1.setText(str);
} else
{ str= txtnum2.getText() + btn7.getText();
txtnum2.setText(str); } }
private void btn8ActionPerformed(java.awt.event.ActionEvent evt) {
String str;
if(txtop.getText().equals(""))
{ str = txtnum1.getText() + btn8.getText();
txtnum1.setText(str);
} else
{ str= txtnum2.getText() + btn8.getText();
txtnum2.setText(str); } }
private void btn9ActionPerformed(java.awt.event.ActionEvent evt) {
String str;

5
if(txtop.getText().equals(""))
{ str = txtnum1.getText() + btn9.getText();
txtnum1.setText(str);
} else
{ str= txtnum2.getText() + btn9.getText();
txtnum2.setText(str); } }
private void btn0ActionPerformed(java.awt.event.ActionEvent evt) {
String str;
if(txtop.getText().equals(""))
{ str = txtnum1.getText() + btn0.getText();
txtnum1.setText(str);
} else
{ str= txtnum2.getText() + btn0.getText();
txtnum2.setText(str); } }

private void btnaddActionPerformed(java.awt.event.ActionEvent evt) {

if(txtnum1.getText().equals(""))JOptionPane.showMessageDialog(null," Enter value in the first


textfield");

else{

txtop.setText("+");

btndot.setEnabled(true); } }

private void btnsubtActionPerformed(java.awt.event.ActionEvent evt) {

if(txtnum1.getText().equals(""))JOptionPane.showMessageDialog(null," Enter value in the first


textfield");

else{

txtop.setText("-");

btndot.setEnabled(true); } }

private void btnmultActionPerformed(java.awt.event.ActionEvent evt) {

6
if(txtnum1.getText().equals(""))JOptionPane.showMessageDialog(null," Enter value in the
first textfield");

else{

txtop.setText("*");

btndot.setEnabled(true); } }

private void btndivActionPerformed(java.awt.event.ActionEvent evt) {

if(txtnum1.getText().equals(""))JOptionPane.showMessageDialog(null," Enter value in the first


textfield");

else{

txtop.setText("/");

btndot.setEnabled(true); } }

private void btnmodActionPerformed(java.awt.event.ActionEvent evt) {

if(txtnum1.getText().equals(""))JOptionPane.showMessageDialog(null," Enter value in the first


textfield");

else{

txtop.setText("%");

btndot.setEnabled(true); }

private void btnequalActionPerformed(java.awt.event.ActionEvent evt) {


float val1=Float.parseFloat(txtnum1.getText()); //to convert string to float
float val2= Float.parseFloat(txtnum2.getText());
float sum=val1+val2;
float diff=val1-val2;
float prod=val1*val2;
float div=val1/val2;
float mod=val1%val2;
String oper=txtop.getText();
if(oper.equals("+"))
{ String sumres=Float.toString(sum); //to convert float to string

7
txtresult.setText(sumres);
}
else if(oper.equals("-"))
{ String diffres=Float.toString(diff);
txtresult.setText(diffres); }
else if(oper.equals("*"))
{ String prodres=Float.toString(prod);
txtresult.setText(prodres);
}
else if(oper.equals("/"))
{ String divres=Float.toString(div);
txtresult.setText(divres);
}
else if(oper.equals("%"))
{ String modres=Float.toString(mod);
txtresult.setText(modres);
}
}

private void btnclearActionPerformed(java.awt.event.ActionEvent evt) {


txtnum1.setText("");
txtnum2.setText("");
txtresult.setText("");
txtop.setText("");
btndot.setEnabled(true);
}

private void btndotActionPerformed(java.awt.event.ActionEvent evt) {


String str;
if(txtop.getText().equals(""))
{
str = txtnum1.getText() + btndot.getText();
txtnum1.setText(str);
btndot.setEnabled(false);
}
else {
str = txtnum2.getText() + btndot.getText();
txtnum2.setText(str);
btndot.setEnabled(false); }

8
9

You might also like