表格视图Javafx无法正确显示数据。表中的数据不可见

时间:2018-08-09 09:54:15

标签: java mysql database uitableview javafx

几天来,我找不到JavaFX表格视图中显示数据不正确的原因。当我启动程序时,我的桌子看起来 Befor popullating table

当我按下“更新表”按钮时,表看起来: Look after

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

用户类别:

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);
    }

AdministratorViewController类:

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数据存储数据库中的数据。我检查了。

  <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>

0 个答案:

没有答案