在javafx中将对象发送到其他屏幕
我是javafx的新手,到目前为止进展顺利。但是我找不到任何形式可以将一个对象从一个屏幕发送到另一个屏幕。我很难理解函数od注释在javafx中将对象发送到其他屏幕,java,javafx,Java,Javafx,我是javafx的新手,到目前为止进展顺利。但是我找不到任何形式可以将一个对象从一个屏幕发送到另一个屏幕。我很难理解函数od注释@FXML和初始化接口中的方法初始化 调用另一个 public class FluxoCaixaController extends ParametrosTelas implements iTelaPrincipalFX { /*Atributos locais*/ private ObservableList<String> opcoes
@FXML
和初始化接口中的方法初始化
调用另一个
public class FluxoCaixaController extends ParametrosTelas implements iTelaPrincipalFX {
/*Atributos locais*/
private ObservableList<String> opcoes = FXCollections.observableArrayList("Receita", "Despesa");
private Object parent;
private AberturaDeTelasFX formaAbertura;
private ToggleGroup modalGroup = new ToggleGroup();
private Categoria categoria;
private CategoriaTreeViewController root = new CategoriaTreeViewController();
@Override
public void showScreen() {
formaAbertura = new AberturaDialogFX();
formaAbertura.loadFXML(bundle, icone, bundle.getString("screnn.fluxo.title"), new AnchorPane(), "FluxoCaixa.fxml");
}
@Override // This method is called by the FXMLLoader when initialization is complete
public void initialize(URL fxmlFileLocation, ResourceBundle resources) {
tipoField.getItems().setAll(opcoes);
root.setParentController(getInstance());
//idColumn.setCellValueFactory(new PropertyValueFactory<>("id"));
//descricaoColumn.setCellValueFactory(new PropertyValueFactory<>("descricao"));
//atualizaTabela();
}
@FXML
private void btnAddCetegoria() {
root.showScreen();
categoriaSubcategoriaField.setText(categoria.getDescricao());
}
private Object getInstance(){
return this;
}
ge生成的方法体,选择工具|模板。
}
}查看您的代码时,我没有找到扩展应用程序的类。
在我最近构建的项目中,我将其用作表示层的一部分,表示层是控制器(表示控制器层)之间的桥梁,每个控制器管理一个屏幕。考虑下面的例子:
public class MainAppFX extends Application {
// The primary window or frame of this application
private Stage primaryStage;
此“患者”表示控制器中的对象:
private PatientData patient;
/**
* Default constructor
*/
public MainAppFX() {
super();
}
/**
* The application starts here
*
* @param primaryStage
* @throws Exception
*/
@Override
public void start(Stage primaryStage) throws Exception {
log.info("Program loads");
// The Stage comes from the framework so make a copy to use elsewhere
this.primaryStage = primaryStage;
// Create the Scene and put it on the Stage
loadPatientParentWindow();
// Set the window title
this.primaryStage.setTitle("Your Window title");
this.primaryStage.show();
}
/**
* Loads Patient FXML layout
* This method loads one of your screens
*/
public void loadPatientParentWindow() {
try {
// Instantiate the FXMLLoader
FXMLLoader loader = new FXMLLoader();
// Set the location of the fxml file in the FXMLLoader
loader.setLocation(MainAppFX.class.getResource("/fxml/PatientForm.fxml"));
// Parent is the base class for all nodes that have children in the
// scene graph such as AnchorPane and most other containers
Parent parent = (AnchorPane) loader.load();
// Load the parent into a Scene
Scene scene = new Scene(parent);
// Put the Scene on Stage
primaryStage.setScene(scene);
// Give the PatientFXMLController controller access to the main app.
PatientFXMLController controller = loader.getController();
下面是Patient controller的setter方法,在这里它可以访问主类
controller.setMainAppFX(this);
} catch (IOException | SQLException ex) { // getting resources or files could fail
log.error(null, ex);
System.exit(1);
}
}
/**
* Setter for PatientData object in the Main class
您可以在控制器类中使用它
*
* @param patient
*/
public void setPatient(PatientData patient) {
this.patient = patient;
}
/**
* Getting PatientData object from the Main class
*
* @return
*/
public PatientData getPatient() {
return patient;
}
/**
* Where it all begins
*
* @param args command line arguments
*/
public static void main(String[] args) {
launch(args);
System.exit(0);
}
}
下面是一个控制器类,它代表一个屏幕:
public class PatientFXMLController {
对主应用程序的引用从这里开始
private MainAppFX mainApp;
下一个对象将使用setter传递给主类
private PatientData patient;
// The @FXML annotation on a class variable results in the matching
// reference being injected into the variable
// label is defined in the fxml file
// Bunch of @FXML annotations with respective fields (you can get them from SceneBuilder)
@FXML
private TextField patientIdField;
@FXML
private TextField lastNameField;
// so on...
/**
* The constructor. The constructor is called before the initialize()
* method. You don't need to call it. It's being called by automatically
*/
public PatientFXMLController() {
super();
为初始化方法创建空的患者对象
patient = new PatientData();
}
/**
* Initializes the controller class. This method is automatically called
* after the fxml file has been loaded. Useful if a control must be
* dynamically configured such as loading data into a table.
*/
@FXML
private void initialize() {
log.info("initialize called");
// Initializing form fields
Bindings.bindBidirectional(patientIdField.textProperty(), patient.patientIdProperty(), new NumberStringConverter());
// so on .....
}
/**
* Loads Another screen
下面的这个方法表示一个按钮的操作,它将导致另一个屏幕,在这里您将一个对象从这个控制器传递到主类。然后您可以将这个对象从主类传递到另一个Screen控制器,因为这里有它的loadAnotherScreenWindow()方法
*
* @param event
*/
@FXML
private void loadAnotherScreen(ActionEvent event) throws SQLException {
将Patient对象传递给主类,以便可以在另一个屏幕控制器中使用它来获取相关的住院患者
mainApp.setPatient(patient);
}
/**
* Is called by the main application to give a reference back to itself.
* This class receives the reference of the main class.
*
* @param mainApp
*/
public void setMainAppFX(MainAppFX mainApp) {
this.mainApp = mainApp;
}
/**
* Setter for PatientData object
*
* @param patient
*/
public void setPatient(PatientData patient) {
this.patient = patient;
}
}
从阅读类的文档开始。@VGR我放置了显示UI的类。我说的是屏幕,但我的意思是,我看到我的问题是接口初始化。现在我可以发送一个对象,但我必须初始化每个对象。
patient = new PatientData();
}
/**
* Initializes the controller class. This method is automatically called
* after the fxml file has been loaded. Useful if a control must be
* dynamically configured such as loading data into a table.
*/
@FXML
private void initialize() {
log.info("initialize called");
// Initializing form fields
Bindings.bindBidirectional(patientIdField.textProperty(), patient.patientIdProperty(), new NumberStringConverter());
// so on .....
}
/**
* Loads Another screen
*
* @param event
*/
@FXML
private void loadAnotherScreen(ActionEvent event) throws SQLException {
mainApp.setPatient(patient);
}
/**
* Is called by the main application to give a reference back to itself.
* This class receives the reference of the main class.
*
* @param mainApp
*/
public void setMainAppFX(MainAppFX mainApp) {
this.mainApp = mainApp;
}
/**
* Setter for PatientData object
*
* @param patient
*/
public void setPatient(PatientData patient) {
this.patient = patient;
}
}