通过单击新按钮在JavaFX中打开一个新窗口后,我想初始化一些东西。怎么样?

时间:2015-11-30 03:51:45

标签: java javafx javafx-8 fxml

我有一个应用程序,我想在打开一个新的JavaFX框后初始化一些东西。 我意识到这被标记为重复,但我不知道这个重复的答案如何帮助我。答案让我困惑,我只是想在新打开的窗口上初始化一些东西

以下是此新窗口的代码:

从第一个窗口打开另一个窗口的方法:

@FXML
    public void displayAddNewPerson() {

        FXMLLoader fxmlLoader = new FXMLLoader(getClass().getResource("CreateNewStudent.fxml"));
        Parent root;
        AddNewPersonToFile activateMethod = new AddNewPersonToFile();
        try {
            root = fxmlLoader.load();
            Stage addNewPersonStage = new Stage();
            addNewPersonStage.initModality(Modality.APPLICATION_MODAL);
            addNewPersonStage.setOpacity(1);
            addNewPersonStage.setTitle("My New Stage Title");
            addNewPersonStage.setScene(new Scene(root, 500, 300));
            addNewPersonStage.show();
            addNewPersonStage.hide();
            //activateMethod.initializeNewPerson();
            addNewPersonStage.showAndWait();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }


    }

是新窗口的类:

public class AddNewPersonToFile extends AnchorPane implements Initializable {
    @FXML
    final ObservableList<String> stateList =FXCollections.observableArrayList(
            "AL", "AK", "AZ", "AR", "CA", "CO", "CT", "DE", "FL", "GA", "HI", "ID", "IL", "IN", 
            "IA","KS", "KY", "LA", "ME", "MD", "MA", "MI", "MN", "MS", "MO", "MT", "NE", "NV", 
            "NH", "NJ", "NM", "NY", "NC", "ND", "OH", "OK", "OR", "PA", "RI", "SC", "SD", "TN", 
            "TX","UT", "VT", "VA", "WA", "WV", "WI", "WY");
@FXML
private TextField enterLastName;
@FXML
private TextField enterFirstName;
@FXML
private TextField enterEmail;
@FXML
private TextField enterMoneyOwed;
@FXML
private TextField enterSchoolName;
@FXML
private ComboBox<String> enterState; 
@FXML
private ComboBox<Integer> enterYearJoined;
@FXML
private CheckBox enterIsHeActive;
@FXML
private Button enterStudentInfo;

@Override
public void initialize(URL location, ResourceBundle resources) {
    // TODO Auto-generated method stub



    int year = Calendar.getInstance().get(Calendar.YEAR);
    ObservableList<Integer> yearRange = FXCollections.observableArrayList((year - 4), (year - 3), (year - 2), (year - 1), (year), (year + 1), (year + 2), (year + 3), (year + 4));
    enterIsHeActive.setIndeterminate(false);
    System.setProperty("glass.accessible.force", "false");
    enterState.setVisibleRowCount(4);
    enterState.setItems(stateList);
    enterYearJoined.setItems(yearRange);
    enterYearJoined.setVisibleRowCount(4);


}

}

这是新窗口的FXML:

<?xml version="1.0" encoding="UTF-8"?>

<?import javafx.scene.text.*?>
<?import javafx.scene.control.*?>
<?import java.lang.*?>
<?import javafx.scene.layout.*?>
<?import javafx.scene.layout.AnchorPane?>
<?import javafx.scene.layout.AnchorPane?>

<AnchorPane xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1" fx:controller="fbla.rahulshah.database.view.AddNewPersonToFile">
   <children>
      <AnchorPane layoutY="1.0" maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="300.0" prefWidth="500.0">
         <children>
            <Label layoutX="6.0" layoutY="6.0" text="Create New Student">
               <font>
                  <Font size="32.0" />
               </font>
            </Label>
            <TextField fx:id="enterLastName" layoutX="14.0" layoutY="70.0" prefHeight="25.0" prefWidth="150.0" promptText="Last Name">
               <font>
                  <Font size="9.0" />
               </font>
            </TextField>
            <TextField fx:id="enterFirstName" layoutX="170.0" layoutY="70.0" prefHeight="25.0" prefWidth="141.0" promptText="First Name">
               <font>
                  <Font size="9.0" />
               </font>
            </TextField>
            <TextField fx:id="enterEmail" layoutX="316.0" layoutY="70.0" prefHeight="25.0" prefWidth="176.0" promptText="Email">
               <font>
                  <Font size="9.0" />
               </font>
            </TextField>
            <Label layoutX="14.0" layoutY="53.0" text="Personal Info" />
            <Label layoutX="19.0" layoutY="147.0" text="School Info" />
            <TextField fx:id="enterSchoolName" layoutX="14.0" layoutY="166.0" prefHeight="25.0" prefWidth="150.0" promptText="School's Full Name">
               <font>
                  <Font size="9.0" />
               </font>
            </TextField>
            <CheckBox fx:id="enterIsHeActive" layoutX="16.0" layoutY="209.0" mnemonicParsing="false" text="Check if Active member" />
            <Button layoutX="416.0" layoutY="261.0" mnemonicParsing="false" text="Enter Info" />
            <TextField fx:id="enterMoneyOwed" layoutX="191.0" layoutY="109.0" prefHeight="25.0" prefWidth="86.0" promptText="(i.e. 5.00 for $5.00)">
               <font>
                  <Font size="9.0" />
               </font>
            </TextField>
            <Label layoutX="20.0" layoutY="113.0" text="Amountt of Money Owed (in $)" />
            <ComboBox fx:id="enterState" layoutX="184.0" layoutY="166.0" prefHeight="25.0" prefWidth="100.0" promptText="Enter State" />
            <ComboBox fx:id="enterYearJoined" layoutX="303.0" layoutY="166.0" prefHeight="25.0" prefWidth="107.0" promptText="Year Joined" />
         </children>
      </AnchorPane>
   </children>
</AnchorPane>

1 个答案:

答案 0 :(得分:0)

Stage newStage;//declare a new stage out of the action event

@FXML
private void andNewBtnAction(ActionEvent event) throws IOException {
    newStage = new Stage();
    newStage.setTitle("Add New");
    //container of your parent FXML (you can set blur effect to your parent once your new window open )
    stackPane.setEffect(new GaussianBlur(10));

    newStage.setScene(new Scene(FXMLLoader.
            load(URL_TO_YOUR_FXML));
    newStage.setResizable(false);
    newStage.initModality(Modality.WINDOW_MODAL);
    newStage.initOwner(YOUR_PRIMARY_STAGE);
    newStage.show();
    newStage.setOnCloseRequest((WindowEvent t) -> {
        stackPane.setEffect(null);//remove the effect after you close the new window
        newStage = null;
    });
}