2017-07-22 14 views
0

ラベルとボタンがあるウィンドウと、TextFieldとボタンがある別のウィンドウがあります。メインウィンドウから、ボタンを使用して別のウィンドウを開きたい場合は、新しいウィンドウのテキストフィールドに何かを入力して、新しいウィンドウのボタンをクリックした後、閉じるためにメインウィンドウのラベルをテキストで更新します入力された。また、私は新しいウィンドウをモーダルにしたい。JavaFXとFXML - 別のコントローラからのデータでラベルを更新する

Main Window

New Window

public class MainController { 

    @FXML 
    public void showNewWindow() { 
     try { 
      Stage newWindowStage = new Stage(); 
      newWindowStage.setTitle("New Window"); 
      newWindowStage.initModality(Modality.APPLICATION_MODAL); 
      VBox root = FXMLLoader.load(getClass().getResource("newWindow.fxml")); 
      Scene scene = new Scene(root); 
      newWindowStage.setScene(scene); 
      newWindowStage.showAndWait(); 
     } catch (IOException e) { 
      e.printStackTrace(); 
     } 
    } 
} 


public class NewWindowController { 

    @FXML 
    private TextField textField; 

    @FXML 
    public void updateMainWindowLabel() { 
     // update label in main window 
     // close new window 
    } 
} 

私はそれが右のすべてを設定されていない知っているが、うまくいけば、それは私がやろうとしているかを説明。ここで

答えて

0

作品のサンプルコード(私は編集をいただければ幸いですので、それがneatestではありません)

は、私はそれに関連した別の質問を見つけた私は、モーダルウィンドウを完全に理解しているが、hereないことです。アプリケーションを拡張

メインクラス:

public static void main(String[] args) { 
    Application.launch(Test.class, args); 
} 

private static Stage stage1; 
private static Stage stage2;  
private static MyExampleController controller; 

@Override 
public void start(Stage primaryStage) throws Exception { 
    stage1 = primaryStage;   
    FXMLLoader loader1 = new FXMLLoader(Test.class.getResource("/test/MyExample.fxml")); 
    AnchorPane pane1 = (AnchorPane)loader1.load(); 
    controller = loader1.getController(); 
    Scene scene1 = new Scene(pane1); 
    stage1.setScene(scene1); 

    stage2 = new Stage(); 
    FXMLLoader loader2 = new FXMLLoader(Test.class.getResource("/test/MyEx2.fxml")); 
    AnchorPane pane2 = (AnchorPane)loader2.load(); 
    Scene scene2 = new Scene(pane2); 
    stage2.setScene(scene2); 

    stage1.show(); 
} 

public static Stage getStage2(){ 
    return stage2; 
} 

public static MyExampleController getController(){ 
    return controller; 
} 

Controllerクラス1:

public class MyExampleController implements Initializable { 

@FXML 
private Label labLab; 
@FXML 
private Button btnButton; 

@Override 
public void initialize(URL url, ResourceBundle rb) {  
}  

@FXML 
private void clickButton(ActionEvent event) { 
    Test.getStage2().show(); 
} 

public Label getLabel(){ 
    return labLab; 
} 
} 

Controllerクラス2:

public class MyEx2Controller implements Initializable { 

@FXML 
private Button btnUpdate; 
@FXML 
private TextField txtField; 

@Override 
public void initialize(URL url, ResourceBundle rb) { 
}  

@FXML 
private void doUpdateForTitle(ActionEvent event) { 
    Test.getController().getLabel().setText(txtField.getText()); 
    Test.getStage2().close(); 
} 

} 
1

あなたは(あなたがやりたい2つの段階にする必要はありませんわずか2シーン)。

Stage st 
st.setOnCloseRequest(e -> { 

}); 

あなたのステージが閉じるとき(newWindow)に任意のコードをプリフォームできます。

次に、TextFieldオブジェクトでgetText()を使用し、setText()をメインステージのLabelオブジェクトに使用します。

st.initModality(Modality.APPLICATION_MODAL); 

...モーダルにします。

関連する問題