vsarunov
vsarunov

Reputation: 1547

JavaFX TabPane remove/removeAll does not work

Good day, I have a problem when trying to remove tab's from a TabPane in JavaFX. First thing I do I pass an ArrayList of String's with selected tab names from the list to the method which suppose to delete the corresponding by name tabs from the TabPane:

removeBranch.setOnAction(e->{
            ArrayList<String>indexes=new ArrayList(list.getSelectionModel().getSelectedItems());

            entryShifts.removeBranch(indexes);
            list.getItems().removeAll(indexes);

        });

Afterwards I try to delete them like this in the mentioned method:

public void removeBranch(ArrayList<String> branch){
           pane.getTabs().removeAll(branch);
    }

However it does not work, even if I do something like(Please notice I pass only the first name from the list):

public void removeBranch(ArrayList<String> branch){
        pane.getTabs().remove(branch.get(0));
    }

But if I do something like this, it works (here I only remove the first name from the ArrayList as well):

public void removeBranch(ArrayList<String> branch){
        pane.getTabs().forEach(i -> {
            if (i.getText().equals(branch.get(0))) {
                Platform.runLater(() -> {
                    pane.getTabs().remove(i);
                });
            }
        });
    }

Why is this happening? Am I doing something wrong?

P.S the way I fixed it by looping through the ArrayList and passing each time a different name to the method:

indexes.forEach(i->{entryShifts.removeBranch(i);});

public void removeBranch(String branch){
        pane.getTabs().forEach(i -> {
            if (i.getText().equals(branch)) {
                Platform.runLater(() -> {
                    pane.getTabs().remove(i);
                });
            }
        });
    }

Upvotes: 1

Views: 1177

Answers (1)

Itai
Itai

Reputation: 6921

Your problem is that pane.getTabs() returns a list of Tab, and you are trying to find Strings in it. You need a way to convert the strings (Tab names?) to the actual Tab nodes in the TabPane.

One such way is to have a Map<String, Tab> map which you initialize with the tab names and tabs, and then do something like:

List<Tab> tabs = branch.stream().map(map::get).collect(Collectors.toList());
pane.getTabs().removeAll(tabs);

Upvotes: 3

Related Questions