'Button ActionEvent btnsave of Save Button in Child Stage created using JavaFX in Eclipse not working
I working on a project which has multiple (stages). In the Primary Stage a dialog box is launched with options to be selected by user. One of the Options is New. When, New button is clicked in Dialog Box, next Stage is initiated and another window (Scene) is opened in child stage. The Save button in child stage does not work. The first system message also is not shown and it means no entry to btnsave() function. The actual project code is very large. I put in reduced form the Main.java, newpat.java and newpt.fxml files sequentially.
The main.java code:
package application;
import javafx.application.Application;
import java.io.*;
import javafx.stage.Modality;
import javafx.stage.Stage;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
static String pat_options[]= {"New","Existing"};
static Newpatcontrol newpat_controller = new Newpatcontrol();
static Control controller = new Control();
public static void main(String[] args)
{
launch(args);
}
@Override
public void start(Stage primaryStage) throws IOException
{
try {
FXMLLoader loader = new FXMLLoader();
URL url = this.getClass().getClassLoader().getResource("abc.fxml");
Parent root = FXMLLoader.load(url);
loader.setController(controller);
scene = new Scene(root);
scene.getStylesheets().add(getClass().getResource("application.css").toExternalForm());
primaryStage.setScene(scene);
primaryStage.setTitle("TEST");
primaryStage.sizeToScene();
primaryStage.setMaximized(true);
primaryStage.show();
pat_dialog = new ChoiceDialog(pat_options[0],pat_options[1]);
pat_dialog.setTitle("Patient ?");
pat_dialog.setHeaderText("Select Patient Option");
pat_dialog.setContentText("Which Patient ? :");
pat_dialog.showAndWait();
try{
String selected = pat_dialog.getResult().toString();
System.out.println("Selected Pat is : " + selected );
if(selected != null)
{
case("New"):{
FXMLLoader loader2 = new FXMLLoader();
URL urlnew = this.getClass().getClassLoader().getResource("newpat.fxml");
Parent root2 = FXMLLoader.load(urlnew);
loader2.setController(newpat_controller);
Stage stage2 = new Stage();
stage2.setTitle("New");
stage2.setScene(new Scene(root2));
stage2.show();
break;}
case("Guest"):{break;}
}
}
}catch(Exception pt)
{
pt.printStackTrace();
}
} catch(Exception e)
{
e.printStackTrace();
}
}
The Newpatcontrol.java file:
package application;
import javafx.scene.control.Button;
mport javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
public class Newpatcontrol implements Initializable {
@FXML
private AnchorPane newpat_ap;
@FXML
private Button save_btn;
@FXML
private void btnsave(ActionEvent event)
{
System.out.println("New Patient Save Button Click ");
insertrecord();
System.out.println("New Patient Inserted");
}
}
Newpat.fxml file:
<?xml version="1.0" encoding="UTF-8
<?import javafx.scene.control.Button?>
<AnchorPane fx:id="newpat_ap" maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="663.0" prefWidth="1235.0" xmlns="http://javafx.com/javafx/8.0.171" xmlns:fx="http://javafx.com/fxml/1" fx:controller="application.Newpatcontrol">
<children>
<Button id="save" fx:id="save_btn" layoutX="639.0" layoutY="587.0" mnemonicParsing="false" onAction="#btnsave" prefHeight="45.0" prefWidth="75.0" text="SAVE" textFill="#020302" AnchorPane.bottomAnchor="31.0" AnchorPane.topAnchor="587.0">
<font>
<Font size="20.0" />
</font></Button>
</children>
</AnchorPane>
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
