The main page operation on the javafx dual screen refreshes the content display of the secondary page in real time

I am also a novice of javafx, because the company wants to develop the client to use javafx technology, share some pits encountered in the development for everyone to understand, not much to talk about, directly on the code

**

The first is to click the button to pop up two windows

**`

try {
			//点击按钮正常跳转fxml页面的逻辑
			FXMLLoader loader = new FXMLLoader();
	        loader.setLocation(Main.class.getResource("view/answer/AnswerView.fxml"));
	        AnchorPane fingerView = (AnchorPane) loader.load();
	        AnswerController controller = loader.getController();
	        controller.rootVbox=rootVbox;
	        controller.setMain(main);
	        
	        //如果当前存在第二个窗口,先关闭
	        Stage stage2 = StageManager.STAGE.get("second");
	        if(stage2 !=null) {
	        	stage2.close();
	        }
	        
		        FXMLLoader loader2 = new FXMLLoader();
		   
	        	Stage stage = new Stage();
            	AsrUtils.getInstance().url=AsrUtils.getInstance().proMap.get("address").toString()+"/assessmentInHeart/toPsychologicalTest?id="+AsrUtils.getInstance().id+"&article_id=1&type=0&returnAddress=detailed";
            	loader2.setLocation(Main.class.getResource("view/answer/Guide.fxml"));
    	        AnchorPane yindao = (AnchorPane) loader2.load();
    	        
    	        GuideController controller2 = loader2.getController();
    	        //将第二个窗口放入管理类中
    	        StageManager.STAGE.put("second", stage);
//    	        rootVbox.getChildren().add(fingerView);
    	        controller2.setMain(main);
    			stage.setScene(new Scene(yindao));
    			stage.show();
	        
		} catch (Exception e) {
			e.printStackTrace();
		}

`

In the above StageManager.STAGE.put("second", stage); this is a stage management class I created

public class StageManager {
	//Stage容器存储第二个窗口,用来点击第一个窗口中的按钮时方便操作第二个窗口
	public static Map<String, Stage> STAGE=new HashMap<String, Stage>();
	//controller管理类实际应用中其实没用到
	public static Map<String, Object> CONTROLLER=new HashMap<String, Object>();

}

Now it has been realized that clicking the button to pop up two windows, the second window can be manually dragged to the other screen of the dual screen (the dual screen machine can be set to display different things on the two screens normally, Baidu is very simple).

Click the button in the first window. If the fxml page used by the first window is used, the button can be directly obtained in the background controller. The first window here is an embedded html page, so first pass the callback to the html page Object

public class ArchivesController extends BaseView implements Initializable {

	
	@FXML
	private WebView webView;
	private WebEngine webEngine;
	private OpenAppUtil openAppUtil = new OpenAppUtil();
	
	@Override
	public void initialize(URL location, ResourceBundle resources) {
		if(webView != null) {
			
		}else {
			webView= new WebView();
		}
		if(webEngine != null) {
			
		}else {
			webEngine = webView.getEngine();
		}
		webEngine.getLoadWorker().stateProperty().addListener(new ChangeListener<State>() {
			public void changed(ObservableValue ov, State oldState, State newState) {

				if (newState == Worker.State.SUCCEEDED) { // 页面加载成功
					 JSObject win = (JSObject) webEngine.executeScript("window");
					 	//把页面回调的对象传到html页面
		                win.setMember("openAppUtil", openAppUtil);
				}
			}
		});
		
		//加载html页面AsrUtils.getInstance().proMap.get("address").toString()是我再配置文件中定义的html访问的前缀
		webEngine.load(AsrUtils.getInstance().proMap.get("address").toString()+"/test/index");
	}
	@Override
	public void setMain(Main main) {
		this.main = main;
		//设置页面的高度可以写死,可以忽略
		webView.setPrefHeight(main.rootLayout.getHeight() - 70);
		
	}
	
	//html 页面回调方法
   public class OpenAppUtil {
    	public void toFxml() {
    		
    		//从stage容器中取出第二个窗口,这里就可以操作第二个窗口了
    		Stage stage2 = StageManager.STAGE.get("second");
    		stage2.close();
    		try {
    		    //操作第二窗口去加载一个页面
    			FXMLLoader loader = new FXMLLoader();
    	        loader.setLocation(Main.class.getResource("view/answer/AnswerView.fxml"));
    	        AnchorPane fingerView = (AnchorPane) loader.load();

    	        rootVbox.getChildren().add(fingerView);
    	        
    	        AnswerController controller = loader.getController();
    	        
    	        
    	        controller.setMain(main);
    	        
    		} catch (Exception e) {
    			e.printStackTrace();
    		}
    	}
    }   
}


html page callback javafx code
The html page callback javafx code can directly use the openAppUtil. custom method.
This is basically achieved here. Operate in the first window to refresh the page in the second window in real time. If you have any questions, welcome to consult. I am a novice. If there is any misleading in the article, please criticize and correct me. .

Guess you like

Origin blog.csdn.net/Lele___/article/details/88662998