有几天我在majavafx表视图中找不到错误显示数据的原因。当我开始编程时,我的表看起来比人口统计表更重要
当我按下“更新表格”按钮时,表格显示:look after
我用filter创建了一个方法,我可以在其中搜索表中的数据。当我试图在数据库中找到某个我知道存在的单词时,它会找到它,但不会再显示出来。例如,我把我的电子邮件地址: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);
}
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);
}
方法启动后的变量observeList数据存储数据库中的数据。我查过了。
<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>
暂无答案!
目前还没有任何答案,快来回答吧!