2

I have a JavaFX application that has various TextField widgets in the main frame. I have a MenuBar that includes the MenuItem objects "Copy" and "Paste" like a standard production application would have. Since any or none of the various TextField objects could be selected at any given time, it seems easier to just hardcode a "Ctrl+C" or "Ctrl+V" key press in the setOnAction events of the "Copy" and "Paste" MenuItem objects rather than use a Clipboard object and loop iterating through all TextFields to find the highlighted text (if any).

Is there a way to hardcode this key press action in Java? I looked into the KeyCombination class but it does not actually trigger the action described by the given key combination.

9
  • 1
    Hardcode a Ctrl+C Key press??? Iterate through the fields? Could you explain what you mean by that??? (Firing a key event yourself on a descendant node of the TextField or something like this???)
    – fabian
    May 23, 2017 at 20:38
  • I think I explained the problem pretty well but here is the pseudocode: Clipboard.execute("Ctrl+C"). And yes, the alternative solution is to put all TextField objects in an array, iterate through the array looking for the first TextField with a getSelected() property that is true.
    – jshapy8
    May 23, 2017 at 20:40
  • Text field doesn't have a getSelected() method. Are you referring to having the focus?
    – James_D
    May 23, 2017 at 20:41
  • I believe getSelectedText() is the TextField method that will return highlighted text
    – jshapy8
    May 23, 2017 at 20:44
  • I was just wondering if I can just trigger Ctrl+C as a command
    – jshapy8
    May 23, 2017 at 20:44

1 Answer 1

4

I think by "Since any or none of the various TextField objects could be selected at any given time" you are referring to which (if any) text field has the keyboard focus.

You can easily get this information from the scene: just do

Node focusOwner = scene.getFocusOwner();
if (focusOwner instanceof TextField) {
    TextField textField = (TextField) focusOwner ;
    String selectedText = textField.getSelectedText();
    // ...
}

Note also that TextInputControl defines a copy() method that copies the selected text to the system clipboard. (Similarly, there's a paste() method too.) So you can leverage those to make the functionality easy.

Here's a SSCCE:

import javafx.application.Application;
import javafx.scene.Node;
import javafx.scene.Scene;
import javafx.scene.control.Menu;
import javafx.scene.control.MenuBar;
import javafx.scene.control.MenuItem;
import javafx.scene.control.TextField;
import javafx.scene.control.TextInputControl;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;

public class FocusMenuTest extends Application {

    @Override
    public void start(Stage primaryStage) {
        BorderPane root = new BorderPane();
        VBox textFields = new VBox(5, new TextField("One"), new TextField("Two"), new TextField("Three"));
        MenuBar menuBar = new MenuBar();
        Menu edit = new Menu("Edit");
        MenuItem copy = new MenuItem("Copy");

        copy.setOnAction(e -> {
            Node focusOwner = menuBar.getScene().getFocusOwner();
            if (focusOwner instanceof TextInputControl) {
                ((TextInputControl)focusOwner).copy();
            }
        });     

        MenuItem paste = new MenuItem("Paste");
        paste.setOnAction(e -> {
            Node focusOwner = menuBar.getScene().getFocusOwner();
            if (focusOwner instanceof TextInputControl) {
                ((TextInputControl)focusOwner).paste();
            }
        });

        menuBar.getMenus().add(edit);
        edit.getItems().addAll(copy, paste);
        root.setCenter(textFields);
        root.setTop(menuBar);
        Scene scene = new Scene(root, 400, 400);


        primaryStage.setScene(scene);
        primaryStage.show();
    }

    public static void main(String[] args) {
        launch(args);
    }
}
0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.