Credits

Monday, November 15, 2010

Easy Java Data Entry using Java GUI Components

Java can both support command and GUI data entry.  But, for those who are newbie in Java, GUI JOptionPane.showInputDialog can be an easier command to use.

Below is the sample program that uses GUI command to replace command streams like BufferedReader for data entry.

[+/-] show/hide


/**
*
* @author Rose
*/
/*Sample program on methods
*/
import javax.swing.*;
public class TuitionMethodv3 {
double tuition;
double tunits;
double unitprice;

String name;
String course;

void setName(String xname){
name=xname;
}

void setCourse(String xcourse){
course=xcourse;
}

void setUnitPrice(double xunitprice){
unitprice=xunitprice;
}

void setTotalUnits(double xtunits){
tunits=xtunits;
}

String getName(){
return name;
}

String getCourse(){
return course;
}

double getUnitPrice(){
return unitprice;
}

double getTotalUnits(){
return tunits;
}

double compute()
{ tuition= tunits*unitprice;
return tuition;}

public static void main(String args[]) {

TuitionMethodv3 myObject=new TuitionMethodv3();

String myname=JOptionPane.showInputDialog(null,"Input your name:");
String mycourse= JOptionPane.showInputDialog(null,"Input your course:");
String totalunits=JOptionPane.showInputDialog(null,"Input tuition units:");
String unitrate=JOptionPane.showInputDialog(null,"Input unit price:");

double mytunits=Double.parseDouble(totalunits);
double myuprice=Double.parseDouble(unitrate);

myObject.setName(myname);
myObject.setCourse(mycourse);
myObject.setUnitPrice(myuprice);
myObject.setTotalUnits(mytunits);

JOptionPane.showMessageDialog(null,"Name: "+myObject.getName()+"\nCourse: "+myObject.getCourse()+"\nTotal Units: "+myObject.getTotalUnits()+"\nUnit Price: "+myObject.getUnitPrice()+"\nDue Tuition: "+myObject.compute());

}
}

AddThis