我有一个问题: 我有一个TabPane,设计在FXML文件中,现在我想在点击“检查”按钮时更改所选标签的名称。
public void onAddDateRequest(){
try {
//datePane wird in den FXML Loader geladen
FXMLLoader loader = new FXMLLoader(getClass().getResource("/homenizer/view/datePane.fxml"));
Parent content = (Parent)loader.load();
Tab tab = new Tab("Neuer Termin");
tab.setContent(content); //Dem neuen Tab wird das Layout der FXXML Datei aus Content zugewiesen
tabPane.getTabs().add(tab);
} catch (IOException ex) {
Logger.getLogger(HomenizerController.class.getName()).log(Level.SEVERE, null, ex);
}
}
public void onCheckRequest(){
Date date = new Date();
tabPane.getSelectionModel().getSelectedItem().setText("A");
}
当你点击按钮添加新标签时,会调用'onAddDateRequest'方法。
这里是FXML文件:
<TabPane fx:id="tabPane">
<Tab text="Willkommen" closable="true" >
<VBox>
<TitledPane text="Allgemeines" expanded="true">
<GridPane>
<Label text="Benutzername:" GridPane.columnIndex="0" GridPane.rowIndex="0" />
<Label fx:id="username" GridPane.columnIndex="1" GridPane.rowIndex="0" />
<Label text="Termine gesamt:" GridPane.columnIndex="0" GridPane.rowIndex="1" />
<Label fx:id="dates" GridPane.columnIndex="1" GridPane.rowIndex="1" />
<Label text="Aufgaben gesamt:" GridPane.columnIndex="0" GridPane.rowIndex="2" />
<Label fx:id="toDos" GridPane.columnIndex="1" GridPane.rowIndex="2" />
</GridPane>
</TitledPane>
<TitledPane text="Aktuell" expanded="true" >
<ScrollPane>
<GridPane>
<Label text="Aktuelle Termine:" GridPane.columnIndex="0" GridPane.rowIndex="0" />
<Label fx:id="actualDates" GridPane.columnIndex="1" GridPane.rowIndex="0" />
<Label text="Aktuelle Aufgaben:" GridPane.columnIndex="0" GridPane.rowIndex="1" />
<Label fx:id="actualToDos" GridPane.columnIndex="1" GridPane.rowIndex="1" />
</GridPane>
</ScrollPane>
</TitledPane>
</VBox>
</Tab>
</TabPane>
这里是新标签的布局,也是用FXML文件编码的
<ScrollPane fx:id="scrollPane" minHeight="350.0" xmlns:fx="http://javafx.com/fxml" fx:controller="homenizer.controller.HomenizerController">
<VBox>
<TitledPane text="Termin" expanded="true">
<GridPane fx:id="datePane" alignment="TOP_LEFT" hgap="10" vgap="10" prefWidth="900" >
<Label text="Termin Beschreibung" GridPane.columnIndex="0" GridPane.rowIndex="1" />
<TextField fx:id="dateInformation" GridPane.columnIndex="1" GridPane.rowIndex="1" />
<Label text="Datum" GridPane.columnIndex="0" GridPane.rowIndex="2" />
<TextField fx:id="dateOfDate" GridPane.columnIndex="1" GridPane.rowIndex="2" />
<Label text="Termin beginn" GridPane.columnIndex="0" GridPane.rowIndex="3" />
<TextField fx:id="dateTimeBegin" GridPane.columnIndex="1" GridPane.rowIndex="3" />
<Label text="Termin ende" GridPane.columnIndex="2" GridPane.rowIndex="3" />
<TextField fx:id="dateTimeEnd" GridPane.columnIndex="3" GridPane.rowIndex="3" />
<Label text="Priorität" GridPane.columnIndex="0" GridPane.rowIndex="4" />
<fx:define>
<ToggleGroup fx:id="priorityGroup" />
</fx:define>
<RadioButton text="Hoch" GridPane.columnIndex="1" GridPane.rowIndex="4" toggleGroup="$priorityGroup" />
<RadioButton text="Normal" GridPane.columnIndex="1" GridPane.rowIndex="5" toggleGroup="$priorityGroup"/>
<RadioButton text="Niedrig" GridPane.columnIndex="1" GridPane.rowIndex="6" toggleGroup="$priorityGroup"/>
</GridPane>
</TitledPane>
<TitledPane text="Person/Unternehmen" ellipsisString="Persson / Unternehmen" expanded="false" >
<GridPane fx:id="personCompanyPane" hgap="10" vgap="10">
<Label text="Name" GridPane.columnIndex="0" GridPane.rowIndex="0" />
<TextField fx:id="name" GridPane.columnIndex="1" GridPane.rowIndex="0" />
<Label text="Vorname" GridPane.columnIndex="0" GridPane.rowIndex="1" />
<TextField fx:id="firstname" GridPane.columnIndex="1" GridPane.rowIndex="1" />
<Label text="Ort" GridPane.columnIndex="0" GridPane.rowIndex="2" />
<TextField fx:id="city" GridPane.columnIndex="1" GridPane.rowIndex="2" />
<Label text="PLZ" GridPane.columnIndex="2" GridPane.rowIndex="2" />
<TextField fx:id="place" maxWidth="60" GridPane.columnIndex="3" GridPane.rowIndex="2" />
<Label text="Strasse" GridPane.columnIndex="0" GridPane.rowIndex="3" />
<TextField fx:id="street" GridPane.columnIndex="1" GridPane.rowIndex="3" />
<Label text="Hausnr." GridPane.columnIndex="2" GridPane.rowIndex="3" />
<TextField fx:id="houseNr" maxWidth="35" GridPane.columnIndex="3" GridPane.rowIndex="3" />
<Label text="Unternehmen" GridPane.columnIndex="0" GridPane.rowIndex="4" />
<TextField fx:id="company" GridPane.columnIndex="1" GridPane.rowIndex="4" />
</GridPane>
</TitledPane>
<TitledPane text="Notiz" expanded="false" >
<GridPane>
<Label text="Notiz" GridPane.columnIndex="0" GridPane.rowIndex="0" />
<TextArea GridPane.columnIndex="0" GridPane.rowIndex="1" prefWidth="900" />
</GridPane>
</TitledPane>
<HBox>
<Button fx:id="check" text="bestätigen"
onAction="#onCheckRequest"/>
<Button fx:id="cancel" text="abbrechen" />
</HBox>
</VBox>
我希望这足以分析问题的代码:)
这是我的代码,每次我得到一个NullPointerException,但我不知道为什么。 TabPane不是空的,所以我不知道为什么我会得到这个例外。这是我的错误代码:
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)
at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:170)
at com.sun.javafx.event.CompositeEventDispatcher.dispatchBubblingEvent(CompositeEventDispatcher.java:38)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:37)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:92)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:35)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:92)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:35)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:92)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:35)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:92)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:35)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:92)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:35)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:92)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:35)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:92)
at com.sun.javafx.event.EventUtil.fireEventImpl(EventUtil.java:53)
at com.sun.javafx.event.EventUtil.fireEvent(EventUtil.java:28)
at javafx.event.Event.fireEvent(Event.java:171)
at javafx.scene.Node.fireEvent(Node.java:6867)
at javafx.scene.control.Button.fire(Button.java:179)
at com.sun.javafx.scene.control.behavior.ButtonBehavior.mouseReleased(ButtonBehavior.java:193)
at com.sun.javafx.scene.control.skin.SkinBase$4.handle(SkinBase.java:336)
at com.sun.javafx.scene.control.skin.SkinBase$4.handle(SkinBase.java:329)
at com.sun.javafx.event.CompositeEventHandler.dispatchBubblingEvent(CompositeEventHandler.java:64)
at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:217)
at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:170)
at com.sun.javafx.event.CompositeEventDispatcher.dispatchBubblingEvent(CompositeEventDispatcher.java:38)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:37)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:92)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:35)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:92)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:35)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:92)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:35)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:92)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:35)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:92)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:35)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:92)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:35)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:92)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:35)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:92)
at com.sun.javafx.event.EventUtil.fireEventImpl(EventUtil.java:53)
at com.sun.javafx.event.EventUtil.fireEvent(EventUtil.java:33)
at javafx.event.Event.fireEvent(Event.java:171)
at javafx.scene.Scene$MouseHandler.process(Scene.java:3369)
at javafx.scene.Scene$MouseHandler.process(Scene.java:3209)
at javafx.scene.Scene$MouseHandler.access$1900(Scene.java:3164)
at javafx.scene.Scene.impl_processMouseEvent(Scene.java:1582)
at javafx.scene.Scene$ScenePeerListener.mouseEvent(Scene.java:2267)
at com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(GlassViewEventHandler.java:250)
at com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(GlassViewEventHandler.java:173)
at java.security.AccessController.doPrivileged(Native Method)
at com.sun.javafx.tk.quantum.GlassViewEventHandler.handleMouseEvent(GlassViewEventHandler.java:292)
at com.sun.glass.ui.View.handleMouseEvent(View.java:530)
at com.sun.glass.ui.View.notifyMouse(View.java:924)
at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
at com.sun.glass.ui.win.WinApplication.access$100(WinApplication.java:17)
at com.sun.glass.ui.win.WinApplication$3$1.run(WinApplication.java:67)
at java.lang.Thread.run(Thread.java:744)
Caused by: java.lang.reflect.InvocationTargetException
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:606)
at sun.reflect.misc.Trampoline.invoke(MethodUtil.java:75)
at sun.reflect.GeneratedMethodAccessor1.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:606)
at sun.reflect.misc.MethodUtil.invoke(MethodUtil.java:279)
at javafx.fxml.FXMLLoader$ControllerMethodEventHandler.handle(FXMLLoader.java:1453)
... 64 more
Caused by: java.lang.NullPointerException
at homenizer.controller.HomenizerController.onCheckRequest(HomenizerController.java:105)
... 74 more
感谢您的帮助,我不知道您是否需要更多我的代码,但我确信他们的代码中没有任何错误。
答案 0 :(得分:0)
所以我发现了我的错误:)我在我的控制器代码中为我的FXML设置了控制器,而不是在FXML文件本身,现在它可以工作