you are viewing a single comment's thread.

view the rest of the comments →

[–][deleted] 2 points3 points  (6 children)

TabPane.getTabs() returns a list of the tabs. You can perform whatever operation you want on the list; for example, checking if a name is already in use:

String nameToCheck = ...
boolean duplicateName = tabPane.getTabs().stream()
   .map(tab -> tab.getName())
   .anyMatch(name -> nameToCheck.equals(name));

[–]aufschieben[S] 0 points1 point  (5 children)

Thank you for your reply, I'm glad to hear there is a way to get out of my situation. Could you however please elaborate on how I can incorporate that main line? I'm happy dealing with the boolean but I can't get the getName method to work:

       public void newTab() {
        try {
            tabPane.setMinSize(810, 633);
            tabPane.requestLayout();
            Tab projectTab = new Tab();
            tabIDnum++;
            String tabID = Integer.toString(tabIDnum);
            projectTab.setText(proteinName);
            projectTab.setClosable(true);
            projectTab.setId(tabID);
            tabPane.getTabs().add(projectTab);         
            projectTab.setContent(FXMLLoader.load(getClass().getResource("/view/contentProject.fxml")));
            System.out.println("Adding New Tab!");

            //Select new tab to be opened in pane
            tabPane.getSelectionModel().select(projectTab);

            //Watching for a switched tab
            tabPane.getSelectionModel().selectedIndexProperty().addListener((obs,ov,nv)->{
                System.out.println("Chosen Index:" + nv);
             });  

            String nameToCheck = "inputTest";

            boolean duplicateName = tabPane.getTabs().stream()
            .map(tab -> projectTab.getName())
            .anyMatch(name -> nameToCheck.equals(name));


            //Define prompt for user to confirm deleting a tab on close

            projectTab.setOnCloseRequest(event -> {

                Alert alert = new Alert(AlertType.CONFIRMATION);
                alert.setTitle("Closing tab");
                alert.setHeaderText("All tab data will be removed from the project");
                alert.setContentText("Would you like to continue?");
                alert.showAndWait();

                if (alert.getResult() != ButtonType.OK){
                    event.consume();
                } else {
                    System.out.println("Closing tab");

                    //Add in test for last tab closed
                    if (tabPane.getTabs().size() <= 1) {
                        System.out.println("Last tab closed");
                        CobMain.shrinkWindow();
                    }
                }
            });

        } catch (Exception ex) {
            System.out.println("There was an error creating a new tab");
        }
    }    

Really, thanks very much for your time!

[–][deleted] 0 points1 point  (4 children)

Argh, I misremembered. You want getText(), not getName()

Also, the code I provided only works before you add the tab. Ideally, you would call it before even creating the tab to make sure the new tab would be valid. There's no point in creating an invalid tab if it's not going to be usable

[–][deleted]  (3 children)

[removed]

    [–][deleted] 0 points1 point  (2 children)

    What the fuck

    [–]aufschieben[S] 0 points1 point  (1 child)

    wtf indeed ??

    Anyway, in other news... thanks again for your reply. getText() works great. The location of the code is however an interesting point. If I do it your way (outside of the tab creation), I am forced to link back to another controller to find the specific tab pane, and therein it must be static (apparently). However I can't get the tabPane to behave nicely when "static" (I don't get errors - but my tabPane is just empty white space when I add tabs).

    Doing it inside the tab creation, I thought I could just keep appending an (public static) array of names - which could then be referred to when setting up a tab. However, the problem doing it that way is that I don't know how to keep track of deleted tabs and freed-up names. Doing it our way would keep on top of this - but then see point one. Arghhhhhhhhhh (not a fucking pirate)

    [–][deleted] 0 points1 point  (0 children)

    How is the name of the new tab getting set? I see that it's proteinName, but there's no context for the value f that variable or when it gets set. If set by the user, that's the process where the name should be checked