Доброго вемени суток!
Пишу простое приложение JavaFX+Базы данных: Типа управление школой, решил попрактиковаться
Столкнулся с такой проблемой:
Есть фрейм с TableView, в котором отображаются данные с БД(список студентов), с этим проблем не возникло.
Далее я реализовал событие выделения строки(пока что когда нажимаешь на строку в лейбле окна выводится uid студента).
А теперь интересное:
Создал окно для редактирования и хочу чтобы после виделения строки и нажатия кнопки "Редактировать" у меня открылся фрейм с уже заполненными полями. Вопрос как передать обьект студента в окно редактирования. Пробовал разными путями, но все выдавали либо ошибку компиляции, либо NullPointerException, пробовал передавать uid - тоже NullPointerException.
Вот код класса списка студентов:
package com.gui;
import java.io.IOException;
import java.net.URL;
import java.util.ResourceBundle;
import com.bean.Student;
import com.classes.App;
import com.implementation.StudentImpl;
import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;
import javafx.scene.control.cell.PropertyValueFactory;
public class StudentListFrame {
@FXML
private ResourceBundle resources;
@FXML
private URL location;
@FXML
private Button btn_AddStudent;
@FXML
private Button btn_DeleteStudent;
@FXML
private Button btn_EditStudent;
@FXML
private Button btn_refreshStudentList;
@FXML
private Label lbl_testOuttext;
@FXML
private TableView<Student> tbl_studentTable;
@FXML
private TableColumn<Student, Integer> tblcolumn_id;
@FXML
private TableColumn<Student, String> tblcolumn_address;
@FXML
private TableColumn<Student, String> tblcolumn_codename;
@FXML
private TableColumn<Student, String> tblcolumn_dateofbirth;
@FXML
private TableColumn<Student, String> tblcolumn_email;
@FXML
private TableColumn<Student, String> tblcolumn_grade;
@FXML
private TableColumn<Student, String> tblcolumn_name;
@FXML
private TableColumn<Student, String> tblcolumn_phone;
@FXML
private TableColumn<Student, String> tblcolumn_secname;
@FXML
private TableColumn<Student, String> tblcolumn_surname;
@FXML
private TableColumn<Student, String> tblcolumn_sex;
@FXML
void initialize() {
tblcolumn_id.setCellValueFactory(new PropertyValueFactory<Student, Integer>("id"));
tblcolumn_surname.setCellValueFactory(new PropertyValueFactory<Student, String>("surName"));
tblcolumn_name.setCellValueFactory(new PropertyValueFactory<Student, String>("name"));
tblcolumn_secname.setCellValueFactory(new PropertyValueFactory<Student, String>("secName"));
tblcolumn_codename.setCellValueFactory(new PropertyValueFactory<Student, String>("codeName"));
tblcolumn_dateofbirth.setCellValueFactory(new PropertyValueFactory<Student, String>("dateOfBirth"));
tblcolumn_address.setCellValueFactory(new PropertyValueFactory<Student, String>("address"));
tblcolumn_phone.setCellValueFactory(new PropertyValueFactory<Student, String>("telephone"));
tblcolumn_email.setCellValueFactory(new PropertyValueFactory<Student, String>("email"));
tblcolumn_sex.setCellValueFactory(new PropertyValueFactory<Student, String>("sex"));
tblcolumn_grade.setCellValueFactory(new PropertyValueFactory<Student, String>("grade"));
// tbl_studentTable.setItems(studentImpl.findAllStudents());
// tblcolumn_surname.setCellValueFactory(new PropertyValueFactory<Student,
// String>(""));
selectForEdit();
}
@FXML
void openAddSudentFrame(ActionEvent event) throws IOException {
App.setRoot("add_studentframe");
}
@FXML
void refreshStudentList(ActionEvent event) {
StudentImpl studentImpl = new StudentImpl();
tbl_studentTable.setItems(studentImpl.findAllStudents());
}
@FXML
void editSelectedRow(ActionEvent event) throws IOException {
App.setRoot("edit_studentframe");
}
public String uuid = null;
String selectForEdit() {
TableView.TableViewSelectionModel<Student> selectionModel = tbl_studentTable.getSelectionModel();
selectionModel.selectedItemProperty().addListener(new ChangeListener<Student>() {
@Override
public void changed(ObservableValue<? extends Student> arg0, Student oldVal, Student newVal) {
if (newVal != null) {
uuid = newVal.getUid();
lbl_testOuttext.setText(newVal.getUid());
// studentToEditList.add(newVal);
} else {
lbl_testOuttext.setText("null");
}
}
});
return uuid;
}
}
А вот код окна редактирования:
package com.gui;
import java.io.IOException;
import java.net.URL;
import java.util.ResourceBundle;
import com.bean.Student;
import com.classes.App;
import com.implementation.StudentImpl;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.scene.control.Button;
import javafx.scene.control.ChoiceBox;
import javafx.scene.control.TextField;
import javafx.scene.input.MouseEvent;
public class EditStudentFrame {
StudentListFrame studentListFrame = new StudentListFrame();
StudentImpl studentImpl = new StudentImpl();
String[] gradeLetters = { "A", "B", "C", "D" };
String[] gradeNumbers = { "1", "2", "3", "5", "6", "7", "8", "9", "10", "11", "12" };
String[] sexStatus = { "None", "male", "female" };
@FXML
private ResourceBundle resources;
@FXML
private URL location;
@FXML
private Button btn_studentFormCANCEL;
@FXML
private Button btn_studentFormOK;
@FXML
private ChoiceBox<?> chb_studentClassTeacher;
@FXML
private ChoiceBox<String> chb_studentGradeCircuit;
@FXML
private ChoiceBox<String> chb_studentGradeLetter;
@FXML
private ChoiceBox<String> chb_studentSex;
@FXML
private TextField tf_studentAddress;
@FXML
private TextField tf_studentCodeName;
@FXML
private TextField tf_studentDateOfBirth;
@FXML
private TextField tf_studentEmail;
@FXML
private TextField tf_studentName;
@FXML
private TextField tf_studentPhone;
@FXML
private TextField tf_studentSecname;
@FXML
private TextField tf_studentSurname;
@FXML
void cancelAddStudentFrame(ActionEvent event) throws IOException {
App.setRoot("studentlistframe");
}
@FXML
void createRecordInDB(ActionEvent event) {
}
@FXML
void fillTheFieldCodeName(MouseEvent event) {
String studentSurname = tf_studentSurname.getText().isEmpty() ? ""
: tf_studentSurname.getText().substring(0, 1).toUpperCase()
+ tf_studentSurname.getText().substring(1);
String studentName = tf_studentName.getText().isEmpty() ? ""
: tf_studentName.getText().substring(0, 1).toUpperCase() + ".";
String studentSecName = tf_studentSecname.getText().isEmpty() ? ""
: tf_studentSecname.getText().substring(0, 1).toUpperCase() + ".";
tf_studentCodeName.setText(studentSurname + " " + studentName + " " + studentSecName);
}
@FXML
void initialize() {
chb_studentGradeLetter.getItems().addAll(gradeLetters);
chb_studentGradeCircuit.getItems().addAll(gradeNumbers);
chb_studentSex.getItems().addAll(sexStatus);
Student student;
tf_studentSurname.setText(student.getSurName());
tf_studentName.setText(student.getName());
tf_studentSecname.setText(student.getSecName());
tf_studentCodeName.setText(student.getCodeName());
tf_studentDateOfBirth.setText(student.getDateOfBirth());
tf_studentAddress.setText(student.getAddress());
tf_studentPhone.setText(student.getTelephone());
chb_studentSex.setValue(student.getSex());
chb_studentGradeCircuit.setValue(student.getGrade().substring(0, 1));
chb_studentGradeLetter.setValue(student.getGrade().substring(1));
}
}
}
PS: если нужна доп. информация по реализации остальных компонентов, пишите.