spoke
spoke

Reputation: 267

JavaFX: add row to table

I want to add rows to a table depending on an ID i get earlier. I have managed to do it in SWT but I do not know how to do it in javaFx.

Here is the code I wrote for SWT:

int availableDesings = designManagement.getNumberOfDesigns();
ArrayList<Design> designs = designManagement.getDesignArray();
    for (int i = 0 ; i< availableDesings ; i++){
        TableItem item = new TableItem(table, SWT.NONE);
        item.setText (0, String.valueOf(designs.get(i).getId()));
        item.setText (1, String.valueOf(designs.get(i).getPart_name()));
    }

Can anyone help me translating the code to javaFx?

Upvotes: 1

Views: 6242

Answers (3)

persistent_poltergeist
persistent_poltergeist

Reputation: 564

you can actually add row to a tableview in javafx though i haven't tried using it. But i have done it with TableColumn. This is some code that i have done for my small project

    TableView<Person> personTable = new TableView<>();
    TableColumn<Person,String> nameColumn=new TableColumn<>("Name");
    nameColumn.setMinWidth(200);
    nameColumn.setCellValueFactory(new PropertyValueFactory<>("Name"));
    TableColumn<Person, Gender> genderColumn=new TableColumn<>("Gender");
    genderColumn.setMinWidth(30);
    genderColumn.setCellValueFactory(new PropertyValueFactory<>("gender"));
    TableColumn<Person,String> mobileNumberColumn=new TableColumn<>("mobileNumber");
    mobileNumberColumn.setMinWidth(150);
    mobileNumberColumn.setCellValueFactory(new PropertyValueFactory<>("mobileNumber"));
    TableColumn<Person, Blood> bloodColumn=new TableColumn<>("Blood Type");
    bloodColumn.setMinWidth(30);
    bloodColumn.setCellValueFactory(new PropertyValueFactory<Person, Blood>("blood"));
    personTable.setItems(getPerson());//setting content retrieved from the database into the table
    personTable.getColumns().addAll(nameColumn, genderColumn, mobileNumberColumn,bloodColumn);//adding all columns to table

so what it means is when i say TableColumn mobileNumberColumn=new TableColumn<>("mobileNumber"); i mean mobileNumberColumn is using the class Person from which a string is assigned to the column. the mobile number in quotes is the name of the table.

mobileNumberColumn.setCellValueFactory(new PropertyValueFactory<>("mobileNumber")); The above line say that the value of the column is the instance variable mobileNumber of the the class person.But for this to work you should have a getMobileNumber() method in person class. It has to be exactly like that for the tableView to work. hope this helps

Upvotes: 0

ItachiUchiha
ItachiUchiha

Reputation: 36722

If you have a TableView in JavaFX and it is assigned to a ObservableList of items :

TableView<String> table = new TableView<>();
ObservableList<String> list = FXCollections.observableArrayList();
table.setItems(list);

Then you can add data to the Table, by adding data to the list :

table.getItems().add("New Item");

or, you can directly add data to the list :

list.add("New Item");

For more, information, check this link :

Adding New Rows

Upvotes: 4

griFlo
griFlo

Reputation: 2164

    private ObservableList<Design> data = FXCollections.observableArrayList();

.....

....

    data.addAll(designManagement.getDesignArray()); //all you items

    TableView<Design> table = new TableView<>();
    TableColumn<Design, String> column1 = new TableColumn<>();
    column1.setCellValueFactory(new PropertyValueFactory<Design, String>("id"));
    TableColumn<Design, String> column2 = new TableColumn<>();
    column2.setCellValueFactory(new PropertyValueFactory<Design, String>("part_name"));
    table.getColumns().add(column1);
    table.getColumns().add(column2);

    table.setItems(data);

Upvotes: 0

Related Questions