//class used to handle action events generated by the buttons.
private class ButtonHandler
implements ActionListener{
//Invoked when a user clicks a button.
public void actionPerformed(ActionEvent ae){
//Get the object on which the Event
//initially occurred.
JButton temp = (JButton)ae.getSource();
//Get the action command for this button.
String actionCommand = temp.getActionCommand();
try{
//if add button is clicked
//call the add method
if(actionCommand.equals("Add"))
add();
//if Update button is clicked
//all the update method
else if(actionCommand.equals("Update"))
update();
//if Delete button is pressed
//call the delete method
else if(actionCommand.equals("Delete"))
delete();
//if Clear button is clicked
//call the clear method
else if(actionCommand.equals("Clear"))
clear();
//if Find button is clicked
//call the find method
else if(actionCommand.equals("Find"))
find();
}catch(Exception ex){
showMessageDialog(ex.getMessage());
}
}
//method invoked when add button is pressed
private void add()
throws RemoteException,SQLException{
//Check whether the form is properly filled or not.
if(check()){
showMessageDialog(
"Please fill complete information.");
return;
}
//Parse the string argument.
int rollno = Integer.parseInt(txtRollNumber.getText());
//Get the name of student
String name = txtName.getText();
//Get the course
String course = txtCourse.getText();
//create a student object
//with specified roll no,
//name and course.
Student std = new Student(rollno,name,course);
//Call the remote method to add
//a new student record
String res = server.addRecord(std);
//Clear all text boxes
clear();
//Show the message returned by the server.
showMessageDialog(res);
}
//method invoked when update button is pressed
private void update()
throws RemoteException,SQLException{
//Check whether the form is properly filled or not.
if(check()){
showMessageDialog(
"Please fill complete information.");
return;
}
//get the roll number
int rollno = Integer.parseInt(txtRollNumber.getText());
//get the name
String name = txtName.getText();
//get the course
String course = txtCourse.getText();
//create a student object with specified roll no,
//name and course.
Student std = new Student(rollno,name,course);
//call the remote method to update
//the record.
String res = server.updateRecord(std);
//clear values in the text boxes
clear();
//Show the message returned by the server.
showMessageDialog(res);
}
//Method invoked when Delete button is pressed
private void delete()
throws RemoteException,SQLException{
//get the roll number
String value = txtRollNumber.getText().trim();
if(value.equals("")){
//if roll number is not entered
showMessageDialog("Please enter the Roll number.");
return;
}
//Parse the string argument.
int rollno = Integer.parseInt(txtRollNumber.getText());
//Call the remote method to delete the record.
String res = server.deleteRecord(rollno);
//Clear the text boxes.
clear();
//Show message returned by the server.
showMessageDialog(res);
}
//method invoked when Clear button is pressed
private void clear(){
//set the text of all the text boxes.
txtRollNumber.setText("");
txtName.setText("");
txtCourse.setText("");
}
//method to find a record
private void find()
throws RemoteException, SQLException{
//Get the Roll number of student.
String value = txtRollNumber.getText().trim();
if(value.equals("")){
//if roll number is not entered
showMessageDialog("Please enter the Roll number.");
return;
}
int rollno = Integer.parseInt(txtRollNumber.getText());
// Call the remote method to search the record.
Object res = server.findRecord(rollno);
//If a string is returned by server.
if(res instanceof String){
String temp = (String)res;
showMessageDialog(temp);
clear();
}
else{
Student temp = (Student)res;
//method to fill text boxes with values.
fill(temp);
}
}
//Check whether values are filled or not,
private boolean check(){
boolean value = txtRollNumber.getText().trim().equals("") ||
txtName.getText().trim().equals("")||
txtCourse.getText().trim().equals("");
return value;
}
//fill the text boxes.
private void fill(Student std){
txtName.setText(std.getName());
txtCourse.setText(std.getCourse());
}
//method used to show a dialog box.
private void showMessageDialog(String msg){
JOptionPane.showMessageDialog(null,msg
,"INFORMATION",JOptionPane.INFORMATION_MESSAGE);
}
}
}
|