Unable to open a new window in JavaFX - javafx

when i put the method "openPlanes" in button the option "onAction".
This show many errors.
But if I remove the "openPlanes" his open normal.
#FXML
private void openPlanes() {
openStage("view/Cadastro.fxml");
}
private void openStage(String fxml) {
try {
Stage currentStage = (Stage) PLANE.getScene().getWindow();
Parent root = FXMLLoader.load(getClass().getResource(fxml));
Scene scene = new Scene(root);
Stage stage = new Stage(StageStyle.TRANSPARENT);
stage.setScene(scene);
stage.show();
currentStage.hide();
} catch (IOException ex) {
Logger.getLogger(mainController.class.getName()).log(Level.SEVERE, null, ex);
}
}
Caused by: javafx.fxml.LoadException: No controller specified.
file:/C:/Users/diego/Documents/NetBeansProjects/Automekanik/DGDSoft/dist/run708547813/DGD%20Soft.jar!/dgdsoft/view/MainDGD.fxml:23
at javafx.fxml.FXMLLoader.constructLoadException(FXMLLoader.java:2597)
at javafx.fxml.FXMLLoader.access$100(FXMLLoader.java:103)
at javafx.fxml.FXMLLoader$Element.getControllerMethodHandle(FXMLLoader.java:557)
at javafx.fxml.FXMLLoader$Element.processEventHandlerAttributes(FXMLLoader.java:599)
at javafx.fxml.FXMLLoader$ValueElement.processEndElement(FXMLLoader.java:770)
at javafx.fxml.FXMLLoader.processEndElement(FXMLLoader.java:2823)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2532)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2441)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3214)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3175)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3148)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3124)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3104)
at javafx.fxml.FXMLLoader.load(FXMLLoader.java:3097)
at dgdsoft.DGDSoft.start(DGDSoft.java:19)
at com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$162(LauncherImpl.java:863)
at com.sun.javafx.application.PlatformImpl.lambda$runAndWait$175(PlatformImpl.java:326)
at com.sun.javafx.application.PlatformImpl.lambda$null$173(PlatformImpl.java:295)
at java.security.AccessController.doPrivileged(Native Method)
at com.sun.javafx.application.PlatformImpl.lambda$runLater$174(PlatformImpl.java:294)
at com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:95)
at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
at com.sun.glass.ui.win.WinApplication.lambda$null$148(WinApplication.java:191)
... 1 more
Exception running application dgdsoft.DGDSoft
Java Result: 1

At first make sure the fxml resource you are trying to load is in the directory you expect it to be. If it is in the appropriate directory then open the fxml file and search for the string fx:controller.
Make sure the controller is inside the stated package.
If you don't find the string fx:controller in the fxml file then you need to do it programatically as:
private Scene getScene(String fxmlPath, ControllerClass controller) {
FXMLLoader loader;
Parent parent;
Scene scene;
try {
//not FXMLLoader.load(getClass().getResource(fxmlPath)
loader = new FXMLLoader(getClass().getResource(fxmlPath));
loader.setController(controller);
parent = loader.load();
} catch (Exception e) {
e.printStackTrace();
return null;
}
scene = new Scene(parent);
return scene;
}
Lastly, submit the fxml file and code.

Related

FXML Exception in JavaFx | TableView - Table is Null | Eclispe IDE

