Skip to Main Content

Java SE (Java Platform, Standard Edition)

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

And dynamic load fxml file problem?

928958Sep 10 2012 — edited Sep 10 2012
The first handleButtonAction(){ } the label has value !!
In handleButtonAction() {} I dynamically loaded new fxml, new ui controls, while adding a new button event.


But the secondhandleButtonAction2(){ } the label is <font color="red">null </font> ?

----------------------
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package javafxapplication25;

import java.io.IOException;
import java.net.URL;
import java.util.ResourceBundle;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.fxml.Initializable;
import javafx.scene.Parent;
import javafx.scene.control.Label;
import javafx.scene.layout.AnchorPane;

/**
*
* @author Administrator
*/
public class SampleController implements Initializable {
<font color="red">
@FXML
private Label label,</font> label2;
@FXML
AnchorPane a1;
<font color="red"> @FXML
private void handleButtonAction(ActionEvent event) throws IOException {

Parent root2 = FXMLLoader.load(getClass().getResource("Sample_1.fxml"));
</font>

label.setText("This label in Sample.fxml is "+ label);
System.out.println(label);
a1.getChildren().add(root2);

}
<font color="red"> @FXML
private void handleButtonAction2(ActionEvent event) {
System.out.println(label);
label2.setText("This label in @FXML is "+label);
}</font>
@Override
public void initialize(URL url, ResourceBundle rb) {
// TODO
}
}
-----------------------
Sample.fxml
<?xml version="1.0" encoding="UTF-8"?>
<?import java.lang.*?>
<?import java.util.*?>
<?import javafx.scene.*?>
<?import javafx.scene.control.*?>
<?import javafx.scene.layout.*?>

<AnchorPane id="AnchorPane" prefHeight="400" prefWidth="620" xmlns:fx="http://javafx.com/fxml" fx:controller="javafxapplication25.SampleController" fx:id="a1">
<children>
<Button layoutX="126" layoutY="90" text="button111" onAction="#handleButtonAction" fx:id="button111" />
<Label layoutX="126" layoutY="120" minHeight="16" minWidth="69" fx:id="label" text=" label"/>
</children>
</AnchorPane>

------------------
Sample_1.fxml

<?xml version="1.0" encoding="UTF-8"?>
<?import java.lang.*?>
<?import java.util.*?>
<?import javafx.scene.*?>
<?import javafx.scene.control.*?>
<?import javafx.scene.layout.*?>
<AnchorPane id="AnchorPane2" prefHeight="200" prefWidth="320" xmlns:fx="http://javafx.com/fxml" fx:controller="javafxapplication25.SampleController">
<children>
<Button layoutX="186" layoutY="190" text="button2" onAction="#handleButtonAction2" fx:id="button222" />
<Label layoutX="186" layoutY="320" minHeight="16" minWidth="69" fx:id="label2" />
</children>
</AnchorPane>


------------------

package javafxapplication25;

import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.stage.Stage;

/**
*
* @author Administrator
*/
public class JavaFXApplication25 extends Application {

@Override
public void start(Stage stage) throws Exception {
Parent root = FXMLLoader.load(getClass().getResource("Sample.fxml"));

Scene scene = new Scene(root);

stage.setScene(scene);
stage.show();
}

/**
* The main() method is ignored in correctly deployed JavaFX application.
* main() serves only as fallback in case the application can not be
* launched through deployment artifacts, e.g., in IDEs with limited FX
* support. NetBeans ignores main().
*
* @param args the command line arguments
*/
public static void main(String[] args) {
launch(args);
}
}


Edited by: user7993481 on 2012-9-10 上午8:38

Edited by: user7993481 on 2012-9-10 上午8:39
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Oct 8 2012
Added on Sep 10 2012
2 comments
236 views