-1

Nowadays I am working on raspberry pi and I write some programs in java , javafx platforms.I just would like to inform you that I am simply beginner on javafx.

According to that I just would like to trigger ENTER key after changing my textfield.Working principle of my program is like this;

1)I have created one masterform fxml and it is directing all other pages with one textfield.

2)I created main method that let me to use keyboard to enter some specific String values to assign them to textfield for page alteration.

3)I have a bridge java page, it includes global variables to use everywhere in project.So Firstly I set value from keyboard to these global variables.These global variables are created as stringproperty for adding actionlistener for any change.

4)Then I set these global variables to textfield.

5)Textfield indicates relevant values from keyboard.But Unfortunately I can not forward the pages without pressing to enter key.In this case ı would like to trigger this textfield.But unfortunately ı have no idea how to trigger texfield without pressing enter key.Therefore I decided to make auto trigger to enter key for this textfield.

I simply used robot method;

Robot robot = new Robot();
robot.keyPress(KeyEvent.VK_ENTER);

But it didn't work.Because After I set the global variable to textfield for first time.It does not define the value of the textfield is changed.It determines after pressing the enter key.

So how can I trigger this textfield after getting value of my global variables.I would like to pass how to set pages, I will show you how my program works.

Example of my code is;

Main method

 public static void main(String[] args) {
Scanner scanner=new Scanner(System.in);

      for (String strBarcode = scanner.nextLine(); !strBarcode.isEmpty(); 
              strBarcode = scanner.nextLine()) {

          if (strBarcode.equals("distribution")){

          Global.G_MOD.set("distribution");
          System.out.println(Global.G_MOD.get());

      }
}}

GlobalVariables.java(bridge page)

public class Global{
public static StringProperty G_MOD = new SimpleStringProperty("");
}

My MasterController Page for javafx