I have been working on it for the past 3-4 days. I have googled it and also saw similar questions' answers but it did not help me.
Exception at Changing Scence:javafx.fxml.LoadException:
address-to-//application/Dashboard.fxml
This is the Exception I am getting when I log into the dashboard from the login page. Usually, I am not getting this exception but when I am trying to display the data in tableView from the xampp server then this exception occurs otherwise it is working fine.
here is the code of Main.java
public class Main extends Application {
private static Stage stg;
#Override
public void start(Stage primaryStage) {
stg = primaryStage;
try {
primaryStage.setResizable(false);
Parent root = FXMLLoader.load(getClass().getResource("/application/Login.fxml"));
primaryStage.setTitle("My Application Title");
primaryStage.setResizable(false);
primaryStage.setScene(new Scene(root , 1200,670));
primaryStage.show();
}catch(Exception e) {
System.out.println("Exception Occured: " +e);
}
}
//to change the scene
public void changeScene(String fxml) {
//passing XML file via a string parameter
try {
Parent pane = FXMLLoader.load(getClass().getResource(fxml));
stg.getScene().setRoot(pane);
}catch(Exception e) {
System.out.println("Exception at Changing Scence:" +e);
}
}
public static void main(String[] args) {
launch(args);
}
}
and this is the code on the login button
/*TO CHECK THE LOGIN
* CREDENTIALS STARTS*/
#FXML
public void loginUser(ActionEvent event) {
uname = username.getText().toString();
upass = password.getText().toString();
try {
DbConnectivity db = new DbConnectivity();
pst = db.connect.prepareStatement("SELECT * FROM adminlogin WHERE admin=? AND password=?");
pst.setString(1, uname);
pst.setString(2, upass);
rst = pst.executeQuery();
if(uname.isEmpty() && upass.isEmpty()) {
wronginput.setText("Please Enter Credentials!");
} else if(rst.next()) {
wronginput.setText("Log In Success!");
Main main = new Main();
main.changeScene("/application/Dashboard.fxml");
} else {
wronginput.setText("Wrong Username or Password");
username.setText("");
password.setText("");
username.requestFocus();
}
} catch (SQLException e) {
System.out.println("Prepared Exception: "+e);
}
}
/*TO CHECK LOGIN
* CREDENTIALS ENDS*/
And these are StackTrace
javafx.fxml.LoadException:
address-to-/application/Dashboard.fxml
at javafx.fxml#19.0.2.1/javafx.fxml.FXMLLoader.constructLoadException(FXMLLoader.java:2714)
at javafx.fxml#19.0.2.1/javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2692)
at javafx.fxml#19.0.2.1/javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2555)
at javafx.fxml#19.0.2.1/javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3368)
at javafx.fxml#19.0.2.1/javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3324)
at javafx.fxml#19.0.2.1/javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3292)
at javafx.fxml#19.0.2.1/javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3264)
at javafx.fxml#19.0.2.1/javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3240)
at javafx.fxml#19.0.2.1/javafx.fxml.FXMLLoader.load(FXMLLoader.java:3233)
at application.Main.changeScene(Main.java:39)
at application.LoginControl.loginUser(LoginControl.java:59)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:77)
at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.base/java.lang.reflect.Method.invoke(Method.java:568)
at com.sun.javafx.reflect.Trampoline.invoke(MethodUtil.java:77)
at jdk.internal.reflect.GeneratedMethodAccessor2.invoke(Unknown Source)
at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.base/java.lang.reflect.Method.invoke(Method.java:568)
at javafx.base#19.0.2.1/com.sun.javafx.reflect.MethodUtil.invoke(MethodUtil.java:275)
at javafx.fxml#19.0.2.1/com.sun.javafx.fxml.MethodHelper.invoke(MethodHelper.java:84)
at javafx.fxml#19.0.2.1/javafx.fxml.FXMLLoader$MethodHandler.invoke(FXMLLoader.java:1852)
at javafx.fxml#19.0.2.1/javafx.fxml.FXMLLoader$ControllerMethodEventHandler.handle(FXMLLoader.java:1724)
at javafx.base#19.0.2.1/com.sun.javafx.event.CompositeEventHandler.dispatchBubblingEvent(CompositeEventHandler.java:86)
at javafx.base#19.0.2.1/com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:234)
at javafx.base#19.0.2.1/com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:191)
at javafx.base#19.0.2.1/com.sun.javafx.event.CompositeEventDispatcher.dispatchBubblingEvent(CompositeEventDispatcher.java:59)
at javafx.base#19.0.2.1/com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:58)
at javafx.base#19.0.2.1/com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at javafx.base#19.0.2.1/com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
at javafx.base#19.0.2.1/com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at javafx.base#19.0.2.1/com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
at javafx.base#19.0.2.1/com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at javafx.base#19.0.2.1/com.sun.javafx.event.EventUtil.fireEventImpl(EventUtil.java:74)
at javafx.base#19.0.2.1/com.sun.javafx.event.EventUtil.fireEvent(EventUtil.java:49)
at javafx.base#19.0.2.1/javafx.event.Event.fireEvent(Event.java:198)
at javafx.graphics#19.0.2.1/javafx.scene.Node.fireEvent(Node.java:8923)
at javafx.controls#19.0.2.1/javafx.scene.control.Button.fire(Button.java:203)
at javafx.controls#19.0.2.1/com.sun.javafx.scene.control.behavior.ButtonBehavior.mouseReleased(ButtonBehavior.java:207)
at javafx.controls#19.0.2.1/com.sun.javafx.scene.control.inputmap.InputMap.handle(InputMap.java:274)
at javafx.base#19.0.2.1/com.sun.javafx.event.CompositeEventHandler$NormalEventHandlerRecord.handleBubblingEvent(CompositeEventHandler.java:247)
at javafx.base#19.0.2.1/com.sun.javafx.event.CompositeEventHandler.dispatchBubblingEvent(CompositeEventHandler.java:80)
at javafx.base#19.0.2.1/com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:234)
at javafx.base#19.0.2.1/com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:191)
at javafx.base#19.0.2.1/com.sun.javafx.event.CompositeEventDispatcher.dispatchBubblingEvent(CompositeEventDispatcher.java:59)
at javafx.base#19.0.2.1/com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:58)
at javafx.base#19.0.2.1/com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at javafx.base#19.0.2.1/com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
at javafx.base#19.0.2.1/com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at javafx.base#19.0.2.1/com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
at javafx.base#19.0.2.1/com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at javafx.base#19.0.2.1/com.sun.javafx.event.EventUtil.fireEventImpl(EventUtil.java:74)
at javafx.base#19.0.2.1/com.sun.javafx.event.EventUtil.fireEvent(EventUtil.java:54)
at javafx.base#19.0.2.1/javafx.event.Event.fireEvent(Event.java:198)
at javafx.graphics#19.0.2.1/javafx.scene.Scene$MouseHandler.process(Scene.java:3894)
at javafx.graphics#19.0.2.1/javafx.scene.Scene.processMouseEvent(Scene.java:1887)
at javafx.graphics#19.0.2.1/javafx.scene.Scene$ScenePeerListener.mouseEvent(Scene.java:2620)
at javafx.graphics#19.0.2.1/com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(GlassViewEventHandler.java:411)
at javafx.graphics#19.0.2.1/com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(GlassViewEventHandler.java:301)
at java.base/java.security.AccessController.doPrivileged(AccessController.java:399)
at javafx.graphics#19.0.2.1/com.sun.javafx.tk.quantum.GlassViewEventHandler.lambda$handleMouseEvent$2(GlassViewEventHandler.java:450)
at javafx.graphics#19.0.2.1/com.sun.javafx.tk.quantum.QuantumToolkit.runWithoutRenderLock(QuantumToolkit.java:424)
at javafx.graphics#19.0.2.1/com.sun.javafx.tk.quantum.GlassViewEventHandler.handleMouseEvent(GlassViewEventHandler.java:449)
at javafx.graphics#19.0.2.1/com.sun.glass.ui.View.handleMouseEvent(View.java:551)
at javafx.graphics#19.0.2.1/com.sun.glass.ui.View.notifyMouse(View.java:937)
at javafx.graphics#19.0.2.1/com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
at javafx.graphics#19.0.2.1/com.sun.glass.ui.win.WinApplication.lambda$runLoop$3(WinApplication.java:184)
at java.base/java.lang.Thread.run(Thread.java:833)
Caused by: java.lang.NullPointerException: Cannot invoke "javafx.scene.control.TableView.setItems(javafx.collections.ObservableList)" because "this.studentTable" is null
at application.DashboardController.RefreshStudentRecord(DashboardController.java:343)
at application.DashboardController.loadData(DashboardController.java:289)
at application.DashboardController.initialize(DashboardController.java:281)
at javafx.fxml#19.0.2.1/javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2662)
... 66 more
For more clarifications
I have DashboardController.java that is implementing Initializable class.
Inside the controller class I have method to implement called initialize.
here is this method
#Override
public void initialize(URL arg0, ResourceBundle arg1) {
// TODO Auto-generated method stub
loadData();
}
In this method I am using loadData() method that is
private void loadData() {
DbConnectivity db = new DbConnectivity();
conn = db.getConnection();
RefreshStudentRecord();
stsno.setCellValueFactory(new PropertyValueFactory<>("stsno"));
stsname.setCellValueFactory(new PropertyValueFactory<>("stsname"));
stsfname.setCellValueFactory(new PropertyValueFactory<>("stsfname"));
stsid.setCellValueFactory(new PropertyValueFactory<>("stsid"));
stscnic.setCellValueFactory(new PropertyValueFactory<>("stscnic"));
stsphone.setCellValueFactory(new PropertyValueFactory<>("stsphone"));
stsaddress.setCellValueFactory(new PropertyValueFactory<>("stsaddress"));
stsclass.setCellValueFactory(new PropertyValueFactory<>("stsclass"));
}
Inside the loadData() method I am calling another method that is
#FXML
public void RefreshStudentRecord() {
studentDataList.clear();
studentquery = "SELECT * FROM studentdata";
try {
studentPst = conn.prepareStatement(studentquery);
studentRst = studentPst.executeQuery();
while(studentRst.next()) {
studentDataList.add(new StudentsDataController(
studentRst.getInt("S_NO"),
studentRst.getString("studentname"),
studentRst.getString("fathername"),
studentRst.getString("studentid"),
studentRst.getString("studentcnic"),
studentRst.getString("phone"),
studentRst.getString("address"),
studentRst.getString("class")
)
);
studentTable.setItems(studentDataList);
}
} catch (SQLException e) {
System.out.println("Exception at Refresing student data: "+e);
e.printStackTrace();
}
}
I think the problem is somewhere thereby, seeing the StackTrace as it is pointing out that the table is null that's why all of the problems have occurred, but I have no clue why the table is null.
Kindly help me to solve this problem
Before setting the table ArrayList, do a check to check if the ArrayList has some data. Also, move your studentTable.setItems outside the loop such that it looks as follows:
while(studentRst.next()) {
studentDataList.add(new StudentsDataController(
studentRst.getInt("S_NO"),
studentRst.getString("studentname"),
studentRst.getString("fathername"),
studentRst.getString("studentid"),
studentRst.getString("studentcnic"),
studentRst.getString("phone"),
studentRst.getString("address"),
studentRst.getString("class")
)
);
}
if(studentDataList.size()>0){
studentTable.setItems(studentDataList);
}
Thanks to all for taking interest in my Question.
Here is how I solved it.
I created a separate class to handle the stuff for the tableView and made this class the controller of the fxml of tableView.
Then everything went right :)

