What to do Create a project named Unit03 in NetBeans. Use JavaFX components (no
ID: 3888934 • Letter: W
Question
What to do Create a project named Unit03 in NetBeans. Use JavaFX components (no AWT or Swing) and do not use the GUI builder in your IDE. Part 1 – Build a simple GUI program. We’ll add to it in part 2. There is a part 3, but it is optional. There is really nothing new in this part. Consider it a review of the JavaFX you learned in Java I. 1. DisplayStudent – Create the DisplayStudent class. It will extend Application and contain a start() method, and a main() method too if you wish to use that style. 2. Set the title on the primary Stage to “Display Student”. 3. We want to create a GUI that looks like this figure. The visible components (other than the Stage) is a TextArea and two Labels below it. To get the layout to look the way it does we will use two BorderPanes. (Could we use other layout panes and get the identical result?) 4. The first BorderPane will layout the TextArea above (by placing it in the “Top” part) and the two Labels below (by placing them in the ”Bottom” part of the pane). But how to layout the two Labels? 5. The second BorderPane will layout the Labels by putting one in the “Left” part and one in the “Right” part. Then we put the second BorderPane in the “Bottom” part of the first pane. 6. Load the Text area with a couple of lines of test data using the appendText() method twice, one method call per line (hint: a string that ends in “ ” will cause a newline to be displayed in the TextArea, otherwise the appended lines will run together). 7. Put text into the two Labels as shown above, “Connected to Test” and “Number of Students = 2”. 8. The only buttons that have to work now are the buttons on the title bar (including the Exit button). The screen shot above is from a Linux system, if you are using Windows or a Mac your wiindow decorations will look different. No matter, they do the same things. Hints: Use the following code to create your Scene: Scene scene = new Scene(new VBox(), 400, 260); Use this code to add your pane to the Scene: ((VBox) scene.getRoot()).getChildren().addAll(pane); Using a VBox as the base will help with part 2. Version 1.1 page 1of 4 There! You are done with part one. We’ve “wired in” the test data into the GUI code, and that is OK for now, we’re just trying to get the layout right. Eventually we’ll learn how to pull out the test data and get some real data from a data source using a separate class. What To Turn In Get this working but it’s not ready to turn in yet. We’ll do more in part 2. Part 2 – Adding menus This is something you did not learn in the Java I JavaFX units. Study the Unit 03 materials carefully before attempting this part. 1. DisplayStudent – We will add some code to the DisplayStudent class. 2. Add a MenuBar and two Menus: File and Edit. After creating the MenuBar, include it first in the addAll() method for your scene (so you add the MenuBar and then the pane). 3. On the File menu add three menu items: Connect, Close & Exit. 4. On the Edit menu add two menu items: Search and Clear. 5. When the program starts make sure the TextArea is blank, the left hand Label’s text says “No Connection” and the right hand Label says “Number of Students = 0”. 6. When File Connect is clicked; Put up an Input Dialog that asks for a database name. Store the input from the user in a String variable called database_name. Set the text in the left hand Label to “Connected to xxxx” where xxxx is the database_name. 7. When File Close is clicked; Put up a Confirmation Dialog box. If the user response is “OK” then set the database_name string to null. Set the left hand Label to “No Connection”. If “Cancel” is clicked, do nothing to the labels. 8. When File Exit is clicked; Put up a confirmation dialog box. If the use clicks “OK”, shut down the program, other wise just continue execution. 9. When Edit Search is clicked; Check to see if the database_name is null. If so put up an Error dialog. If the database name is not null put up an Input Dialog requesting a Student ID number. Store the input from the user in a String variable called search_id and then put some test data into the TextArea. Set the right hand Label to “Number of students = n” where n is the number of lines of test data. 10. When Edit Clear menu item is clicked; Clear out the data in the TextArea. Set the right hand Label to “Number of students = 0”. Here are two screen shots, one just before connection and the other after search.
Explanation / Answer
class 1:
AppMain.java
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package student;
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.stage.Stage;
/**
*
* @author Veerendra PAtil H V
*/
public class AppMain extends Application {
public static void main(String[] args) {
Application.launch(args);
}
public void start(Stage stage) throws Exception {
stage.setScene(new Scene(new StudentUI(), 600, 280));
stage.show();
}
}
class 2:
student.java
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package student;
/**
*
* @author Veerendra Patil H V
* POJO class
*/
public class Student {
Student(){}
private int studentId;
private String firstName;
private String middleName;
private String lastName;
private String email;
private String phone;
public int getStudentId() {
return studentId;
}
public void setStudentId(int studentId) {
this.studentId = studentId;
}
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getMiddleName() {
return middleName;
}
public void setMiddleName(String middleName) {
this.middleName = middleName;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public String getPhone() {
return phone;
}
public void setPhone(String phone) {
this.phone = phone;
}
}
class 3
Stdudentbean.java
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package student;
import com.sun.rowset.JdbcRowSetImpl;
import java.sql.SQLException;
import javax.sql.rowset.JdbcRowSet;
/**
*
* @author Veerendra Patiol H V
*/
public class StudentBean {
static final String JDBC_DRIVER = "com.mysql.jdbc.Driver";
static final String DB_URL = "jdbc:mysql://localhost:33061/mydatabase";
static final String DB_USER = "root";
static final String DB_PASS = "secret";
private JdbcRowSet rowSet = null;
public StudentBean() {
try {
Class.forName(JDBC_DRIVER);
rowSet = new JdbcRowSetImpl();
rowSet.setUrl(DB_URL);
rowSet.setUsername(DB_USER);
rowSet.setPassword(DB_PASS);
rowSet.setCommand("SELECT * FROM Person");
rowSet.execute();
}catch (SQLException | ClassNotFoundException ex) {
ex.printStackTrace();
}
}
public Student create(Student s) {
try {
rowSet.moveToInsertRow();
rowSet.updateInt("studentId", s.getStudentId());
rowSet.updateString("firstName", s.getFirstName());
rowSet.updateString("middleName", s.getMiddleName());
rowSet.updateString("lastName", s.getLastName());
rowSet.updateString("email", s.getEmail());
rowSet.updateString("phone", s.getPhone());
rowSet.insertRow();
rowSet.moveToCurrentRow();
} catch (SQLException ex) {
try {
rowSet.rollback();
s = null;
} catch (SQLException e) {
}
ex.printStackTrace();
}
return s;
}
public Student update(Student s) {
try {
rowSet.updateString("firstName", s.getFirstName());
rowSet.updateString("middleName", s.getMiddleName());
rowSet.updateString("lastName", s.getLastName());
rowSet.updateString("email", s.getEmail());
rowSet.updateString("phone", s.getPhone());
rowSet.updateRow();
rowSet.moveToCurrentRow();
} catch (SQLException ex) {
try {
rowSet.rollback();
} catch (SQLException e) {
}
ex.printStackTrace();
}
return s;
}
public void delete() {
try {
rowSet.moveToCurrentRow();
rowSet.deleteRow();
} catch (SQLException ex) {
try {
rowSet.rollback();
} catch (SQLException e) { }
ex.printStackTrace();
}
}
public Student moveFirst() {
Student s = new Student();
try {
rowSet.first();
s.setStudentId(rowSet.getInt("personId"));
s.setFirstName(rowSet.getString("firstName"));
s.setMiddleName(rowSet.getString("middleName"));
s.setLastName(rowSet.getString("lastName"));
s.setEmail(rowSet.getString("email"));
s.setPhone(rowSet.getString("phone"));
} catch (SQLException ex) {
ex.printStackTrace();
}
return s;
}
public Student moveLast() {
Student s = new Student();
try {
rowSet.last();
s.setStudentId(rowSet.getInt("personId"));
s.setFirstName(rowSet.getString("firstName"));
s.setMiddleName(rowSet.getString("middleName"));
s.setLastName(rowSet.getString("lastName"));
s.setEmail(rowSet.getString("email"));
s.setPhone(rowSet.getString("phone"));
} catch (SQLException ex) {
ex.printStackTrace();
}
return s;
}
public Student moveNext() {
Student s = new Student();
try {
if (rowSet.next() == false)
rowSet.previous();
s.setStudentId(rowSet.getInt("personId"));
s.setFirstName(rowSet.getString("firstName"));
s.setMiddleName(rowSet.getString("middleName"));
s.setLastName(rowSet.getString("lastName"));
s.setEmail(rowSet.getString("email"));
s.setPhone(rowSet.getString("phone"));
} catch (SQLException ex) {
ex.printStackTrace();
}
return s;
}
public Student movePrevious() {
Student s = new Student();
try {
if (rowSet.previous() == false)
rowSet.next();
s.setStudentId(rowSet.getInt("personId"));
s.setFirstName(rowSet.getString("firstName"));
s.setMiddleName(rowSet.getString("middleName"));
s.setLastName(rowSet.getString("lastName"));
s.setEmail(rowSet.getString("email"));
s.setPhone(rowSet.getString("phone"));
} catch (SQLException ex) {
ex.printStackTrace();
}
return s;
}
public Student getCurrent() {
Student s = new Student();
try {
rowSet.moveToCurrentRow();
s.setStudentId(rowSet.getInt("personId"));
s.setFirstName(rowSet.getString("firstName"));
s.setMiddleName(rowSet.getString("middleName"));
s.setLastName(rowSet.getString("lastName"));
s.setEmail(rowSet.getString("email"));
s.setPhone(rowSet.getString("phone"));
} catch (SQLException ex) {
ex.printStackTrace();
}
return s;
}
}
class 4
StudentUI.java
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package student;
import java.util.Random;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.TextField;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.GridPane;
import javafx.scene.layout.HBox;
import javafx.scene.layout.Pane;
/**
*
* @author Talkative Parents
*/
public class StudentUI extends BorderPane {
private Label msgLabel = new Label();
private TextField idField = new TextField();
private TextField fNameField = new TextField();
private TextField lNameField = new TextField();
private TextField emailField = new TextField();
private TextField phoneField = new TextField();
private Button createButton = new Button("New");
private Button updateButton = new Button("Update");
private Button deleteButton = new Button("Delete");
private Button firstButton = new Button("First");
private Button prevButton = new Button("Prev");
private Button nextButton = new Button("Next");
private Button lastButton = new Button("Last");
private StudentBean bean = new StudentBean();
public StudentUI() {
setPadding(new Insets(10, 10, 10, 10));
setTop(msgLabel);
setCenter(initFields());
setBottom(initButtons());
setFieldData(bean.moveFirst());
}
private Pane initButtons() {
HBox box = new HBox();
box.setAlignment(Pos.CENTER);
box.setSpacing(5);
box.getChildren().add(createButton);
createButton.setOnAction((EventHandler<ActionEvent>) new ButtonHandler());
box.getChildren().add(updateButton);
//...
box.getChildren().add(lastButton);
lastButton.setOnAction(new ButtonHandler());
return box;
}
private Pane initFields() {
GridPane grid = new GridPane();
grid.setAlignment(Pos.CENTER);
grid.setPadding(new Insets(10, 10, 10, 10));
grid.setHgap(20);
grid.setVgap(2);
grid.add(new Label("ID"), 1, 0);
grid.add(idField, 2, 0);
idField.setEditable(false);
grid.add(new Label("First Name"), 1, 1);
grid.add(fNameField, 2, 1);
//...
return grid;
}
private Student getFieldData() {
Student s = new Student();
s.setStudentId(Integer.parseInt(idField.getText()));
//...
return s;
}
private void setFieldData(Student s) {
idField.setText(String.valueOf(s.getStudentId()));
}
private boolean isEmptyFieldData() {
return false;
//...
}
private class ButtonHandler implements EventHandler<ActionEvent> {
@Override
public void handle(ActionEvent e) {
Student s = getFieldData();
if (e.getSource().equals(createButton)
&& createButton.getText().equals("Save")) {
if (isEmptyFieldData()) {
msgLabel.setText("Cannot create an empty record");
return;
}
if (bean.create(s) != null)
msgLabel.setText("New person created successfully.");
createButton.setText("New...");
} else if (e.getSource().equals(createButton)
&& createButton.getText().equals("New...")) {
s.setStudentId(new Random().nextInt(Integer.MAX_VALUE) + 1);
s.setFirstName("");
//...
setFieldData(s);
createButton.setText("Save");
} else if (e.getSource().equals(updateButton)) {
if (isEmptyFieldData()) {
msgLabel.setText("Cannot update an empty record");
return;
}
if (bean.update(s) != null)
msgLabel.setText("Person with ID:"
+ String.valueOf(s.getStudentId()
+ " is updated successfully"));
} else if (e.getSource().equals(deleteButton)) {
if (isEmptyFieldData()) {
msgLabel.setText("Cannot delete an empty record");
return;
}
s = bean.getCurrent();
bean.delete();
msgLabel.setText("Person with ID:"
+ String.valueOf(s.getStudentId()
+ " is deleted successfully"));
} else if (e.getSource().equals(firstButton)) {
setFieldData(bean.moveFirst());
} else if (e.getSource().equals(prevButton)) {
setFieldData(bean.movePrevious());
} else if (e.getSource().equals(nextButton)) {
setFieldData(bean.moveNext());
} else if (e.getSource().equals(lastButton)) {
setFieldData(bean.moveLast());
}
}
}
}
SQL commands:
CREATE TABLE Student(
StudentId int (30) NOT NULL,
firstName varchar (30) NOT NULL,
middleName varchar (30) NOT NULL,
lastName varchar (30) NOT NULL,
email varchar (30) NOT NULL,
phone varchar (30) NOT NULL,
PRIMARY KEY (StudentId)
);
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.