Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Remove columns from table view javafx

I have a table view in javafx and I add dynamically the columns because I don't know the number of the columns. I want when I click in a button all the rows and columns of the tableView to be removed. I have the code below. I want before I add new data in the table to remove all the columns from it. I tried a lot of things that I found online but nothing worked. Thanks.

public void handleEventsClick(){
    String tableName = (String) events.getSelectionModel().getSelectedItem();

    ObservableList<ObservableList> transitionEvents = FXCollections.observableArrayList();

    tableColumns = new TableColumn[traEv.size()];

    int trEvSize = 0;

    for(int i = 0; i < traEv.size();i++){
        ArrayList<TransitionSubsequencePair> tep = traEv.get(i); 

        TableColumn tc = new TableColumn("TrEv:" + i);
        tableColumns[i] = tc;
            final int j = i;
            tc.setCellValueFactory(new Callback<CellDataFeatures<ObservableList,String>,ObservableValue<String>>(){                    
                public ObservableValue<String> call(CellDataFeatures<ObservableList, String> param) {                                                                                              
                    return new SimpleStringProperty(param.getValue().get(j).toString());                        
                }                    
            });
            eventsTableView.getColumns().addAll(tc);
    }

    ObservableList<String> row = FXCollections.observableArrayList();
    row.clear();
    for(int i = 0; i < traEv.size();i++){
        ArrayList<TransitionSubsequencePair> tep = traEv.get(i); 

        ArrayList<String> seenTables = new ArrayList<String>();
        for(TransitionSubsequencePair tre: tep){
            System.out.println(tre.getTable());
            String lala = "";
            if(tre.getTable().equals(tableName) && (!seenTables.contains(tre.getTable()) || seenTables.isEmpty())){
                System.out.println(i);
                lala += tre.getTransition() + ":" + tre.getSubsequence() + ",";
                row.add(lala);
                seenTables.add(tre.getTable());
            }
            System.out.println("--"+lala);
        }


    }
    transitionEvents.add(row);


    eventsTableView.setItems(transitionEvents); // finally add data to tableview


}
like image 631
Thanos Pappas Avatar asked Jul 13 '15 14:07

Thanos Pappas


People also ask

How to add columns in TableView in JavaFX?

The columns are created by using the TableColumn class. The getColumns method of the TableView class adds the previously created columns to the table. In your applications, you can use this method to dynamically add and remove columns.

How to sort TableView in JavaFX?

The JavaFX TableView enables you to sort the rows in the TableView. There are two ways rows can be sorted. The first way is for the user to click on the TableColumn header cell (where the column title is displayed). This will sort the rows in the TableView after the values of that column.


1 Answers

To remove all data:

eventsTableView.getItems().clear();

To remove all columns:

eventsTableView.getColumns().clear();
like image 162
James_D Avatar answered Sep 28 '22 00:09

James_D