JavaFX - 创建标签的麻烦

时间:2015-01-13 21:18:44

标签: java tabs javafx fxml

我希望在按下“接受”按钮时创建新标签。 但抛出异常。我尝试创建命令类(CreateTab类),但不幸的是完全相同。

标签

    public class Tabs implements Initializable {

    @FXML
    TabPane tabPane;

    @Override
    public void initialize(URL url, ResourceBundle resourceBundle) {
        try {
            Tab tab = new Tab();
            tabPane.getTabs().add(tab);
            //tabPane.getTabs().addAll((Tab) FXMLLoader.load(this.getClass().getResource("main.fxml")));
            tab.setContent((Node) FXMLLoader.load(this.getClass().getResource("connectLayout.fxml")));
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public void createTab(String name){
        try {
            Tab tab = new Tab(name);
            tabPane.getTabs().add(tab);
            tab.setContent((Node) FXMLLoader.load(this.getClass().getResource("main.fxml")));
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

}

ConnectView

这里有恶魔按钮))

public class ConnectView implements Initializable {

    // Create
    @FXML
    TextField chatName;


    // Create chat
    @FXML
    public void accept(){
        String chatName = this.chatName.getText();
        //String limit = this.limit.getText();

        Tabs tabs = new Tabs();
        tabs.createTab(chatName);

    }
}

一些例外

java.lang.RuntimeException: java.lang.reflect.InvocationTargetException
    at javafx.fxml.FXMLLoader$ControllerMethodEventHandler.handle(FXMLLoader.java:1456)
    at com.sun.javafx.event.CompositeEventHandler.dispatchBubblingEvent(CompositeEventHandler.java:69)
    at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:217)

    Caused by: java.lang.NullPointerException
    at View.Tabs.createTab(Tabs.java:34)
    at View.ConnectView.accept(ConnectView.java:63)

0 个答案:

没有答案