我正在开发学校管理层的应用程序 JavaFX 。我需要从数据库中加载教师列表;每行都是一个HBox,其中包含一个ImageView和文本标签(NameOfTeacher,Degree)。
我想知道如何循环播放相同的HBox(具有SceneBuilder中创建的相同样式),如屏幕快照1所示,并为每个HBox插入正确的数据库数据吗?
这是我的应用程序的屏幕截图:
Screenshot of App in SceneBuilder
我的课程:
TeacherController.java
package Controllers;
import Models.DB_Connexion;
import Models.Teacher;
import java.net.URL;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;
import java.util.ResourceBundle;
import java.util.logging.Level;
import java.util.logging.Logger;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.Node;
import javafx.scene.layout.HBox;
import javafx.scene.layout.VBox;
import javafx.scene.text.Text;
public class TeacherController implements Initializable {
Connection connection = null;
Statement statement = null;
ResultSet result = null;
@FXML
private HBox Hbox_teacher;
@FXML
private Text Hbox_name_teacher;
@FXML
private Text Hbox_teacher_formation;
@FXML
private VBox VBox_teachers;
@FXML
private VBox Vbox_list;
/**
* Initializes the controller class.
*/
@Override
public void initialize(URL url, ResourceBundle rb) {
try {
getTeachers();
} catch (ClassNotFoundException ex) {
Logger.getLogger(TeacherController.class.getName()).log(Level.SEVERE, null, ex);
} catch (SQLException ex) {
Logger.getLogger(TeacherController.class.getName()).log(Level.SEVERE, null, ex);
}
}
private void getTeachers() throws ClassNotFoundException, SQLException {
connection = DB_Connexion.DBcnx();
statement = connection.createStatement();
String query = "SELECT * FROM teachers";
result = statement.executeQuery(query);
while(result.next()){
// Something Here !!
teacher_name.setText(result.getString("name");
degree.setText(result.getString("degree");
}
}
}
和一个包含getter和setter的模型类 Teacher.java 。