java windowevent_事件处理 – 如何在场景图的节点中监听WindowEvent.WINDOW_SHOWN?

似乎WindowEvent.WINDOW_SHOWN永远不会在场景图中的任何节点上调度,也无论如何(我可以找到)知道节点何时可见/呈现/显示.例如:

TestLauncher.java

package com.example.javafx.event;

import javafx.application.Application;

import javafx.fxml.FXMLLoader;

import javafx.scene.Parent;

import javafx.scene.Scene;

import javafx.stage.Stage;

public class TestLauncher extends Application

{

public static void main(String[] args)

{

Application.launch(TestLauncher.class,args);

}

@Override

public void start(Stage stage) throws Exception

{

Parent root = FXMLLoader.load(TestController.class.getResource("TestView.fxml"));

Scene scene = new Scene(root);

stage.setScene(scene);

stage.show();

}

}

TestController.java

package com.example.javafx.event;

import java.net.URL;

import java.util.ResourceBundle;

import javafx.event.EventHandler;

import javafx.fxml.FXML;

import javafx.fxml.Initializable;

import javafx.scene.Parent;

import javafx.scene.control.TextField;

import javafx.stage.WindowEvent;

public class TestController implements Initializable

{

@FXML private Parent root;

@FXML private TextField serverAddressInput;

@FXML private TextField usernameInput;

@Override

public void initialize(URL url,ResourceBundle rb)

{

serverAddressInput.setText("127.0.0.1");

//won't work because stage isn't visible yet

trySetFocusOnUsernameInput1();

//apparently Stage never passes on any WindowEvents to the children...

trySetFocusOnUsernameInput2();

}

private void trySetFocusOnUsernameInput1()

{

usernameInput.requestFocus();

}

private void trySetFocusOnUsernameInput2()

{

root.addEventFilter(WindowEvent.WINDOW_SHOWN,new EventHandler()

{

@Override

public void handle(WindowEvent window)

{

usernameInput.requestFocus();

}

});

root.addEventHandler(WindowEvent.WINDOW_SHOWN,new EventHandler()

{

@Override

public void handle(WindowEvent window)

{

usernameInput.requestFocus();

}

});

}

public void handleWindowShownEvent()

{

usernameInput.requestFocus();

}

}

TestView.fxml

xmlns:fx="http://javafx.com/fxml"

fx:id="root"

fx:controller="com.example.javafx.event.TestController"

prefHeight="150"

prefWidth="200"

>

那么,实际上,节点如何才能意识到它是可见/渲染/显示的事实?

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值