Не могу установить фокус на JTextPane внутри JavaFX SwingNode - PullRequest
0 голосов
/ 12 сентября 2018

Я пытаюсь установить фокус на 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 ]

0 голосов
/ 13 сентября 2018

Я не знаю, помогает ли это, но ниже приведена демонстрационная программа, которую я сделал на основе примера кода, и по какой-то причине она работает для меня:

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 голосов
/ 12 сентября 2018

Теперь он работает благодаря решению от BWC_semaJ .Вместо использования:

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

Я должен был использовать:

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