Placing HBox in BorderPane throws Exception in start method, JavaFX

I am trying to make a layout consisting of a TreeView List and a couple of textfields, buttons at the bottom. I figured i'd use a BorderPane to hold my layout in place. But i am getting an Exception in Application start method and it also tells me Children: duplicate children added: parent = BorderPane#71e344e8
Do you guys have an idea of whats going on?
public class Main extends Application {
Stage window;
TextField nameInput, quantityInput;
TreeView<String> tree;
public static void main(String[] args) {
launch(args);
}
public void start (Stage primaryStage) throws Exception{
window = primaryStage;
window.setTitle("GrocerieList");
// tree view
TreeItem<String> root, food, drink;
root = new TreeItem<>();
root.setExpanded(true);
// food
food = makeBranch("Food",root);
makeBranch("Banana", food);
makeBranch("Coconut", food);
makeBranch("Eggs", food);
// drink
drink = makeBranch("Drinks",root);
makeBranch("Water", drink);
makeBranch("Fanta", drink);
makeBranch("Beer", drink);
// tree
tree = new TreeView<>(root);
tree.setShowRoot(false);
// bottom input fields
nameInput = new TextField();
nameInput.setPromptText("Productname");
nameInput.setMinWidth(100);
quantityInput = new TextField();
quantityInput.setPromptText("Quantity");
quantityInput.setMinWidth(100);
// buttons
Button addbutton = new Button("Add");
// Layout
// HBOX
HBox hBox = new HBox();
hBox.setPadding(new Insets(10));
hBox.setSpacing(10);
hBox.getChildren().addAll(nameInput, quantityInput, addbutton);
// BorderPane
BorderPane borderPane = new BorderPane();
borderPane.getChildren().addAll(tree, hBox);
borderPane.setCenter(tree);
borderPane.setBottom(hBox);
Scene scene = new Scene(borderPane, 520, 620);
window.setScene(scene);
window.show();
}
public TreeItem<String> makeBranch(String title, TreeItem<String> parent){
TreeItem<String> item = new TreeItem<>(title);
item.setExpanded(true);
parent.getChildren().add(item);
return item;
}
Complete error:
Exception in Application start method
java.lang.reflect.InvocationTargetException
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.base/java.lang.reflect.Method.invoke(Method.java:567)
at javafx.graphics/com.sun.javafx.application.LauncherImpl.launchApplicationWithArgs(LauncherImpl.java:464)
at javafx.graphics/com.sun.javafx.application.LauncherImpl.launchApplication(LauncherImpl.java:363)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.base/java.lang.reflect.Method.invoke(Method.java:567)
at java.base/sun.launcher.LauncherHelper$FXHelper.main(LauncherHelper.java:1051)
Caused by: java.lang.RuntimeException: Exception in Application start method
at javafx.graphics/com.sun.javafx.application.LauncherImpl.launchApplication1(LauncherImpl.java:900)
at javafx.graphics/com.sun.javafx.application.LauncherImpl.lambda$launchApplication$2(LauncherImpl.java:195)
at java.base/java.lang.Thread.run(Thread.java:830)
Caused by: java.lang.IllegalArgumentException: Children: duplicate children added: parent = BorderPane#71e344e8
at javafx.graphics/javafx.scene.Parent$3.onProposedChange(Parent.java:560)
at javafx.base/com.sun.javafx.collections.VetoableListDecorator.add(VetoableListDecorator.java:206)
at javafx.graphics/javafx.scene.layout.BorderPane$BorderPositionProperty.invalidated(BorderPane.java:692)
at javafx.base/javafx.beans.property.ObjectPropertyBase.markInvalid(ObjectPropertyBase.java:112)
at javafx.base/javafx.beans.property.ObjectPropertyBase.set(ObjectPropertyBase.java:147)
at javafx.graphics/javafx.scene.layout.BorderPane.setCenter(BorderPane.java:268)
at sample.Main.start(Main.java:70)
at javafx.graphics/com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$9(LauncherImpl.java:846)
at javafx.graphics/com.sun.javafx.application.PlatformImpl.lambda$runAndWait$12(PlatformImpl.java:455)
at javafx.graphics/com.sun.javafx.application.PlatformImpl.lambda$runLater$10(PlatformImpl.java:428)
at java.base/java.security.AccessController.doPrivileged(AccessController.java:391)
at javafx.graphics/com.sun.javafx.application.PlatformImpl.lambda$runLater$11(PlatformImpl.java:427)
at javafx.graphics/com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:96)
Exception running application sample.Main
Your problem is here:
BorderPane borderPane = new BorderPane();
borderPane.getChildren().addAll(tree, hBox);
borderPane.setCenter(tree);
borderPane.setBottom(hBox);
The calls to setCenter and setBottom add those nodes to the children list. Since you've manually added the two nodes to the children list, those method calls lead to adding the same node to the same parent twice—that's not allowed. When it comes to layouts that provide special properties for positioning, such as BorderPane, you want to avoid interacting with the children list directly. Change your code to:
BorderPane borderPane = new BorderPane();
borderPane.setCenter(tree);
borderPane.setBottom(hBox);

