eniniz
eniniz

Reputation: 37

how to create an empty row in a tableview in javafx

I wonder if there is a way to add data to a tableview by double clicked on an empty row. (In oracle tutorial about TableView, I can modified data by double clicked on it, I want to do the same thing with empty column). If just I could know how to create an empty row, or something like that

Upvotes: 0

Views: 4080

Answers (2)

javaHunter
javaHunter

Reputation: 1097

The following code adds a new Item to the table when a row is double-clicked. You basically have to set the RowFactory of the table.

 myTable.setRowFactory(new Callback<TableView<TableItem>, TableRow<TableItem>>() {

                @Override
                public TableRow<TableItem> call(TableView<TableItem> param) {
                    TableRow<TableItem> row = new TableRow<TableItem>();
                    row.setOnMouseClicked(new EventHandler<MouseEvent>() {

                            @Override
                            public void handle(MouseEvent event) {
                                if ((event.getClickCount() == 2)) {
                                    //create a new Item and intialize it ...
                                    TableItem myItem = new TableItem();
                                    myTable.getItems().add(myItem);
                                } 
                            }

                        });

                    return row;
                }
            });

Upvotes: 0

Mailkov
Mailkov

Reputation: 1231

In TableView the rows are creted from an items list, so for create empty row you simple use this code were Item() is the item of your items list.

tableView.getItems().add(new Item());

Upvotes: 2

Related Questions