JavaFX:多进程应用程序中通过TCP/IP的焦点请求

JavaFX:多进程应用程序中通过TCP/IP的焦点请求,java,javafx,Java,Javafx,我试图从多个应用程序中将焦点设置为JavaFX文本字段 应用程序应该是多个应用程序的用户管理。从每个应用程序中,我希望能够请求显示和激活登录对话框,以便用户只需输入用户名和密码,无需进一步单击。因为不是所有的应用程序都是基于Java的,所以我通过TCP/IP实现了登录请求 焦点请求似乎适用于textfield。textfield看起来好像有焦点,但我没有将焦点转移到应用程序。有什么建议吗 随附两个示例应用程序以演示该行为。首先启动示例登录对话框,然后启动示例外部应用程序。单击login按钮会更改

我试图从多个应用程序中将焦点设置为JavaFX文本字段

应用程序应该是多个应用程序的用户管理。从每个应用程序中,我希望能够请求显示和激活登录对话框,以便用户只需输入用户名和密码,无需进一步单击。因为不是所有的应用程序都是基于Java的,所以我通过TCP/IP实现了登录请求

焦点请求似乎适用于textfield。textfield看起来好像有焦点,但我没有将焦点转移到应用程序。有什么建议吗

随附两个示例应用程序以演示该行为。首先启动示例登录对话框,然后启动示例外部应用程序。单击login按钮会更改登录对话框中的焦点,但是如果不单击示例登录对话框,我无法在文本字段中输入文本

示例登录对话框

import javafx.application.Application;
import javafx.application.Platform;
import javafx.geometry.Insets;
import javafx.scene.Scene;
import javafx.scene.control.Label;
import javafx.scene.control.PasswordField;
import javafx.scene.control.TextField;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;

import java.net.ServerSocket;
import java.net.Socket;
import java.util.Scanner;

public class FocusApplication extends Application {

    @Override
    public void start(Stage primaryStage) throws Exception {

        TextField userField = new TextField();
        PasswordField passwordField = new PasswordField();

        VBox vbox = new VBox(5);
        vbox.setPadding(new Insets(20));
        vbox.getChildren().addAll(new Label("User"), userField, new Label("Password"), passwordField);

        primaryStage.setTitle("Request Focus");
        primaryStage.setScene(new Scene(vbox, 400, 300));

        Thread thread = new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    ServerSocket serverSocket = new ServerSocket(1234);
                    Socket client = serverSocket.accept();

                    Scanner in = new Scanner(client.getInputStream());

                    while (true) {
                        String command = in.nextLine();
                        if ("login".equals(command)) {
                            System.out.println("login recieved");
                            Platform.runLater(new Runnable() {
                                @Override
                                public void run() {
                                    primaryStage.show();
                                    primaryStage.toFront();
                                    primaryStage.requestFocus();
                                    if (userField.isFocused()) {
                                        passwordField.requestFocus();
                                    } else {
                                        userField.requestFocus();
                                    }
                                }
                            });
                        }
                    }
                } catch (Exception ex) {
                    ex.printStackTrace();
                }
            }
        });
        thread.start();
        primaryStage.show();
    }
}
外部应用程序示例

import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.layout.HBox;
import javafx.stage.Stage;

import java.io.PrintWriter;
import java.net.Socket;

public class TriggerFocusApplication extends Application {

    @Override
    public void start(Stage primaryStage) throws Exception {
        Socket server = new Socket("localhost",  1234);
        PrintWriter out = new PrintWriter(server.getOutputStream(), true);

        Button triggerButton = new Button("Login");
        triggerButton.setOnAction(new EventHandler<ActionEvent>() {
            @Override
            public void handle(ActionEvent event) {
                out.println("login");
            }
        });

        HBox hbox = new HBox(5);
        hbox.getChildren().add(triggerButton);
        primaryStage.setTitle("Trigger Focus Request");
        primaryStage.setScene(new Scene(hbox, 400, 300));
        primaryStage.show();
    }
}
导入javafx.application.application;
导入javafx.event.ActionEvent;
导入javafx.event.EventHandler;
导入javafx.scene.scene;
导入javafx.scene.control.Button;
导入javafx.scene.layout.HBox;
导入javafx.stage.stage;
导入java.io.PrintWriter;
导入java.net.Socket;
公共类TriggerFocusApplication扩展了应用程序{
@凌驾
public void start(Stage primaryStage)引发异常{
套接字服务器=新套接字(“localhost”,1234);
PrintWriter out=新的PrintWriter(server.getOutputStream(),true);
按钮触发按钮=新按钮(“登录”);
setOnAction(新的EventHandler(){
@凌驾
公共无效句柄(ActionEvent事件){
out.println(“登录”);
}
});
HBox HBox=新的HBox(5);
hbox.getChildren().add(triggerButton);
primaryStage.setTitle(“触发焦点请求”);
初级阶段。设置场景(新场景(hbox,400300));
primaryStage.show();
}
}