Advanced Programming Chapter One
Advanced Programming Chapter One
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.
//Letter validation,Right click on txt1,then point to Events then point to Key finally select KeyPessed
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);
}
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.
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); } }
else{
txtop.setText("+");
btndot.setEnabled(true); } }
else{
txtop.setText("-");
btndot.setEnabled(true); } }
6
if(txtnum1.getText().equals(""))JOptionPane.showMessageDialog(null," Enter value in the
first textfield");
else{
txtop.setText("*");
btndot.setEnabled(true); } }
else{
txtop.setText("/");
btndot.setEnabled(true); } }
else{
txtop.setText("%");
btndot.setEnabled(true); }
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);
}
}
8
9