我正在使用前端的javafx开发一个供个人使用的小型应用程序。但是,我遇到了这个问题:从数据库中获取的数据不会显示在TableView中。 我使用RavenDB 3.5作为我的数据库。
我的控制器类:
public class FXMLHomePageController implements Initializable {
@FXML
private VBox vBox;
@FXML
private TabPane tabPane;
@FXML
private Tab tabFornecedores, tabGraficos;
@FXML
private AnchorPane anchorPaneFornecedores;
@FXML
private TableView<Fornecedor> tableFornecedores;
@FXML
private TableColumn<Fornecedor, String> colIdFornecedor;
@FXML
private TableColumn<Fornecedor, String> colNomeFantasia;
@FXML
private TableColumn<Fornecedor, String> colCnpjFornecedor;
@FXML
private TableColumn<Fornecedor, String> colEmailFornecedor;
@FXML
private Button btNovoFornecedor;
@FXML
private Button btDetalhesFornecedor;
@FXML
private Button btEditarFornecedor;
@FXML
private Button btRemoverFornecedor;
private FornecedorDAO dao = new FornecedorDAO();
private ObservableList<Fornecedor> fornecedores = FXCollections.observableArrayList(dao.getAll());
@Override
public void initialize(URL location, ResourceBundle resources) {
colIdFornecedor.setCellValueFactory(new PropertyValueFactory<Fornecedor, String>("id"));
colNomeFantasia.setCellValueFactory(new PropertyValueFactory<Fornecedor, String>("nomeFantasia"));
colCnpjFornecedor.setCellValueFactory(new PropertyValueFactory<Fornecedor, String>("cnpj"));
colEmailFornecedor.setCellValueFactory(new PropertyValueFactory<Fornecedor, String>("email"));
tableFornecedores.getItems().addAll(fornecedores);
}
}
我的FXML文件:
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.control.*?>
<?import java.lang.*?>
<?import javafx.scene.layout.*?>
<?import javafx.scene.layout.VBox?>
<VBox fx:id="vBox" maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="600.0" prefWidth="800.0" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1">
<children>
<TabPane fx:id="tabPane" prefHeight="606.0" prefWidth="800.0" tabClosingPolicy="UNAVAILABLE">
<tabs>
<Tab fx:id="tabFornecedores" text="Fornecedores">
<content>
<AnchorPane fx:id="anchorPaneFornecedores" prefHeight="200.0" prefWidth="200.0">
<children>
<TableView fx:id="tableFornecedores" layoutX="28.0" layoutY="79.0" prefHeight="431.0" prefWidth="744.0" AnchorPane.bottomAnchor="61.0" AnchorPane.leftAnchor="28.0" AnchorPane.rightAnchor="28.0" AnchorPane.topAnchor="79.0">
<columns>
<TableColumn fx:id="colIdFornecedor" prefWidth="75.0" text="#" />
<TableColumn fx:id="colNomeFantasia" prefWidth="153.0" text="Nome Fantasia" />
<TableColumn fx:id="colCnpjFornecedor" prefWidth="251.0" text="CNPJ" />
<TableColumn fx:id="colEmailFornecedor" prefWidth="264.0" text="E-mail" />
</columns>
</TableView>
<Button fx:id="btNovoFornecedor" layoutX="622.0" layoutY="21.0" mnemonicParsing="false" prefHeight="40.0" prefWidth="150.0" text="Novo Fornecedor" />
<Button fx:id="btDetalhesFornecedor" layoutX="28.0" layoutY="523.0" mnemonicParsing="false" prefHeight="30.0" prefWidth="160.0" text="Detalhes do Fornecedor" />
<Button fx:id="btEditarFornecedor" layoutX="228.0" layoutY="523.0" mnemonicParsing="false" prefHeight="30.0" prefWidth="160.0" text="Editar Fornecedor" />
<Button fx:id="btRemoverFornecedor" layoutX="429.0" layoutY="523.0" mnemonicParsing="false" prefHeight="30.0" prefWidth="160.0" text="Remover Fornecedor" />
</children>
</AnchorPane>
</content>
</Tab>
<Tab fx:id="tabGraficos" text="Gráficos" />
</tabs>
</TabPane>
</children>
</VBox>
我的DAO档案:
public class FornecedorDAO {
private IDocumentStore store;
private IDocumentSession session;
public FornecedorDAO() {
store = DatabaseConnection.openConnection();
session = store.openSession("easystock");
}
public void save(Fornecedor fornecedor) {
session.store(fornecedor);
session.saveChanges();
}
public void update(Fornecedor fornecedor) {
Fornecedor oldForn = session.load(Fornecedor.class, fornecedor.getId());
oldForn.setNomeFantasia(fornecedor.getNomeFantasia());
oldForn.setCnpj(fornecedor.getCnpj());
oldForn.setEmail(fornecedor.getEmail());
oldForn.setEndereco(fornecedor.getEndereco());
oldForn.setProdutosFornecidos(fornecedor.getProdutosFornecidos());
session.saveChanges();
}
public void remove(Fornecedor fornecedor) {
session.delete(fornecedor.getId());
session.saveChanges();
}
public List<Fornecedor> getAll() {
return session.query(Fornecedor.class).take(Integer.MAX_VALUE).toList();
}
public List<Fornecedor> getByName(String name) {
QFornecedor query = QFornecedor.fornecedor;
return session.query(Fornecedor.class)
.where(query.nomeFantasia.contains(name))
.toList();
}
}
POJO班级:
@QueryEntity
public class Fornecedor {
private String id;
private String nomeFantasia;
private String cnpj;
private String email;
private Endereco endereco;
private List<Produto> produtosFornecidos;
// getters & setters
}
申请类:
public class Main extends Application {
@Override
public void start(Stage stage) throws Exception {
Parent root = FXMLLoader.load(getClass().getResource("/fxml/HomePage.fxml"));
Scene scene = new Scene(root);
stage.setScene(scene);
stage.setResizable(false);
stage.show();
}
public static void main(String[] args) {
launch(args);
}
}
答案 0 :(得分:0)
您的FXML文件缺少fx:controller
属性。你需要:
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.control.*?>
<?import java.lang.*?>
<?import javafx.scene.layout.*?>
<?import javafx.scene.layout.VBox?>
<VBox fx:controller="my.package.FXMLHomePageController" fx:id="vBox" maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="600.0" prefWidth="800.0" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1">
<!-- ... -->
</VBox>
其中my.package
是包含FXMLHomePageController
类的包的名称。