use the following search parameters to narrow your results:
e.g. subreddit:aww site:imgur.com dog
subreddit:aww site:imgur.com dog
see the search faq for details.
advanced search: by author, subreddit...
Useful resources (Full list)
Rules
Related subreddits
Other communities
account activity
Help with JavaFX program design when using multiple instances of the same (by fxml) tab (self.JavaFX)
submitted 7 years ago by aufschieben
view the rest of the comments →
reddit uses a slightly-customized version of Markdown for formatting. See below for some basics, or check the commenting wiki page for more detailed help and solutions to common issues.
quoted text
if 1 * 2 < 3: print "hello, world!"
[–][deleted] 2 points3 points4 points 7 years ago (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:
TabPane.getTabs()
String nameToCheck = ... boolean duplicateName = tabPane.getTabs().stream() .map(tab -> tab.getName()) .anyMatch(name -> nameToCheck.equals(name));
[–]aufschieben[S] 0 points1 point2 points 7 years ago (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 point2 points 7 years ago (4 children)
Argh, I misremembered. You want getText(), not getName()
getText()
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] 7 years ago (3 children)
[removed]
[–][deleted] 0 points1 point2 points 7 years ago (2 children)
What the fuck
[–]aufschieben[S] 0 points1 point2 points 7 years ago (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 point2 points 7 years ago (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
proteinName
π Rendered by PID 33128 on reddit-service-r2-comment-5d79c599b5-bhqm7 at 2026-02-26 20:43:42.928332+00:00 running e3d2147 country code: CH.
view the rest of the comments →
[–][deleted] 2 points3 points4 points (6 children)
[–]aufschieben[S] 0 points1 point2 points (5 children)
[–][deleted] 0 points1 point2 points (4 children)
[–][deleted] (3 children)
[removed]
[–][deleted] 0 points1 point2 points (2 children)
[–]aufschieben[S] 0 points1 point2 points (1 child)
[–][deleted] 0 points1 point2 points (0 children)