user3829658
user3829658

Reputation: 159

Javafx adding new tab with fxml on click

I am trying to add a new tab with fxml

I am trying this code:

Tab sd=new Tab("Customeradd");
sd.setContent(Source.sourceFor("","Customeradd.fxml"));
tabpanel.getTabs().add(sd);

and i am getting syntax error on line sd.setContent(Source.sourceFor("","Customeradd.fxml")); and tabpanel is my TabPane.

Error Look Like

enter image description here

Help Me please

Upvotes: 1

Views: 4956

Answers (1)

Renato
Renato

Reputation: 13690

Assuming you have a FXML at the same directory as the class where you will load it from, you should do something like this (if your FXML defines a TabPane):

 TabPane pane = FXMLLoader.load(this.getClass().getResource("SomeWidget.fxml"));

Now, suppose you have a nice Tab in a MyTab.fxml file:

pane.getTabs().addAll((Tab)FXMLLoader.load(this.getClass().getResource("MyTab.fxml")));

Really easy!

Upvotes: 6

Related Questions