0

is it possible, to disable Ctrl+V shortcut in my JavaFx application ? its like in the textfields, user will not be able to copy text from somewhere and paste it using Ctrl+V.

here's my controller class :

package application;

import com.jfoenix.controls.JFXTextField;
import javafx.fxml.FXML;

public class Controller {

    @FXML
    private JFXTextField t1 = new JFXTextField() {
        @Override
        public void paste() {
            //do something to stop paste
        }
    };
}
Enigo
  • 3,685
  • 5
  • 29
  • 54
Aman Gupta
  • 21
  • 4

1 Answers1

1

You can do so by overriding the paste method and remove the line

super.paste();

it will do nothing when user try to paste

import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.TextField;
import javafx.scene.layout.StackPane;
import javafx.stage.Stage;

public class Main extends Application {

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

    @Override
    public void start(Stage stage){

        TextField textField = new TextField(){
            @Override
            public void paste() {

            }
        };
        Scene scene = new Scene(new StackPane(textField));
        stage.setScene(scene);
        stage.show();
    }

}
Ahmed Emad
  • 674
  • 6
  • 19
  • nope its still not working, i @override the public void paste() and wrote nothing inside but its still taking paste from Ctrl+V...do i need to write something inside the paste method ? – Aman Gupta Jul 22 '18 at 06:55
  • can u please post your code as I tried it with the code I mentioned above and it's working fine. – Ahmed Emad Jul 22 '18 at 18:38
  • i have posted my controller class, please rectify it. – Aman Gupta Jul 23 '18 at 06:13
  • @AmanGupta, which version of `jfoenix` do you use?'cos the code in this answer works just fine for me. – Enigo Jul 23 '18 at 21:06
  • ok I think i figured what is going on ur t1 (TextField) is already a reference to the FXML text field so what u r doing is assigning a new value to that reference and change the paste function to that new reference but the old one remain the same – Ahmed Emad Jul 23 '18 at 21:10
  • @Enigo im using `jfoenix` version 9.0.0 and @AhmedEmad so what should i do in this case??...is there any other way using keyCombination or something to avoid default keyboard shortcuts ? – Aman Gupta Jul 24 '18 at 08:32
  • You can do it the other way around by creating a new textfield in the controller and add it to the pane in your scene if u want i can edit my answer with a sample code – Ahmed Emad Jul 24 '18 at 10:41
  • please give a sample code it will be really helpful and i guess using fxml i cant stop keyboard shortcuts right ? – Aman Gupta Jul 25 '18 at 01:20
  • I don't think so. but you can make your own Textfield that has an attribute to enable or disable the paste property and use it in FXML OR u can use my answer after the edit – Ahmed Emad Jul 25 '18 at 14:48
  • Accept the answer if it helps – Ahmed Emad Jul 25 '18 at 21:22