Now that we’ve added JComboBoxes, and Buttons to Create New and Save Data, lets work on loading data from our data source.
We have our existing Student Data Access Class which we can use. Now we need to focus on getting a list of students, and displaying them.
To do so, we’re going to start with a JList
which we’re going to call allStudents. This will be a class level variable.
Updating the Constructor
Next we’re going to update the constructor to create the list box.
allStudents = new JList<Student>(StudentDA.getStudents());
allStudents.setBounds(20, 150, 100, 125);
this.add(allStudents);
Notice how we’re passing in the list of all students from our Data Access Object.
Fixing the Student Display
If we run this, we’ll see something peculiar. Specifically, the content of the list box will have Student@ and then some weird value. That is the memory address, and the default toString()
result. So we’ll need to override the toString()
of our Student Class.
public String toString() {
return this.firstName + " " + this.lastName;
}
Now when we display the list box, we see a regular list of names to select. These names may be truncated due to the width of the JList
box, but it works for now.
Loading Data
Previously we’ve created new students and saved existing students. Now let’s load from the list.
We’re going to add one more button, a loadButton
, and we’re going to do the initial set up like many of the existing buttons.
loadButton = new JButton("Load Student");
loadButton.setBounds(120, 200, 200, 25);
loadButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
loadSelectedStudent();
}
});
this.add(loadButton);
Now we need to actually write the loadSelectedStudent()
method.
Selecting Data From the List
To load the data, we’re going to have to select data from the JList box.
First we’re going to need to determine which index was selected. Our items are stored in the order which they were created in our vector which makes it easy to access. For more complicated scenarios, we’d get the object and get an ID and look it up that way.
public void loadSelectedStudent() {
int selected = this.allStudents.getSelectedIndex();
}
The next thing we need to do, is use the Student Data Access object to get that student, and store it in our student class variable. Luckily we’ve got a method in the Data Access object which will do this for us.
student = StudentDA.getStudent(selected);
Then we need to set our needsAdding boolean flag to false, since this is existing data.
needsAdding = false;
Finally, we’ll call our display student method to load the data into the form fields.
this.displayStudent();
Completed our method looks like what you see below.
public void loadSelectedStudent() {
int selected = this.allStudents.getSelectedIndex();
student = StudentDA.getStudent(selected);
needsAdding = false;
this.displayStudent();
}
Notice there isn’t a lot of code there really. Why, because we’ve created the other parts of it already to do different sections. By logically and physically separating our code, we can write smaller methods much faster.
Where Else Could We Enhance
Of course, we could take this and run with the idea adding various new features.
- Allowing for active/inactive students (JCheckBox and disabling certain fields)
- Improving Layout
- Change student when you select the JList box (Adding a Listener to the JList)
- Making sure changed data is saved (boolean flag with dialog box)
- Checking for errors on save (i.e. only active students can change a major – how do you display error?)
Adding a JList Box was originally found on Access 2 Learn