首页 > 解决方案 > Table View Javafx 无法正确显示数据。表中的数据是不可见的

问题描述

这几天找不到ma JavaFX table view显示数据不正确的原因。当我开始程序时,我的表看起来 比填充表

当我按下“更新表格”按钮时,表格看起来: 照顾

我创建了带有过滤器的方法,我可以在其中搜索表中的数据。当我试图找到一些我知道存在于数据库中的单词时,它会找到它但不会再次显示。例如,我将我的电子邮件地址放入数据库:dpopov88@me.com,当我将其写入搜索文本字段时,表格视图中没有任何变化,但如果我尝试查找并在 5 个字母表显示“无内容”后特别出错表中”。 无内容

用户等级:

package application.controllers;
import javafx.beans.property.SimpleStringProperty;

public class Users {

    private SimpleStringProperty id;
    private SimpleStringProperty fn;
    private SimpleStringProperty ln;
    private SimpleStringProperty lo;
    private SimpleStringProperty pa;
    private SimpleStringProperty em;
    private SimpleStringProperty ro;

    public Users(String id, String firstName, String lastName, String login, String password, String email, String role) {
        this.id = new SimpleStringProperty(id);
        this.fn = new SimpleStringProperty(firstName);
        this.ln = new SimpleStringProperty(lastName);
        this.lo = new SimpleStringProperty(login);
        this.pa = new SimpleStringProperty(password);
        this.em = new SimpleStringProperty(email);
        this.ro = new SimpleStringProperty(role);
    }   
    public String getId() {
        return id.get();
    }

    public void setId(String id) {
        this.id.set(id);
    }
    public String getFn() {
        return fn.get();
    }
    public void setFn(String fn) {
        this.fn.set(fn);
    }
    public String getLn() {
        return ln.get();
    }
    public void setLn(String ln) {
        this.ln.set(ln);
    }
    public String getLo() {
        return lo.get();
    }
    public void setLo(String lo) {
        this.lo.set(lo);
    }
    public String getPa() {
        return pa.get();
    }
    public void setPa(String pa) {
        this.pa.set(pa);
    }
    public String getEm() {
        return em.get();
    }
    public void setEm(String em) {
        this.em.set(em);
    }
    public String getRo() {
        return ro.get();
    }
    public void setRo(String ro) {
        this.ro.set(ro);
    }

管理员视图控制器类:

package application.adminview;

import java.net.URL;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ResourceBundle;
import application.controllers.ConnectionToSQL;
import application.controllers.Users;
import javafx.beans.property.SimpleStringProperty;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.collections.transformation.FilteredList;
import javafx.collections.transformation.SortedList;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.Button;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;
import javafx.scene.control.TextField;
import javafx.scene.input.KeyEvent;
import javafx.scene.layout.GridPane;
import javafx.scene.layout.Pane;
import javafx.scene.layout.StackPane;

public class AdministratorViewController implements Initializable {

    @FXML
    private Button employeeUpdateButton;
    @FXML
    private TextField employeeSearchField;
    @FXML
    TableView<Users> employeeTableView;
    @FXML
    TableColumn<Users, String> id;
    @FXML
    TableColumn<Users, String> firstName;
    @FXML
    TableColumn<Users, String> lastName;
    @FXML
    TableColumn<Users, String> login;
    @FXML
    TableColumn<Users, String> password;
    @FXML
    TableColumn<Users, String> email;
    @FXML
    TableColumn<Users, String> role;
    ObservableList<Users> data;
    FilteredList<Users> filteredData;

    @Override
    public void initialize(URL arg0, ResourceBundle arg1) {
        employeeTableView.setItems(data);
        id.setCellValueFactory( c-> new SimpleStringProperty(c.getValue().getId()));
        firstName.setCellValueFactory(c-> new SimpleStringProperty(c.getValue().getFn()));
        lastName.setCellValueFactory(c-> new SimpleStringProperty(c.getValue().getLn()));
        login.setCellValueFactory(c-> new SimpleStringProperty(c.getValue().getLo()));
        password.setCellValueFactory(c-> new SimpleStringProperty(c.getValue().getPa()));
        email.setCellValueFactory(c-> new SimpleStringProperty(c.getValue().getEm()));
        role.setCellValueFactory(c-> new SimpleStringProperty(c.getValue().getId()));
        employeeTableView.setItems(data);
        mainGridPane.toFront();
        mainPane.toFront();
        employeeTableView.setFixedCellSize(1);

    @FXML
    private void loadDataToEmployeeTableView(ActionEvent event) {
        try {
            data = FXCollections.observableArrayList();
            ResultSet rs = ConnectionToSQL.getConnection().createStatement().executeQuery("SELECT * FROM users");
            while (rs.next()) {
                data.add(new Users(rs.getString(1), rs.getString(2), rs.getString(3), rs.getString(4), rs.getString(5),
                        rs.getString(6), rs.getString(7)));
                filteredData = new FilteredList<>(data, e -> true);
                System.out.println(rs.getString("idusers") + rs.getString("FirstName") + rs.getString("LastName")
                        + rs.getString("Login") + rs.getString("Password") + rs.getString("Email")
                        + rs.getString("Role"));
            }
            employeeTableView.setItems(data);
        } catch (SQLException exc) {
            System.err.println("Error" + exc);
        }
        System.out.println("Done");
        employeeTableView.setItems(data);
    }

方法启动后的变量 ObservableList 数据存储来自 DB 的数据。我检查了它。

  <TableView fx:id="employeeTableView" prefHeight="200.0" prefWidth="200.0" GridPane.rowIndex="1">
                     <columnResizePolicy>
                        <TableView fx:constant="CONSTRAINED_RESIZE_POLICY" />
                     </columnResizePolicy>
                     <columns>
                        <TableColumn fx:id="id" prefWidth="75.0" text="id" />
                        <TableColumn fx:id="firstName" prefWidth="75.0" text="firstName" />
                        <TableColumn fx:id="lastName" prefWidth="75.0" text="lastName" />
                        <TableColumn fx:id="login" prefWidth="75.0" text="login" />
                        <TableColumn fx:id="password" prefWidth="75.0" text="password" />
                        <TableColumn fx:id="email" prefWidth="75.0" text="email" />
                        <TableColumn fx:id="role" prefWidth="75.0" text="role" />
                     </columns></TableView>

标签: javamysqldatabaseuitableviewjavafx

解决方案


推荐阅读