我想用Java数据库的内容填充JavaFX tableview。我创建了一个可观察列表并在其中添加了表格数据,但我不知道如何在tableview中显示此列表。这是列表代码,
public ObservableList<PatientDto> getAllPatientInfo() {
ObservableList<PatientDto> list = FXCollections.observableArrayList();
PreparedStatement ps = null;
try {
String sql = "select * from patient_record";
ps = DbUtil.getConnection().prepareStatement(sql);
ResultSet rs = ps.executeQuery();
while (rs.next()) {
PatientDto patientDto = new PatientDto();
patientDto.setPid(rs.getInt("PID"));
patientDto.setPatient_Name(rs.getString("Patient_Name"));
patientDto.setAddress(rs.getString("Address"));
patientDto.setAge(rs.getInt("Age"));
patientDto.setDisease(rs.getString("Disease"));
patientDto.setDoctor_Name(rs.getString("Doctor_Name"));
patientDto.setAdmit_Date(rs.getDate("Admit_date"));
patientDto.setDischarge_Date(rs.getDate("Discharge_Date"));
list.add(patientDto);
}
} catch (SQLException e) {
e.printStackTrace();
}
return list;
}
我正在使用UI的场景构建器,这是我的tableview的界面 TableView In scene builder
这是我在mysql数据库中的表的ss。 Table in Mysql server。 我是JavaFX的新手,所以我发现很难实现可用的答案。所以请详细描述。
答案 0 :(得分:0)
首先,您需要一个包含表格信息的模型类,我们将其称为PatientModel
,如下所示:
public class PatientModel {
private StringProperty pid;
private StringProperty patientName;
private StringProperty address;
/* ...
* and so on...
*/
public PatientModel(String pid, String patientName, String address) {
this.pid = new SimpleStringProperty(pid);
this.patientName = new SimpleStringProperty(patientName);
this.address = new SimpleStringProperty(address);
}
public String getPid() {
return pid.get();
}
public StringProperty pidProperty() {
return pid;
}
public String getPatientName() {
return patientName.get();
}
public StringProperty patientNameProperty() {
return patientName;
}
public String getAddress() {
return address.get();
}
public StringProperty addressProperty() {
return address;
}
}
然后您需要一个与此类似的.fxml
文件:
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.control.TableColumn?>
<?import javafx.scene.control.TableView?>
<?import javafx.scene.layout.AnchorPane?>
<AnchorPane prefHeight="500.0" prefWidth="700.0" xmlns="http://javafx.com/javafx/8.0.91"
xmlns:fx="http://javafx.com/fxml/1"
fx:controller="stackoverflow.TestController">
<TableView fx:id="table">
<columns>
<TableColumn fx:id="pidColumn" text="PID"/>
<TableColumn fx:id="nameColumn" text="Patient Name"/>
<TableColumn fx:id="addressColumn" text="Address"/>
<!-- and so on... -->
</columns>
</TableView>
</AnchorPane>
然后是控制器类:
public class TestController implements Initializable {
@FXML
private TableView<PatientModel> table;
@FXML
private TableColumn<PatientModel, String> pidColumn;
@FXML
private TableColumn<PatientModel, String> nameColumn;
@FXML
private TableColumn<PatientModel, String> addressColumn;
private ObservableList<PatientModel> tableData = FXCollections.observableArrayList();
@Override
public void initialize(URL location, ResourceBundle resources) {
initTable();
// if the data is coming from an other class you have probably a list of them,
// if not you can simply populate the tableData
List<PatientDto> dtosFromDB = new ArrayList<>();
tableData.addAll(dtosFromDB.stream().map(this::convertDtoToModel).collect(Collectors.toList()));
}
private void initTable() {
table.setItems(tableData);
pidColumn.setCellValueFactory(data -> data.getValue().pidProperty()); // here is where tha magic happens :)
nameColumn.setCellValueFactory(data -> data.getValue().patientNameProperty()); // this sets the cells value from
// the model.
addressColumn.setCellValueFactory(data -> data.getValue().addressProperty());
}
private PatientModel convertDtoToModel(PatientDto dto) {
return new PatientModel(dto.getPid().toString(), dto.getName(), dto.getAddress()/* and so on */);
}
}
获得这些课程后,您只需从主课程中加载.fxml
,然后您必须完成所有信息,它应该可以正常工作。
P.S。对不起,缩进,我不能在这里只是按行缩进整个代码,这太无聊了。