0

I'm designing a project about cataloging something. In this project user must be able to create his own table as he wish. Therefore I do not have any static class and instance of it.

I'm creating a diaglog pane and I can create textfields for user inputs according to column names of database table dynamically but how can i add those user's inputs into the tableView ?

As I can add any String input into the ListView can I add user String inputs into tableView columns?

ListView<String> listView = new ListView();
public ObservableList<String> listCatalogNames = FXCollections.observableArrayList();
listCatalogNames.add("Books");

More details with an example;

There is listview that contains all catalog names and according to lisview selection tableview will be created dynamically center of borderpane.

User have books(name, author, page) and movies(name, year, director, genree) catalogs.

Lets say user selected movies and tableView appeared with 4 columns and clicked add button. Diaglog pane created with 4 textfield. I built everything until that point but I cannot add user's input into the tableView because i dont have any static class for Movies or Books etc.

Is there any way to create dynamic class ?

Please give me an idea and help me about that situation.

here is the github link of our project

4

2 回答 2

1

Just use String[] storing the Strings for every column of a row (or a similar data structure) as item type for the TableView. It should be simple enough to create a String[] from the inputs and add it to this TableView:

static TableView<String[]> createTable(String... columnNames) {
    TableView<String[]> table = new TableView<>();

    for (int i = 0; i < columnNames.length; i++) {
        final int index = i;
        TableColumn<String[], String> column = new TableColumn<>(columnNames[i]);
        column.setCellValueFactory(cd -> new SimpleStringProperty(cd.getValue()[index]));
        table.getColumns().add(column);
    }

    return table;
}

Adding a String[] userInputs to a TableView<String[]> table would be done like this:

table.getItems().add(userInputs);

A similar issue (creating a TableView based on the metadata of a ResultSet) can be found here: How to fill up a TableView with database data

于 2018-12-17T00:46:07.797 回答
-1

Easiest solution that comes to my mind is to make use of polymorphism. You can create a super class of both Book and Movie, let's call it Item. Then you can declare your table to contain Item and cast to one of the concrete classes when you need to.

于 2018-12-17T00:21:18.817 回答