0

I have multiple lists which I would like to loop through and return all their items in their respective tabs as buttons, each having an action.

I have tried the following:

@FXML
private Tab aTab;

And then on button press:

StackPane tab = new StackPane();
    tab.setStyle("-fx-background-color: Green");
    Label text = new Label("titleLabel");
    text.setStyle("-fx-font-size: 25px;");
    tab.getChildren().add(text);

    aTab.setContent(tab);

The immediate problem is that I get the exception

Exception in thread "JavaFX Application Thread" java.lang.RuntimeException: java.lang.reflect.InvocationTargetException...
Caused by: java.lang.reflect.InvocationTargetException
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
... 52 more
Caused by: java.lang.NullPointerException 
at (the method I'm calling aTab.setContent(tab) in
...

And the slightly more insidious problem is that, as the amount of tabs grow, my code is going to get increasingly bloated.

Ideally, there would be a way to display the content on each tab using a general tab xml file but I have been unable to put something like this together.

How can this be done? Can this be done?

hb22
  • 383
  • 1
  • 2
  • 14
  • You want to use a design pattern to controle the entropy of your code. You can look into "Model-View-Controller Pattern" for that. You can create your Views (fxml Files) with a programe like Scene Builder and then seperate these view files from the Model and Controller part. You can then create a controller for every different tab. – M.Dan Nov 26 '18 at 19:56
  • 1
    You should be looking at the whole stacktrace, not just the first line. The actual Exception causing the error is not the one you posted. – Zephyr Nov 26 '18 at 22:18
  • 1
    Also, see my answer to [this question](https://stackoverflow.com/questions/53453630/javafx-reusable-fxml-snippets/53455765#53455765); it demonstrates one way to reuse an FXML layout in multiple Tabs. – Zephyr Nov 26 '18 at 22:19
  • @Zephyr my bad, I read the whole thing but forgot to post it. I have edited my post to include the relevant items – hb22 Nov 26 '18 at 23:22
  • You could define your FXML up to the Tab container and then use java code to create the tabs within the container. – Perdi Estaquel Nov 26 '18 at 23:57
  • I was able to solve this with a combination of following Zephyr's answer and combing through my main fxml manually to assign ids to the tabs. Apparently scenebuilder was originally unable to. Thanks! – hb22 Nov 27 '18 at 00:06

0 Answers0