Surya Hardiansyah
Surya Hardiansyah

Reputation: 141

JavaFX ObservableListWrapper cannot be cast

I need to insert data from database to table.

But i got "com.sun.javafx.collections.ObservableListWrapper cannot be cast to AdminSide.Adminsupervisor".

This is my method:

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

This is my model class 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");
    }
}}

It would be great of you if you find the solution and tell me which line should be edited. Thank you :)

EDIT: Here is my Table constructor:

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

Upvotes: 0

Views: 1521

Answers (1)

jewelsea
jewelsea

Reputation: 159351

I'm guessing your error occurs on this line:

supervisorTable.setItems(admsup);

You don't show the definition of supervisorTable, but I'll guess that it is TableView<Adminsupervisor>. That would mean that the items that you set to the table would need to be of type ObservableList<Adminsupervisor>. However, your admsup list is defined as ObservableList<ObservableList>.

You need to:

  1. Change the type of your admsup list to ObservableList<Adminsupervisor>.
  2. Write a function which converts an ObservableList to an Adminsupervisor public AdminSupervisor convert(ObservableList values).
  3. Call the conversion function before you add items to your list admsup.add(convert(row)).

Also read up on:

These kind of questions are easier to answer if you create and mcve, which is standalone and somebody could just copy and paste to run to replicate the issue (in this case the mcve would stub out all of the database code). Also, when you have an exception listed in your question, always try to provide the complete stack trace of the exception and indicate in a comment in the question source code where the cause of the error is.

Upvotes: 2

Related Questions