Call a method of a controller from another controller JavaFX

I have a problem, i'm doing a program that simulates the FCFS algorithm. I'm working with threads. The thing is that when I press T the program needs to interrupt the thread and show a window(stage) with a table, when I close the stage i need to start again the thread, so my idea is that from the second controller call the function of the other controller and then close the stage
Here is where i call the second stage:
case T:
banderaPause=true;
th.interrupt();
Stage stTableView = new Stage();
FXMLLoader loader = new FXMLLoader();
Pane root = null;
try {
root = loader.load(getClass().getResource("showTable.fxml").openStream());
showTableController stController = (showTableController)loader.getController();
stController.init(arreglo);
stTableView.setTitle("Programa 4");
stTableView.setScene(new Scene(root, 950, 375));
stTableView.setX(2.00);
stTableView.setY(300.00);
stTableView.show();
stTableView.setOnCloseRequest(new EventHandler<WindowEvent>() {
#Override
public void handle(WindowEvent event) {
System.out.println("ya cerre");
}
});
} catch (IOException e1) {
e1.printStackTrace();
}
break;
And here is where i tried to call the method from the second controller:
case C:
try {
FXMLLoader loader = new FXMLLoader();
loader.setLocation(getClass().getResource("second.fxml"));
Parent root = loader.load();
Controller n = loader.getController();
n.Continua();
Stage stage = (Stage) closeButton.getScene().getWindow();
stage.close();
} catch (IOException e1) {
e1.printStackTrace();
}
break;
But the stage just close and the method was never called
From the javadoc for onCloseRequested (emphasis mine):
Called when there is an external request to close this Window.
So the handler is not executed, if you call close yourself.
For Stages that are not the primary stage there is an option to wait for the window to be closed, however: Stage.showAndWait, so you could use this instead of registering an event handler:
stTableView.showAndWait();
// Print to console after stage is closed
System.out.println("ya cerre");

