Javafx adding new tab with fxml on click

徘徊边缘 提交于 2019-12-24 08:26:54

问题


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

Help Me please


回答1:


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!



来源:https://stackoverflow.com/questions/24850050/javafx-adding-new-tab-with-fxml-on-click

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!