JavaFX ObservableListWrapper无法强制转换

时间:2016-06-20 18:03:28

标签: javafx tableview

我需要将数据从数据库插入到表中。

但我得到了" com.sun.javafx.collections.ObservableListWrapper无法转发给AdminSide.Adminsupervisor"。

这是我的方法:

public void captureDataSuper() {
    ObservableList<ObservableList> admsup;
    admsup = FXCollections.observableArrayList();
    Connection c;
    try {
        c = KonekDB.createConnection();
        String SQL = "SELECT * from adminsupervisor";
        ResultSet rs = c.createStatement().executeQuery(SQL);

        TableColumn<Adminsupervisor, String> id = new TableColumn<Adminsupervisor, String>("ID");
        TableColumn<Adminsupervisor, String> user = new TableColumn<Adminsupervisor, String>("Username");
        TableColumn<Adminsupervisor, String> pass = new TableColumn<Adminsupervisor, String>("Password");
        TableColumn<Adminsupervisor, String> userType = new TableColumn<Adminsupervisor, String>("User Type \n(1=Admin 2=Supervisor)");

        id.setCellFactory(TextFieldTableCell.<Adminsupervisor>forTableColumn());
        user.setCellFactory(TextFieldTableCell.<Adminsupervisor>forTableColumn());
        pass.setCellFactory(TextFieldTableCell.<Adminsupervisor>forTableColumn());
        userType.setCellFactory(TextFieldTableCell.<Adminsupervisor>forTableColumn());

        userType.setPrefWidth(200);

        id.setCellValueFactory(new Callback<CellDataFeatures<Adminsupervisor, String>, ObservableValue<String>>() {
            public ObservableValue<String> call(CellDataFeatures<Adminsupervisor, String> p) {
                return new SimpleStringProperty(p.getValue().getId());
            }
        });

        user.setOnEditCommit(
                new EventHandler<CellEditEvent<Adminsupervisor, String>>() {

            public void handle(CellEditEvent<Adminsupervisor, String> t) {
                ((Adminsupervisor) t.getTableView().getItems().get(
                        t.getTablePosition().getRow())).setUsername(t.getNewValue());
            }
        }
        );
        user.setCellValueFactory(new Callback<CellDataFeatures<Adminsupervisor, String>, ObservableValue<String>>() {
            public ObservableValue<String> call(CellDataFeatures<Adminsupervisor, String> p) {
                return new SimpleStringProperty(p.getValue().getUsername());
            }
        });

        pass.setOnEditCommit(
                new EventHandler<CellEditEvent<Adminsupervisor, String>>() {

            public void handle(CellEditEvent<Adminsupervisor, String> t) {
                ((Adminsupervisor) t.getTableView().getItems().get(
                        t.getTablePosition().getRow())).setPassword(t.getNewValue());
            }
        }
        );
        pass.setCellValueFactory(new Callback<CellDataFeatures<Adminsupervisor, String>, ObservableValue<String>>() {
            public ObservableValue<String> call(CellDataFeatures<Adminsupervisor, String> p) {
                return new SimpleStringProperty(p.getValue().getUsername());
            }
        });

        userType.setOnEditCommit(
                new EventHandler<CellEditEvent<Adminsupervisor, String>>() {

            public void handle(CellEditEvent<Adminsupervisor, String> t) {
                ((Adminsupervisor) t.getTableView().getItems().get(
                        t.getTablePosition().getRow())).setUserType(t.getNewValue());
            }
        }
        );
        userType.setCellValueFactory(new Callback<CellDataFeatures<Adminsupervisor, String>, ObservableValue<String>>() {
            public ObservableValue<String> call(CellDataFeatures<Adminsupervisor, String> p) {
                return new SimpleStringProperty(p.getValue().getUserType().toString());
            }
        });

        supervisorTable.getColumns().addAll(id, user, pass, userType);

        while (rs.next()) {
            //Iterate Row
            ObservableList<String> row = FXCollections.observableArrayList();
            for (int i = 1; i <= 4; i++) {
                //Iterate Column
                row.add(rs.getString(i));
            }
            admsup.add(row);
        }

        supervisorTable.setItems(admsup);
    } catch (Exception e) {
        e.printStackTrace();
        System.out.println("Error on Building Data");
    }
}

这是我的模型类Adminsupervisor:

public class Adminsupervisor {

private String id;
private String username;
private String password;
private String userType;

public String getId() {
    return id;
}

public String getUsername() {
    return username;
}

public String getPassword() {
    return password;
}

public String getUserType() {
    return userType;
}

public void setId(String id) {
    this.id = id;
}

public void setUsername(String username) {
    this.username = username;
}

public void setPassword(String password) {
    this.password = password;
}

public void setUserType(String userType) {
    this.userType = userType;
}

void set(int j, String newValue) {
    for (j = 0; j < 4; j++) {
        if (j == 0) {
            setId(newValue);
        }
        if (j == 2) {
            setPassword(newValue);
        }
        if (j == 3) {
            setUserType(newValue);
        }
        if (j == 1) {
            setUsername(newValue);
        }
    }
    try {
        Connection c = KonekDB.createConnection();
        //SQL FOR SELECTING ALL OF CUSTOMER
        String SQL = "UPDATE adminsupervisor SET "
                + "username=" + username + ","
                + "password=" + password + ","
                + "userType=" + userType + " WHERE id=" + id + "";
        //ResultSet
        c.createStatement().executeUpdate(SQL);
    } catch (Exception e) {
        e.printStackTrace();
        System.out.println("Error on Building Data");
    }
}}

如果您找到解决方案并告诉我应该编辑哪一行,那将是很棒的。谢谢:))

编辑:这是我的表构造函数:

private TableView supervisorTable() {
    TableView table = new TableView();
    table.setEditable(true);
    return table;
}

1 个答案:

答案 0 :(得分:2)

我猜你的错误发生在这一行:

supervisorTable.setItems(admsup);

您没有显示supervisorTable的定义,但我猜它是TableView<Adminsupervisor>。这意味着您设置到表中的项目必须是ObservableList<Adminsupervisor>类型。但是,您的admsup列表定义为ObservableList<ObservableList>

你需要:

  1. 将您的admsup列表类型更改为ObservableList<Adminsupervisor>
  2. 编写一个将ObservableList转换为Adminsupervisor public AdminSupervisor convert(ObservableList values)的函数。
  3. 在将项目添加到列表admsup.add(convert(row))之前调用转换函数。
  4. 另请阅读:

    如果您创建mcve并且{{3}}是独立的,并且有人可以复制并粘贴以运行以复制问题,则这些问题更容易回答(在这种情况下,mcve将存根所有数据库)码)。此外,当您在问题中列出异常时,请始终尝试提供异常的完整堆栈跟踪,并在问题源代码中的注释中指出错误原因。