8

似乎 WindowEvent.WINDOW_SHOWN 永远不会在场景图中的任何节点上分派,也没有(我能找到)知道节点何时可见/渲染/显示。例如:

测试启动器.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();
    }
}

测试控制器.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<WindowEvent>()
        {
            @Override
            public void handle(WindowEvent window)
            {
                usernameInput.requestFocus();
            }
        });

        root.addEventHandler(WindowEvent.WINDOW_SHOWN, new EventHandler<WindowEvent>()
        {
            @Override
            public void handle(WindowEvent window)
            {
                usernameInput.requestFocus();
            }
        });
    }

    public void handleWindowShownEvent()
    {
        usernameInput.requestFocus();
    }
}

测试视图.fxml

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

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

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

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

    prefHeight="150"
    prefWidth="200"
>
    <children>
        <TextField fx:id="serverAddressInput" />
        <TextField fx:id="usernameInput" />
    </children>
</VBox>

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

4

2 回答 2

7

我想可能的解决方案之一是将以下方法添加到TestController.java

    public void handleWindowShownEvent()
    {
        usernameInput.requestFocus();
    }

然后将TestLauncherstart中的方法改成如下:

    @Override
    public void start(Stage stage) throws Exception
    {
        FXMLLoader loader = new FXMLLoader();
        Parent root = (Parent)loader.load(TestController.class.getResourceAsStream("TestView.fxml"));
        final TestController controller = (TestController)loader.getController();
        stage.addEventHandler(WindowEvent.WINDOW_SHOWN, new EventHandler<WindowEvent>()
        {
            @Override
            public void handle(WindowEvent window)
            {
                controller.handleWindowShownEvent();
            }
        });
        Scene scene = new Scene(root);
        stage.setScene(scene);
        stage.show();
    }

我真的很欢迎其他解决方案,因为这个解决方案似乎太笨重了......

于 2012-01-20T16:38:49.447 回答
4

不可否认,另一个解决方案不是很性感,但将节点与应用程序分离:

root.sceneProperty().addListener(new ChangeListener<Scene>() {
  @Override
  public void changed(ObservableValue<? extends Scene> observable, Scene oldValue, Scene newValue) {
    newValue.windowProperty().addListener(new ChangeListener<Window>() {
      @Override
      public void changed(ObservableValue<? extends Window> observable, Window oldValue, Window newValue) {
        newValue.addEventHandler(WindowEvent.WINDOW_SHOWN, new EventHandler<WindowEvent>() {
          @Override
          public void handle(WindowEvent event) {
            usernameInput.requestFocus();
          }
        });
      }
    });
  }
});

在我的情况下更有意义。

于 2015-03-20T14:55:24.280 回答