简体   繁体   中英

javafx How can I change/refresh my primary scene and its controls based on changes made in secondary scene?

What I am looking to do is have changes made in a secondary scene populate controls in my primary scene when switching back to it. The code shown below details a simplified version of what I am trying to do. On my primary scene I have 4 check boxes with names, for example someone choosing from those names to invite them to a party. But I have added an edit button at the bottom that will take the user to a second scene where they can add a new string to the list that made the original 4 checkboxes. On switching back to the primary scene I want the new name to be in a checkbox. I realize that I will have to completely rethink/overhaul my code, but I cannot think of a way to do this. Is this possible and can someone help?

// EditSceneTesting

import java.io.*;
import java.util.Scanner;
import java.util.ArrayList;
import javafx.application.Application;
import javafx.stage.Stage;
import javafx.scene.Scene;
import javafx.scene.control.*;
import javafx.scene.layout.*;
import javafx.event.*;

public class EditSceneTesting extends Application {
   private static ArrayList<String> objects;
   private Scene scene1;
   private Scene scene2;
   
   public static void main(String[] args) throws FileNotFoundException {
      File inputFile = new File("data.txt");
      Scanner inFile = new Scanner(inputFile);
      objects = new ArrayList<String>();
      
      while (inFile.hasNextLine()) {
         String line = inFile.nextLine();
         
         if (line.equals("")) {
            continue;
         }
         
         objects.add(line);
      }
      
      launch(args);
   }
   
   @Override
   public void start(Stage primaryStage) {
      // scene 1
      VBox checkBoxVBox = new VBox(5);
      
      for (String obj : objects) {
         CheckBox checkBox = new CheckBox(obj);
         checkBoxVBox.getChildren().addAll(checkBox);
      }
      
      Button addObjectsButton = new Button("Add Objects");
      AddObjectsButtonHandler addObjectsHandler = new AddObjectsButtonHandler(primaryStage);
      addObjectsButton.setOnAction(addObjectsHandler);
      
      VBox scene1Container = new VBox(20,checkBoxVBox,addObjectsButton);
      scene1 = new Scene(scene1Container,300,300);
      
      // scene 2
      Label objectLabel = new Label("Object:");
      TextField objectTextField = new TextField();
      
      HBox newObjectHBox = new HBox(5,objectLabel,objectTextField);
      
      Button addButton = new Button("Add");
      AddButtonHandler addHandler = new AddButtonHandler(objectTextField);
      addButton.setOnAction(addHandler);
      
      Button returnButton = new Button("Return to CheckBox Screen");
      ReturnButtonHandler returnHandler = new ReturnButtonHandler(primaryStage);
      returnButton.setOnAction(returnHandler);
      
      VBox scene2Container = new VBox(20,newObjectHBox,addButton,returnButton);
      scene2 = new Scene(scene2Container,300,300);
      
      primaryStage.setTitle("Edit Scene Testing");
      primaryStage.setScene(scene1);
      primaryStage.show();
   }
   
   public class AddObjectsButtonHandler implements EventHandler<ActionEvent> {
      private Stage primaryStage;
      
      public AddObjectsButtonHandler(Stage primStage) {
         primaryStage = primStage;
      }
      
      @Override
      public void handle(ActionEvent event) {
         primaryStage.setScene(scene2);
      }
   }
   
   public class AddButtonHandler implements EventHandler<ActionEvent> {
      private TextField objectTextField;
      
      public AddButtonHandler(TextField objectTF) {
         objectTextField = objectTF;
      }
      
      @Override
      public void handle(ActionEvent event) {
         // here can add to list or add to file whichever is best for solution
         objects.add(objectTextField.getText());
         
         objectTextField.clear();
      }
   }
   
   public class ReturnButtonHandler implements EventHandler<ActionEvent> {
      private Stage primaryStage;
      
      public ReturnButtonHandler(Stage primStage) {
         primaryStage = primStage;
      }
      
      @Override
      public void handle(ActionEvent event) {
         primaryStage.setScene(scene1);
      }
   }
}

If you aren't tied to the check boxes, you could create two lists and allow the user to drag and drop people onto an 'Invited' list. Populate the Names list, allow the user to add an item to this list ( you can use the secondary stage, or even an Alert popup ) and add the inputted name onto the list. Then they can drag any name to the wanted list. Checkout this example

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM