2

I have a controller Class extends Thread,

and my Run() method, just do this.

private int con = 1;

public void Run(){
   while(true){
      try{
          con++;
          print();
      catch(exception e){
          system.out.prinln(e);
      }
   }
}

and here's the problem,i need a print() method, to show "con" on a TextField.

I have a frmMain.fxml and the frmMainController.java class.

How i can show my "con", when clicked a button from my interface. (evt onClickedMouse).

i have this:

  private TextField tf;
    private FrmMainController fr;

    public void show(){
       tf.setText(String.valueOf(con));
       fr.setTxtThread1(tf); 
    }

I would appreciate someone help me to get over this.

R4k4210
  • 31
  • 9
  • So you have to create `button` component first. same issue here http://stackoverflow.com/questions/26524583/the-count-number-of-pressed-button – TomN Jul 28 '16 at 03:54
  • Show .fxml file too..TextField object has to be initialized before used or else is null. – GOXR3PLUS Jul 28 '16 at 11:22

1 Answers1

1

Modifying JavaFX Components in other Thread can cause problems.

Although in your run method you can do it using Platform.runLater(...);

In your fxml file add an identifier(fx:id) for your TextField then your Controlled class has to implement Initializable interface which has a method that will be called when all your fxml elements have been initialized.

As soon as this happen you can call your Run method and update the TextField as described above.

Basically in the SceneBuilder create a basic layout with a TextField and a Button which have fixed id.Then add an actionListener to the button inside the method of Initializable interface:

button.setOnAction(a->{···});
GOXR3PLUS
  • 6,877
  • 9
  • 44
  • 93