In our first examples of building a Form in Java, we looked at adding a button and using a simple event listener. Now we want to make a useful button with our Java GUI, ones that will load and save data, as well as one which will allow for creating new data.
We’re going to add to the Student UI we created earlier.
Adding a New Student
Remember, the UI should be separate from the actual content, so when we create a new student record, we’re going to blank out the form fields basically. We will also, set the student, to be a new blank student, which can then be used to save/update.
We’re going to create a JButton
which will be used for new students called newButton
. This will be a class level variable like the other form fields.
Next we’re going build this inside the constructor. We’re going to set the text when we build the JButton, and set the size and position before adding it to the Window.
newButton = new JButton("Create New Record");
newButton.setBounds(120, 150, 200, 25);
this.add(newButton);
We can run and test this to make sure the button is placed under the text boxes, with some space between them.
Adding an Event Listener
Next, we’re going to add an Action Event Listener to our button, just like before. However, instead of changing the text of the button, we’re going to call an existing function inside of our class.
newButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
newStudent();
}
});
Notice that we don’t call this.newStudent()
. That is because this would be referencing that internal class, and we need to reference the external class.
Building the Function
Event Listener functions tend to be small so they can operate quickly and efficiently. They will often hand off the complicated logic to another function, which is what we’ve done here, although it isn’t too complicated.
We’re going to create a Boolean flag to see if we need to add it to our Data Access list (which will be described later) or if we are going to update. With a new record, we’re going to make sure we set this to true. Then we will blank out the data fields and set up a new Student object.
public void newStudent() {
needsAdding = true;
student = new Student();
this.first.setText("");
this.last.setText("");
this.major.setSelectedIndex(0);
}
Notice to blank them out, we simply setText
to an empty string ""
.
Saving a Student
Saving a student starts off the same for most cases. We’ll need a new JButton called saveButton in this case, and we’ll need to initialize it and add it to our form with another action listener.
saveButton = new JButton("Save Record");
saveButton.setBounds(120, 200, 200, 25);
saveButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
saveCurrentStudent();
}
});
this.add(saveButton);
saveCurrentStudent() is a method which will save our data. If we have loaded a student, we can just update that record. However, what if we haven’t? That means we need a way to write to our data storage.
Our Data Access Layer
We’re going to build a very simple Data Access Layer for students which will allow access to students, and it will allow most of the basic CRUD operations. Due to the simplicity of this system, we’re going to just store data temporarily in a Vector
, which is similar to a dynamically sized array.
Obviously, if this was a bigger project, we would store data outside of this system, and into a file, database, etc.
import java.util.Vector;
public class StudentDA {
private static Vector<Student> students = new Vector<Student>();
private StudentDA() { }
private static void init() {
Student s = new Student("Tom", "Jones", "Computer Science");
students.add(s);
s = new Student("Mary Jane", "Watson", "History");
students.add(s);
s = new Student("Jennfer", "Albertson", "Business");
students.add(s);
s = new Student("James", "Kirk", "Computer Science");
students.add(s);
s = new Student("Alice", "Rosenbaum", "Art");
students.add(s);
}
public static Vector<Student> getStudents() {
if(students.size() < 1) {
StudentDA.init();
}
return students;
}
public static Student getStudent(int index) {
if(students.size() < 1) {
StudentDA.init();
}
return (Student)students.get(index);
}
public static void addStudent(Student newStudent) {
students.add(newStudent);
}
}
Notice we’re using a simple Singleton design pattern with static methods to make this work.
Saving our Student Method
Now we’re going to actually save the data to the object, and record this.
public void saveCurrentStudent() {
student.setFirstName(this.first.getText());
student.setLastName(this.last.getText());
student.setMajor(this.major.getSelectedItem().toString());
}
This works if we have an existing Student, but what if we need to add a student. We’ll check our boolean flag, and if so, use the add command for a Data Access object.
public void saveCurrentStudent() {
student.setFirstName(this.first.getText());
student.setLastName(this.last.getText());
student.setMajor(this.major.getSelectedItem().toString());
if(this.needsAdding) {
// reset the flag so we don't keep adding
this.needsAdding = false;
StudentDA.addStudent(student);
}
}
Adding Additional Interactivity to a Form was originally found on Access 2 Learn
One Comment
Comments are closed.