In our previous examples, we built the GUI from within the main method, and then built the GUI into an external class. We said that was beneficial in case we wanted to extend a class, like we will here.
While this will create some extra work on the front end, it will make things easier on the back end, especially for complicated project, where you might have a panel that needs to be on different screens/windows/frames/etc.
We’ll demonstrate a simple example like those from before, and see how that will work in this case, and then expand upon it late.
Setting Up a Data Class
First, we will want to set up a Student class, which will hold some information for us.
public class Student {
String firstName;
String lastName;
String major;
public Student() {
}
public Student(String first, String last, String major) {
firstName = first;
lastName = last;
this.major = major;
}
public void setFirstName(String value) {
this.firstName = value;
}
public String getFirstName() {
return this.firstName;
}
public void setLastName(String value) {
this.lastName = value;
}
public String getLastName() {
return lastName;
}
public void setMajor(String value) {
this.major = value;
}
public String getMajor() {
return major;
}
}
Creating our New Frame
At this point we need to create a new class, which we’ll call StudentUI, and add the class level variables we’ll need to make it display.
Our display will be to take three values, and show them in text fields for first and last name, as well as a student’s major.
Our base class is going to look like:
import javax.swing.*;
import java.awt.event.*;
public class StudentUI extends JFrame {
Student student;
JTextField first;
JTextField last;
JTextField major;
JLabel firstLabel;
JLabel lastLabel;
JLabel majorLabel;
}
In the constructor, we’re going to need to set up all of these values.
public StudentUI() {
this.setTitle("Student Demo");
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
first = new JTextField();
last = new JTextField();
major = new JTextField();
firstLabel = new JLabel("First Name:");
lastLabel = new JLabel("Last Name:");
majorLabel = new JLabel("Major:");
firstLabel.setBounds(20, 25, 100, 25);
lastLabel.setBounds(20, 50, 100, 25);
majorLabel.setBounds(20, 75, 100, 25);
first.setBounds(120, 25, 200, 25);
last.setBounds(120, 50, 200, 25);
major.setBounds(120, 75, 200, 25);
this.add(firstLabel);
this.add(first);
this.add(lastLabel);
this.add(last);
this.add(majorLabel);
this.add(major);
setSize(350,500);
setLayout(null);
this.setVisible(true);
}
Once again, we can choose to either display this frame now, or later with another method. In this instance we’re going to set the frame now.
Our Application Class
Finally we’ll need to create an application class to build/display this JFrame.
public class StudentApp1 {
public static void main(String args[]) {
StudentUI sui1 = new StudentUI();
}
}
Enhancing This Further
The next question for this, is how would we display a student with the class we created earlier? That could be done with a few simple methods to the UI class, and is how we would in a multi-tiered application.
First, we’ll set up some methods to handle getting, setting, and display a student.
public void setStudent(Student newStudent) {
if(newStudent != null) {
this.student=newStudent;
this.displayStudent();
}
}
public Student getStudent() {
return this.student;
}
public void displayStudent() {
if(student != null) {
this.first.setText(student.getFirstName());
this.last.setText(student.getLastName());
this.major.setText(student.getMajor());
}
}
Next, we’ll overload the constructor, to allow it to take a Student Passed into it.
public StudentUI(Student newStudent) {
this();
this.setStudent(newStudent);
}
Notice how we’re calling the parent constructor, so that we properly set up the JFrame
. We then setStudent()
which also sets the student, and updates the display.
The displayStudent
method, called from within setStudent
, gets the attributes of the Student class, and displays them.
Because this information is all part of the class, it makes it easy to display/change values within the interface.
Extending an Class for a GUI was originally found on Access 2 Learn
4 Comments
Comments are closed.