Witam. Zacząłem pisać pewien program. Mam problem z zamianą sceny w oknie (chodzi o zmiane wygladu calego okna po kliknięciu na Label). Wydaje mi się, że robię wszystko tak samo jak w poradnikach znalezionych w Internecie ( np. tutaj: link), ale to nie działa..
Exception in thread "JavaFX Application Thread" java.lang.RuntimeException: java.lang.reflect.InvocationTargetException
at javafx.fxml.FXMLLoader$MethodHandler.invoke(FXMLLoader.java:1774)
at javafx.fxml.FXMLLoader$ControllerMethodEventHandler.handle(FXMLLoader.java:1657)
at com.sun.javafx.event.CompositeEventHandler.dispatchBubblingEvent(CompositeEventHandler.java:86)
at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:238)
at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:191)
at com.sun.javafx.event.CompositeEventDispatcher.dispatchBubblingEvent(CompositeEventDispatcher.java:59)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:58)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at com.sun.javafx.event.EventUtil.fireEventImpl(EventUtil.java:74)
at com.sun.javafx.event.EventUtil.fireEvent(EventUtil.java:54)
at javafx.event.Event.fireEvent(Event.java:198)
at javafx.scene.Scene$ClickGenerator.postProcess(Scene.java:3470)
at javafx.scene.Scene$ClickGenerator.access$8100(Scene.java:3398)
at javafx.scene.Scene$MouseHandler.process(Scene.java:3766)
at javafx.scene.Scene$MouseHandler.access$1500(Scene.java:3485)
at javafx.scene.Scene.impl_processMouseEvent(Scene.java:1762)
at javafx.scene.Scene$ScenePeerListener.mouseEvent(Scene.java:2494)
at com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(GlassViewEventHandler.java:394)
at com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(GlassViewEventHandler.java:295)
at java.security.AccessController.doPrivileged(Native Method)
at com.sun.javafx.tk.quantum.GlassViewEventHandler.lambda$handleMouseEvent$353(GlassViewEventHandler.java:432)
at com.sun.javafx.tk.quantum.QuantumToolkit.runWithoutRenderLock(QuantumToolkit.java:389)
at com.sun.javafx.tk.quantum.GlassViewEventHandler.handleMouseEvent(GlassViewEventHandler.java:431)
at com.sun.glass.ui.View.handleMouseEvent(View.java:555)
at com.sun.glass.ui.View.notifyMouse(View.java:937)
at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
at com.sun.glass.ui.win.WinApplication.lambda$null$147(WinApplication.java:177)
at java.lang.Thread.run(Thread.java:748)
Caused by: java.lang.reflect.InvocationTargetException
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at sun.reflect.misc.Trampoline.invoke(MethodUtil.java:71)
at sun.reflect.GeneratedMethodAccessor1.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at sun.reflect.misc.MethodUtil.invoke(MethodUtil.java:275)
at javafx.fxml.FXMLLoader$MethodHandler.invoke(FXMLLoader.java:1771)
... 31 more
Caused by: java.lang.NullPointerException
at sample.LoginController.clickDodajKonto(LoginController.java:54)
... 41 more
Główna klasa programu - Login.java:
package sample;
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.stage.Stage;
public class Login extends Application {
@Override
public void start(Stage primaryStage) throws Exception{
Parent root = FXMLLoader.load(getClass().getResource("login.fxml"));
primaryStage.setTitle("Hello World");
primaryStage.setScene(new Scene(root));
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
}
Controller pierwszego okna - LoginController.java ( Po kliknięciu myszka na Label Dodaj Konto powinna pojawić się właśnie ta druga scena):
package sample;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.fxml.Initializable;
import javafx.scene.control.*;
import javafx.scene.layout.AnchorPane;
import javafx.scene.layout.Pane;
import java.awt.event.MouseEvent;
import java.io.IOException;
import java.net.URL;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ResourceBundle;
public class LoginController implements Initializable {
@FXML
private Button connect;
@FXML
private ComboBox comboBox;
@FXML
private PasswordField password;
@FXML
private TextField nick;
@FXML
private Label dodajKonto;
@FXML
private AnchorPane anchorPane;
ObservableList<String> list = FXCollections.observableArrayList("Java", "SO2");
//Connector connector = new Connector();
// Statement statement = null;
@Override
public void initialize(URL location, ResourceBundle resources) {
comboBox.setItems(list);
comboBox.setValue("Java");
// try {
// statement = connector.connectDB();
// } catch (SQLException e) {
// e.printStackTrace();
//}
}
@FXML
public void clickDodajKonto() throws IOException {
Pane newPane = FXMLLoader.load(getClass().getResource("rejestration.fxml"));
System.out.println("aaa"); // tu jeszcze jest wszystko dobrze;
anchorPane.getChildren().setAll(newPane); // tak jakby to bylo zle
}
@FXML
public void clickPolacz(ActionEvent actionEvent) {
}
}
Controller drugiego okna - RejestrationController.java
package sample;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.Button;
import javafx.scene.control.PasswordField;
import javafx.scene.control.TextField;
import java.net.URL;
import java.util.ResourceBundle;
public class RejestrationController implements Initializable{
@FXML
private Button newButton;
@FXML
private TextField nick;
@FXML
private PasswordField password;
@FXML
private Button connect;
@Override
public void initialize(URL location, ResourceBundle resources) {
System.out.println("bbb");
}
}
pliki fxml:
login.fxml
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.Cursor?>
<?import javafx.scene.control.Button?>
<?import javafx.scene.control.ComboBox?>
<?import javafx.scene.control.Label?>
<?import javafx.scene.control.PasswordField?>
<?import javafx.scene.control.TextField?>
<?import javafx.scene.layout.AnchorPane?>
<?import javafx.scene.layout.Pane?>
<?import javafx.scene.text.Font?>
<AnchorPane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="400.0" prefWidth="600.0" style="-fx-background-color: #e6e6e6;" xmlns="http://javafx.com/javafx/10.0.1" xmlns:fx="http://javafx.com/fxml/1" fx:controller="sample.LoginController">
<children>
<Pane layoutX="100.0" layoutY="50.0" prefHeight="311.0" prefWidth="400.0" style="-fx-background-color: #e6e6e6;">
<children>
<TextField fx:id="nick" layoutX="125.0" layoutY="61.0" promptText="Nazwa użytkownika">
<font>
<Font name="Microsoft New Tai Lue" size="12.0" />
</font></TextField>
<PasswordField fx:id="password" layoutX="126.0" layoutY="110.0" promptText="Hasło">
<font>
<Font name="Microsoft Tai Le" size="12.0" />
</font></PasswordField>
<ComboBox fx:id="comboBox" layoutX="125.0" layoutY="158.0" prefWidth="150.0" />
<Button fx:id="connect" layoutX="170.0" layoutY="211.0" mnemonicParsing="false" onAction="#clickPolacz" text="Połącz" textFill="WHITE">
<font>
<Font name="System Bold" size="13.0" />
</font></Button>
<Label layoutX="147.0" layoutY="-14.0" text="Zaloguj się" textFill="WHITE">
<font>
<Font name="Impact" size="24.0" />
</font>
</Label>
<Label fx:id="dodajKonto" layoutX="149.0" layoutY="288.0" onMouseClicked="#clickDodajKonto" text="DODAJ KONTO" textFill="WHITE">
<font>
<Font name="Impact" size="18.0" />
</font>
<cursor>
<Cursor fx:constant="HAND" />
</cursor>
</Label>
<Label layoutX="181.0" layoutY="29.0" textFill="RED">
<font>
<Font size="14.0" />
</font>
</Label>
</children>
</Pane>
</children>
</AnchorPane>
rejestration.fxml:
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.control.Button?>
<?import javafx.scene.control.Label?>
<?import javafx.scene.control.PasswordField?>
<?import javafx.scene.control.TextField?>
<?import javafx.scene.layout.AnchorPane?>
<?import javafx.scene.layout.Pane?>
<?import javafx.scene.text.Font?>
<AnchorPane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="400.0" prefWidth="600.0" style="-fx-background-color: #e6e6e6;" xmlns="http://javafx.com/javafx/10.0.1" xmlns:fx="http://javafx.com/fxml/1" fx:controller="sample.RejestrationController">
<children>
<Pane layoutX="100.0" layoutY="44.0" prefHeight="311.0" prefWidth="400.0" style="-fx-background-color: #e6e6e6;">
<children>
<TextField fx:id="nick" layoutX="125.0" layoutY="61.0" promptText="Nazwa użytkownika" />
<PasswordField fx:id="password" layoutX="126.0" layoutY="110.0" promptText="Hasło" />
<Button fx:id="connect" layoutX="172.0" layoutY="207.0" mnemonicParsing="false" text="Wyślij" textFill="WHITE">
<font>
<Font name="System Bold" size="13.0" />
</font></Button>
<PasswordField layoutX="126.0" layoutY="156.0" promptText="Powtórz hasło" />
<Label layoutX="185.0" layoutY="23.0" textFill="RED">
<font>
<Font size="14.0" />
</font>
</Label>
</children>
</Pane>
<Label layoutX="223.0" layoutY="29.0" text="ZAREJESTRUJ SIĘ" textFill="WHITE">
<font>
<Font name="Impact" size="24.0" />
</font>
</Label>
</children>
</AnchorPane>
Mogę liczyć na jakieś wskazówki? Męczę się z tym już dłuższy czas i nie mogę nic wykombinować. Pozdrawiam