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

Emre SERBEST
  • 115
  • 1
  • 1
  • 8

2 Answers2

3

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

fabian
  • 80,457
  • 12
  • 86
  • 114
  • Thank you for the answer, I just need to go one more step let say I stored user's input respectively into userInputs[] array how can I add into the tableView? – Emre SERBEST Dec 17 '18 at 01:08
  • Added a little explanation to the answer. – fabian Dec 17 '18 at 01:12
  • I owe you a lot, fabian you rescued me from the hell ! Thank you so much ! I'm getting the tableview selected item like this String[] mySelection = tableView.getSelectionModel().getSelectedItem(); Finally I did something on my own. – Emre SERBEST Dec 17 '18 at 01:26
-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.

Gnas
  • 698
  • 1
  • 6
  • 14
  • 1
    Let me clarify it again the user may creates variety of catalogs and those catalogs may contain variety of attributes(columns) and data types (int,string,double,date). So could you please tell me **how should I implement that super class**? What should I define in it ? – Emre SERBEST Dec 17 '18 at 00:40
  • Do you have a known limited number of possible classes or are the classes created at run time by the user? If it's the latter case you're in for a minefield and I think you should avoid it, but if insist you can do a quick search on how to create classes at runtime. – Gnas Dec 17 '18 at 00:40