public class masterformController implements Initializable {


@FXML
    public TextField tbxBarcode;

@FXML
    void onchangetbxBarcode(ActionEvent event)  {

if(Global.G_MOD.get().equals("distribution")){

        try {
FXMLLoader loader = new FXMLLoader(getClass().getResource("/puttolightfx/fxml/page1.fxml"));
            Parent rootpage1 = (Parent)loader.load();

            pnPages.getChildren().clear();
            pnPages.getChildren().add(rootpage1);       


         } catch (IOException ex) {
            Logger.getLogger(masterformController.class.getName()).log(Level.SEVERE, null, ex);
        }
}

@Override
    public void initialize(URL url, ResourceBundle rb) {

Global.G_MOD.addListener(new ChangeListener(){
        @Override
            public void changed(ObservableValue observable, Object oldValue, Object newValue) {
                String Newvalue = (String)newValue; 
                tbxBarcode.setText(Global.G_MOD.get());}
        }); 

}


}

So Everything is working, just I have to trigger textfield when the global value : Global.G_MOD is indicated on texfield.Then it will pass to another page according to global value of Global.G_MOD : "distribution".

SOLUTION(SOLVED):

I solved my problem using thread on listener of the textfield.I gave up to trigger enter key automatically and focused on textfield change.

I simply decided to use thread to change .fxml pages in textfield listener.

Platform.runLater(new Runnable() {
    @Override
    public void run() {
        //if you change the UI, do it here !
    }
});

EDITED CODE :

tbxBarcode.textProperty().addListener((ObservableValue<? extends String> observable, String oldValue, String newValue) -> {
            String Newvalue=(String)newValue;
            System.out.println(tbxBarcode.getText());
            Platform.runLater(new Runnable() {
    @Override
    public void run() {
                           if(Global.G_MOD.get().equals("distribution")){

        try {
FXMLLoader loader = new FXMLLoader(getClass().getResource("/puttolightfx/fxml/page1.fxml"));
            Parent rootpage1 = (Parent)loader.load();

            pnPages.getChildren().clear();
            pnPages.getChildren().add(rootpage1);       


         } catch (IOException ex) {
            Logger.getLogger(masterformController.class.getName()).log(Level.SEVERE, null, ex);
        }
}
//                }


    }
});
        });
Xspacex
  • 1
  • 1
  • 10

1 Answers1

0

Try using

textField.fireEvent(new KeyEvent(KeyEvent.KEY_PRESSED, "", "", KeyCode.ENTER, true, true, true, true));

According to the docs

public KeyEvent(EventType<KeyEvent> eventType,
                String character,
                String text,
                KeyCode code,
                boolean shiftDown,
                boolean controlDown,
                boolean altDown,
                boolean metaDown)
Constructs new KeyEvent event with null source and target and KeyCode object directly specified.
Parameters:
eventType - The type of the event.
character - The character or sequence of characters associated with the event
text - A String describing the key code
code - The integer key code
shiftDown - true if shift modifier was pressed.
controlDown - true if control modifier was pressed.
altDown - true if alt modifier was pressed.
metaDown - true if meta modifier was pressed.
Since:
JavaFX 8.0

You can refer https://docs.oracle.com/javase/8/javafx/api/javafx/scene/input/KeyEvent.html

Edit 1 You need to identify the moment when Enter key event must be triggered. For example: If your textfield allows a limited number of characters, then you can add the above mentioned code in the following way:

txtField.textProperty().addListener(new ChangeListener<String>() {

            @Override
            public void changed(ObservableValue<? extends String> observable, String oldValue, String newValue) {

                if (newValue.length()>30) {
                    txtField.setText(oldValue);
                    txtField.fireEvent(new KeyEvent(KeyEvent.KEY_PRESSED, "", "", KeyCode.ENTER, true, true, true, true));
                }
            }
        });

This is just an example. It can fire your event multiple times, so you need to write the code to fire the event just once.

Harshita Sethi
  • 2,035
  • 3
  • 24
  • 46
  • What did you mean with "robot.fireEvent" .What should I define instead of this ? there is no method that named as "fireEvent" what should ı use instead of this. – Xspacex May 16 '16 at 11:07
  • `fireEvent()` is an inbuilt method for all the javafx controls. Sorry for mentioning robot. I have edited my answer. Apply this method on your `TextField` or any other control on which you want to automatically trigger the event. – Harshita Sethi May 16 '16 at 11:13
  • I think it is going to work well.I just tried but another error occured in this line : **tbxBarcode.fireEvent(new KeyEtbxBarcodevent(KeyEvent.KEY_PRESSED, "" , "", KeyCode.ENTER, true, true, true, true));** and error : **incompatible types ; int can not be converted to component** But I dont know which value is "int" – Xspacex May 16 '16 at 12:28
  • Just have a look at the method while calling it you will come to know. Also are you sure `KeyEtbxBarcodevent` method has to be used instead of `KeyEvent` method? I have given the solution according to `KeyEvent` method. So the parameters may different for the method you are calling? – Harshita Sethi May 16 '16 at 13:44
  • I accidentally made it, correct one is : **tbxBarcode.fireEvent(new KeyEvent(KeyEvent.KEY_PRESSED, "" , "", KeyCode.ENTER, true, true, true, true));** but the error is same.İnt can not be converted to component. – Xspacex May 16 '16 at 13:50
  • Now there is no error with this type of using : **tbxBarcode.fireEvent(new javafx.scene.input.KeyEvent(javafx.scene.input.KeyEvent.KEY_PRESSED, "" , "", KeyCode.ENTER, true, true, true, true));** I integrated it to mastercontroler initialize method.But unfortunately still same.The value of Global.G_MOD : distribution to textfield.But this textfield(tbxBarcode) does not trigger enter key. – Xspacex May 16 '16 at 14:38
  • On you textfield `tbxBarcode` you have a code for handling `ActionEvent` not `KeyEvent`.. You need to tell what to do when`Enter` is pressed. Like `tbxBarcode.setOnKeyPressed` . Have a look at this for help : http://stackoverflow.com/questions/27982895/how-to-use-keyevent-in-javafx-project – Harshita Sethi May 17 '16 at 05:56
  • Oh yes you are right.All my conditions are available in action event of tbxbarcode textfield.So it waits for action to pass pages.Should ı change tthis actionevent to key event to trigger this textfield automatically without of any action.For example: if text of the tbxbarcode textfield change , and if this string is "distribution" pass page 1.fxml.How can I integrate it? Really thank you for help. – Xspacex May 17 '16 at 06:08
  • Dont change the current event method as you made it in your `fxml`. Write fresh code to handle `KeyPress event`. You can refer the link I mentioned in my previous comment on `How to write keyPressed event handler`. Also make sure that the event once called should not be called again as it will keep loading the fxml. – Harshita Sethi May 17 '16 at 07:26
  • Thank you HARSHITA SETHI, I solved this problem and edited my question.You helped me a lot – Xspacex May 17 '16 at 15:15
  • your welcome :).. Based on your solution, I am assuming you didn't require an `Enter KeyEvent`.. All you needed was some logic to monitor the entry in your `textField.` – Harshita Sethi May 18 '16 at 05:18