0

So I am creating a simple GUI/Database application using JavaFX. It seems really powerful tool.

The problem is with TableView because whenever I create one, It should be of only one type. I've tried multiple different ways to solve the problem but never works, here's some more details :

    public class SecretaryController implements Initializable {

    @Override
    public void initialize(URL url, ResourceBundle resourceBundle) {
    }

    @FXML
    private TableView<?> tableView;

    @FXML
    private TextField txtSearchCustomerId;

    @FXML
    private Button viewctbtn;

    @FXML
    void viewCustomerById(ActionEvent event) {

        tableview = new TableView<Customer>;

        SecretaryModel secretaryModel = new SecretaryModel();

        TableColumn col_id = new TableColumn("Id");
        TableColumn col_fname = new TableColumn("fname");
        TableColumn col_lname = new TableColumn("lname");
        TableColumn col_address = new TableColumn("Id");
        TableColumn col_age = new TableColumn("age");
        TableColumn col_email = new TableColumn("email");
        TableColumn col_password = new TableColumn("password");
        TableColumn col_isPremium = new TableColumn("ispremium");

        tableView.getColumns().addAll(col_id, col_fname, col_lname, col_address, col_age, col_email, col_password, col_isPremium);

        try {

            col_id.setCellValueFactory(new PropertyValueFactory<Customer, Integer>("id"));
            col_fname.setCellValueFactory(new PropertyValueFactory<Customer, String>("fname"));
            col_lname.setCellValueFactory(new PropertyValueFactory<Customer, String>("lname"));
            col_address.setCellValueFactory(new PropertyValueFactory<Customer, String>("address"));
            col_age.setCellValueFactory(new PropertyValueFactory<Customer, String>("age"));
            col_email.setCellValueFactory(new PropertyValueFactory<Customer, String>("email"));
            col_password.setCellValueFactory(new PropertyValueFactory<Customer, String>("password"));
            col_isPremium.setCellValueFactory(new PropertyValueFactory<Customer, String>("ispremium"));
        } catch (Exception e) {
            e.printStackTrace();
        }

        ObservableList<Customer> customerObservableList = secretaryModel.getDataCustomers(Integer.parseInt(txtSearchCustomerId.getText()));

        tableView.setItems(customerObservableList);

    }

It's giving me this error: java: incompatible types: javafx.collections.ObservableList<com.example.taxiandrentapp.Customer> cannot be converted to javafx.collections.ObservableList<capture#1 of ?>

I would like to declare a TableView that can take any parameterized input , and whenever I press on view customer it associate the table view with customer class , and whenever I press on different button , it associate it to a different class.

If this can't be done , any proposed approach will be helpful .

Roy Ly
  • 1
  • 4
  • 1
    This seems similar to [this answer](https://stackoverflow.com/questions/70684972/how-to-solve-error-no-suitable-method-found-for-addalljava-lang-string-meth), i.e. TableView> should be TableView, but from your explanation, I don't understand what you are trying to do. I also have no idea what "cascading" means. If the comment doesn't help, really study this link well, [mcve], if you want assistance. – jewelsea Jan 13 '22 at 08:15
  • Thanks for reference links. all I am trying to do is populate a table for different classes. – Roy Ly Jan 13 '22 at 09:35
  • 2
    But the different classes will have different fields corresponding to different columns in the table, how do you intend to do that without having different table views? I mean I guess you could use reflection to discover the fields in the class, then [dynamically create the table columns from that](https://stackoverflow.com/questions/18941093/how-to-fill-up-a-tableview-with-database-data), but I really don't know if you need to go that meta. Such an approach is certainly nothing like your example code. – jewelsea Jan 13 '22 at 09:59
  • Thank you ! I think this will work because that is my intent to load data from the database. Thank You again . – Roy Ly Jan 13 '22 at 13:42
  • If you're dynamically creating the table from an arbitrary database table, then presumably you wouldn't have a class to represent the data. You would use something like a `TableView>`. I don't see how your code is relevant to what you're asking. If you have a few specific database tables, then create a model class for each one, and just switch out the different `TableView`s. – James_D Jan 13 '22 at 15:51
  • I kinda new to asking questions here , I get your point . – Roy Ly Jan 16 '22 at 11:12

0 Answers0