首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >无法在JavaFX SwingNode中将焦点设置在JTextPane上

无法在JavaFX SwingNode中将焦点设置在JTextPane上
EN

Stack Overflow用户
提问于 2018-09-12 09:22:03
回答 2查看 399关注 0票数 0

我正在尝试将焦点设置在JTextPane上,以便当窗口打开时,可以立即使用键盘进行编辑。然而,我所做的一切似乎都没有让JTextPane专注于启动。这仅仅是在Swing中使用JavaFX的问题吗?

代码语言:javascript
复制
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);
    }
}
EN

回答 2

Stack Overflow用户

发布于 2018-09-12 11:08:12

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

代码语言:javascript
复制
SwingUtilities.invokeLater(() -> pane.requestFocusInWindow());

我应该使用:

代码语言:javascript
复制
Platform.runLater(() -> {swingNode.requestFocus();}); //Use this instead
票数 0
EN

Stack Overflow用户

发布于 2018-09-13 07:05:40

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

代码语言:javascript
复制
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);
    }
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/52286172

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档