为了账号安全,请及时绑定邮箱和手机立即绑定

无法将焦点设置在 JavaFX SwingNode 内的 JTextPane 上

无法将焦点设置在 JavaFX SwingNode 内的 JTextPane 上

缥缈止盈 2021-09-15 14:18:31
我正在尝试将焦点设置在 JTextPane 上,以便在窗口打开时可以立即使用键盘进行编辑。但是,我所做的一切似乎都没有让 JTextPane 专注于启动。这只是将 JavaFX 与 Swing 结合使用的问题吗?import javafx.collections.FXCollections;import javafx.collections.ObservableList;import javafx.embed.swing.SwingNode;import javafx.event.ActionEvent;import javafx.fxml.FXML;import javafx.scene.control.ListView;import javax.swing.*;public class TestDialog {    @FXML    private ListView listView;    @FXML    private SwingNode node;    private ObservableList<Integer> obsList;    @FXML    public void initialize(){        JTextPane pane = new JTextPane();        SwingUtilities.invokeLater(() -> pane.requestFocusInWindow());        pane.setText("This issue is not reproducible in JDK 8 early-access build (8u172) which is yet to be released.");        node.setContent(pane);         obsList = FXCollections.observableArrayList();        for(int x = 0; x < 12; x++){            obsList.add(x);        }        listView.setItems(obsList);        node.setFocusTraversable(true);        node.requestFocus();        pane.requestFocus();        pane.grabFocus();    }    @FXML    private void removeItem(ActionEvent event) {        obsList.remove(0);    }}
查看完整描述

2 回答

?
jeck猫

TA贡献1909条经验 获得超7个赞

我不知道这是否有帮助,但下面是我根据示例代码制作的演示程序,出于某种原因,它对我有用:


import javafx.application.Application;

import javafx.application.Platform;

import javafx.collections.FXCollections;

import javafx.collections.ObservableList;

import javafx.embed.swing.SwingNode;

import javafx.event.ActionEvent;

import javafx.fxml.FXML;

import javafx.scene.Scene;

import javafx.scene.control.ListView;

import javafx.scene.layout.StackPane;

import javafx.stage.Stage;

import javax.swing.*;   


public class Test extends Application {


    @FXML

    private ListView listView;

    @FXML

    private SwingNode node;


    private ObservableList<Integer> obsList;


    public static void main(String[] args) {    

        launch(args);

    }


    @Override

    public void start(Stage arg0) throws Exception {

        initialize(arg0);

    }


    @FXML

    public void initialize(Stage stage){        

        JTextPane pane = new JTextPane();


        // The program runs the same no matter if one of the below two lines are used or if neither are used

        //SwingUtilities.invokeLater(() -> pane.requestFocusInWindow());

        //Platform.runLater(() -> {node.requestFocus();});


        pane.setText("This issue is not reproducible in JDK 8 early-access build (8u172) which is yet to be released.");


        node = new SwingNode();

        node.setContent(pane);

        obsList = FXCollections.observableArrayList();

        for(int x = 0; x < 12; x++){

            obsList.add(x);

        }

        listView = new ListView();

        listView.setItems(obsList);


        node.setFocusTraversable(true);

        node.requestFocus();

        pane.requestFocus();

        pane.grabFocus();


        StackPane root = new StackPane();

        root.getChildren().add(node);

        Scene scene = new Scene(root, 500, 500);

        stage.setTitle("Test");

        stage.setScene(scene);

        stage.show();

    }


    @FXML

    private void removeItem(ActionEvent event) {

        obsList.remove(0);

    }

}



查看完整回答
反对 回复 2021-09-15
?
弑天下

TA贡献1818条经验 获得超8个赞

由于BWC_semaJ的解决方案,它现在可以工作了。而不是使用:

SwingUtilities.invokeLater(() -> pane.requestFocusInWindow());

我应该使用:

Platform.runLater(() -> {swingNode.requestFocus();}); //Use this instead


查看完整回答
反对 回复 2021-09-15
  • 2 回答
  • 0 关注
  • 163 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信