JavaFx Call another panel and transmits values

My program is structured package and control on the interface as follows
**+ Browser : BrowserController.class**
-#FXML
private SplitPane spHTMLParser;
#FXML
private TextField txtURL;
#FXML
private Button btnSearch;
#FXML
private WebView browser;
**+ Browser/Console : ConsoleController.class**
#FXML
private Label lblURL;
In the browser page when the user clicks the btnSearch,I will show webview site with user input url. When the process is complete I want to show the Console.fxml in Browser.fxml with SplitPane and When the console is displayed, I'd label can get value url . Please help me!
#FXML
void btnSearch(ActionEvent event) {
String url = txtURL.getText().trim();
if (!url.isEmpty()) {
WebEngine webEngine = browser.getEngine();
progressBar.progressProperty().bind(webEngine.getLoadWorker().progressProperty());
webEngine.load(url);
webEngine.getLoadWorker().stateProperty().addListener((ObservableValue<? extends Worker.State> observable, Worker.State oldValue, Worker.State newValue) -> {
switch (newValue) {
case SUCCEEDED:
if (count++ != 0) {
btnNext.setDisable(false);
}
btnBack.setDisable(false);
progressBar.setVisible(false);
{
try {
FXMLLoader fxmlLoader = new FXMLLoader();
Pane pnConsole = fxmlLoader.load(getClass().getResource("console/Console.fxml").openStream());
spHTMLParser.getItems().add(pnConsole);
spHTMLParser.setDividerPositions(0.5);
} catch (IOException ex) {
Logger.getLogger(BrowserController.class.getName()).log(Level.SEVERE, null, ex);
}
}
break;
}
});
}
}
Load events url successful work exactly but I got an error when trying to call the other pane via fxml
Its error messages with commands
Pane pnConsole = fxmlLoader.load(getClass().getResource("console/Console.fxml").openStream());
Exception in thread "JavaFX Application Thread" java.lang.NullPointerException
at publishers.controllers.resources.browser.BrowserController.lambda$webView$4(BrowserController.java:144)
at com.sun.javafx.binding.ExpressionHelper$SingleChange.fireValueChangedEvent(ExpressionHelper.java:182)
at com.sun.javafx.binding.ExpressionHelper.fireValueChangedEvent(ExpressionHelper.java:81)
at javafx.beans.property.ReadOnlyObjectPropertyBase.fireValueChangedEvent(ReadOnlyObjectPropertyBase.java:74)
at javafx.beans.property.ReadOnlyObjectWrapper.fireValueChangedEvent(ReadOnlyObjectWrapper.java:102)
at javafx.beans.property.ObjectPropertyBase.markInvalid(ObjectPropertyBase.java:112)
at javafx.beans.property.ObjectPropertyBase.set(ObjectPropertyBase.java:146)
at javafx.scene.web.WebEngine$LoadWorker.updateState(WebEngine.java:1260)
at javafx.scene.web.WebEngine$LoadWorker.dispatchLoadEvent(WebEngine.java:1371)
at javafx.scene.web.WebEngine$LoadWorker.access$1200(WebEngine.java:1253)
at javafx.scene.web.WebEngine$PageLoadListener.dispatchLoadEvent(WebEngine.java:1240)
at com.sun.webkit.WebPage.fireLoadEvent(WebPage.java:2400)
at com.sun.webkit.WebPage.fwkFireLoadEvent(WebPage.java:2244)
at com.sun.webkit.network.URLLoader.twkDidFinishLoading(Native Method)
at com.sun.webkit.network.URLLoader.notifyDidFinishLoading(URLLoader.java:838)
at com.sun.webkit.network.URLLoader.lambda$didFinishLoading$96(URLLoader.java:829)
at com.sun.javafx.application.PlatformImpl.lambda$null$173(PlatformImpl.java:295)
at java.security.AccessController.doPrivileged(Native Method)
at com.sun.javafx.application.PlatformImpl.lambda$runLater$174(PlatformImpl.java:294)
at com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:95)
at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
at com.sun.glass.ui.win.WinApplication.lambda$null$148(WinApplication.java:191)
at java.lang.Thread.run(Thread.java:745)

How do I embed a controller with a new scene in JavaFX

I want to open a new scene with a controller but I get an IOException when I put the controller in it.
This is my method:
private void forgotAccount (ActionEvent event) {
try {
Node node = (Node) event.getSource();
Stage stage = (Stage) node.getScene().getWindow();
Scene scene = stage.getScene();
FXMLLoader loader = new FXMLLoader(Login.class.getResource("/mattiashellkvist/forgotAccount.fxml"));
loader.setController(new ForgotAccountController());
AnchorPane root = (AnchorPane) loader.load();
scene.setRoot(root);
stage.sizeToScene();
}
catch (IOException e) {
System.out.println("Something went wrong");
}
}
code is correct...please check your fxml location(which you provided to FXMLLoader) and fxml file for any error...and in catch use e.getMessage() to get detailed problem (i.e. System.out.println("Something went wrong "+e.getMessage());)

Resources