0

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;
}
Surya Hardiansyah
  • 121
  • 1
  • 1
  • 6

1 Answers1

2

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.

Community
  • 1
  • 1
jewelsea
  • 150,031
  • 14
  • 366
  • 406
  • Ive shown my TableView definition in my edited question, sir. should I edit my codes just like the instruction above? Thanks for your tips sir, Im still a pure newbie. @jewelsea – Surya Hardiansyah Jun 21 '16 at 06:13
  • I don't know Surya, it depends on if your question is solved. If your program is working after applying the suggestions in my answer, then you can just mark the answer correct and you don't need to edit the question to provide an mcve. If your program is not working for the same cast exception reason, then you might want to edit the question to provide an mcve and somebody might be able to help you with that. If the cast exception is fixed, but your program is still not working, then you might want to create a new question for the new problem and include an mcve in that. – jewelsea Jun 21 '16 at 06:18
  • Im glad you are online @jewelsea. I guess I will give this problem up and change to another method. But the method still doesnt run well. Please kindly help me to solve this one: http://stackoverflow.com/questions/37937125/setoneditcommit-with-iteration-javafx – Surya Hardiansyah Jun 21 '16 at 06:36