Javafx adding new tab with fxml on click


Question

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

1
1
7/20/2014 5:03:44 PM

Accepted Answer

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!

6
7/20/2014 5:45:45 PM

Licensed under: CC-BY-SA with attribution
Not affiliated with: Stack Overflow
Icon