JavaFx - TextField.setText() not working

  • (2 Pages)
  • +
  • 1
  • 2

21 Replies - 12664 Views - Last Post: 15 November 2016 - 10:22 AM Rate Topic: -----

#1 ErtanTaboo   User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 47
  • Joined: 12-June 15

JavaFx - TextField.setText() not working

Posted 14 November 2016 - 09:46 AM

im trying to add some text to a textfield from another class, but when i click the button nothing happens , why is this happening?

MainWindowController.java

@FXML
public static TextField konsumatoriPunetField = new TextField();
 
@FXML
private void initialize()
{
    FXMLLoader loader5 = new FXMLLoader();
    loader5.setLocation(getClass().getResource("ZgjedhKonsumatorinFXML.fxml"));
    BorderPane border5 = new BorderPane();
    border5 = loader5.load();
    Scene scene5 = new Scene(border5);
    zgjedhkonsumatorinstage.setScene(scene5);
    zgjedhkonsumatorinstage.setTitle("Pit Stop");
    zgjedhkonsumatorinstage.initModality(Modality.WINDOW_MODAL);
    zgjedhkonsumatorinstage.initOwner(MainFXMLController.mainFXMLStage);
}
 
@FXML
public void zgjedhKonsumatorin()
{
    zgjedhkonsumatorinstage.showAndWait();
}


MainWindowFXML.fxml
<TextField fx:id="konsumatoriPunetField" editable="false" onMouseClicked="#zgjedhKonsumatorin" promptText="Kliko per te zgjedhur" GridPane.columnIndex="1" GridPane.rowIndex="1" />



ZgjedhKonsumatorinController.java
@FXML
public void zgjedhKonsumatorin()
{
    MainWindowController.zgjedhkonsumatorinstage.close();
    MainWindowController.konsumatoriPunetField.setText("ertani");
 
}



ZgjedhKonsumatorinFXML.fxml
<Button mnemonicParsing="false" onAction="#zgjedhKonsumatorin" prefWidth="150.0" text="Zgjedh Konsumatorin" />



Is This A Good Question/Topic? 0
  • +

Replies To: JavaFx - TextField.setText() not working

#2 NormR   User is offline

  • D.I.C Lover
  • member icon

Reputation: 798
  • View blog
  • Posts: 6,093
  • Joined: 25-December 13

Re: JavaFx - TextField.setText() not working

Posted 14 November 2016 - 11:25 AM

Quote

why is this happening?

Some common reasons:
The setText() method is not called.
The TextField variable does not refer to a component that is actively shown in a GUI
The TextField is not enabled for update

This post has been edited by NormR: 14 November 2016 - 11:26 AM

Was This Post Helpful? 0
  • +
  • -

#3 ErtanTaboo   User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 47
  • Joined: 12-June 15

Re: JavaFx - TextField.setText() not working

Posted 14 November 2016 - 12:05 PM

View PostNormR, on 14 November 2016 - 11:25 AM, said:

Quote

why is this happening?

Some common reasons:
The setText() method is not called.
The TextField variable does not refer to a component that is actively shown in a GUI
The TextField is not enabled for update


The setText() is called at ZgjedhKonsumatorinController.java
@FXML
public void zgjedhKonsumatorin()
{
    MainWindowController.zgjedhkonsumatorinstage.close();
    MainWindowController.konsumatoriPunetField.setText("ertani");=
 
}

This post has been edited by ErtanTaboo: 14 November 2016 - 12:06 PM

Was This Post Helpful? 0
  • +
  • -

#4 NormR   User is offline

  • D.I.C Lover
  • member icon

Reputation: 798
  • View blog
  • Posts: 6,093
  • Joined: 25-December 13

Re: JavaFx - TextField.setText() not working

Posted 14 November 2016 - 12:11 PM

Quote

The setText() is called

Just having code in a program does not mean that code is ever executed. To be sure the code is being executed, add a print statement next to it that prints a message showing that the code is executed.

That is about one of the reasons I listed, what about the other two reasons?

This post has been edited by NormR: 14 November 2016 - 12:12 PM

Was This Post Helpful? 0
  • +
  • -

#5 ErtanTaboo   User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 47
  • Joined: 12-June 15

Re: JavaFx - TextField.setText() not working

Posted 14 November 2016 - 12:22 PM

View PostNormR, on 14 November 2016 - 12:11 PM, said:

Just having code in a program does not mean that code is ever executed. To be sure the code is being executed, add a print statement next to it that prints a message showing that the code is executed.

That is about one of the reasons I listed, what about the other two reasons?


I added some prints and they all are being executed.

About the 2nd: The thing is that when user press Button in newWindow, it should automatically add that text to TextField in Window1(Parent Stage).

And the 3rd: I made TextField editable , but still the same.

p.s I added this line to that method after calling setText():
 System.out.println(main.konsumatoriPunetField.getText());


And it prints ertani
But nothing happens with TextField in GUI

This post has been edited by ErtanTaboo: 14 November 2016 - 12:24 PM

Was This Post Helpful? 0
  • +
  • -

#6 NormR   User is offline

  • D.I.C Lover
  • member icon

Reputation: 798
  • View blog
  • Posts: 6,093
  • Joined: 25-December 13

Re: JavaFx - TextField.setText() not working

Posted 14 November 2016 - 12:34 PM

Ok the first and third reasons have been tested.
Now what about the second one? Was there more than one instance of the TextField created?
One is shown and the other is not shown. If the one not being shown is updated, the update isn't shown.
Was This Post Helpful? 0
  • +
  • -

#7 ErtanTaboo   User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 47
  • Joined: 12-June 15

Re: JavaFx - TextField.setText() not working

Posted 14 November 2016 - 12:43 PM

View PostNormR, on 14 November 2016 - 12:34 PM, said:

Now what about the second one? Was there more than one instance of the TextField created?
One is shown and the other is not shown. If the one not being shown is updated, the update isn't shown.


There's plenty of TextFields in MainWindowController.java , but konsumatoriPunetField is shown in GUI, and its the only TextField with that name in entire project.
MainWindowController.java
@FXML
public static TextField konsumatoriPunetField = new TextField();


MainWindowFXML.fxml
	<TextField fx:id="konsumatoriPunetField" editable="false" onMouseClicked="#zgjedhKonsumatorin" promptText="Kliko per te zgjedhur" GridPane.columnIndex="1" GridPane.rowIndex="1" />

This post has been edited by ErtanTaboo: 14 November 2016 - 12:44 PM

Was This Post Helpful? 0
  • +
  • -

#8 NormR   User is offline

  • D.I.C Lover
  • member icon

Reputation: 798
  • View blog
  • Posts: 6,093
  • Joined: 25-December 13

Re: JavaFx - TextField.setText() not working

Posted 14 November 2016 - 12:48 PM

Quote

konsumatoriPunetField is shown in GUI

Are you sure the TextField referenced by that variable is the one being shown in the GUI?

I don't know JavaFX. With Swing there are methods for GUI components that will give the components location in the GUI, its size and the parent container for the component. Printing what those methods returned would allow you to see if a component was shown in the GUI.
For example:
      JTextField tf = new JTextField();
      tf.setText("Some text");  // possible to call methods >> results not seen
      System.out.println("bnds="+tf.getBounds()); // bnds=java.awt.Rectangle[x=0,y=0,width=0,height=0]


Note the width and height are 0

This post has been edited by NormR: 14 November 2016 - 12:51 PM

Was This Post Helpful? 0
  • +
  • -

#9 ErtanTaboo   User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 47
  • Joined: 12-June 15

Re: JavaFx - TextField.setText() not working

Posted 14 November 2016 - 01:04 PM

View PostNormR, on 14 November 2016 - 12:48 PM, said:

Quote

konsumatoriPunetField is shown in GUI

Are you sure the TextField referenced by that variable is the one being shown in the GUI?

I don't know JavaFX. With Swing there are methods for GUI components that will give the components location in the GUI, its size and the parent container for the component. Printing what those methods returned would allow you to see if a component was shown in the GUI.
For example:
      JTextField tf = new JTextField();
      tf.setText("Some text");  // possible to call methods >> results not seen
      System.out.println("bnds="+tf.getBounds()); // bnds=java.awt.Rectangle[x=0,y=0,width=0,height=0]


Note the width and height are 0


I tried .getBoundsInLocal and .getBoundsInParent and all fields is showing me 0,0,0,0,0

I also tried with another Textfield instead of this one , and still the same. None of the TextFields is updating.
Was This Post Helpful? 0
  • +
  • -

#10 NormR   User is offline

  • D.I.C Lover
  • member icon

Reputation: 798
  • View blog
  • Posts: 6,093
  • Joined: 25-December 13

Re: JavaFx - TextField.setText() not working

Posted 14 November 2016 - 01:12 PM

Quote

all fields is showing me 0,0,0,0,0

I guess that means that field is NOT being shown in the GUI if its width and height is 0.
Was This Post Helpful? 0
  • +
  • -

#11 ErtanTaboo   User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 47
  • Joined: 12-June 15

Re: JavaFx - TextField.setText() not working

Posted 14 November 2016 - 03:55 PM

View PostNormR, on 14 November 2016 - 01:12 PM, said:

Quote

all fields is showing me 0,0,0,0,0

I guess that means that field is NOT being shown in the GUI if its width and height is 0.

But why this is happening with all TextFields , not only with this one.
Was This Post Helpful? 0
  • +
  • -

#12 NormR   User is offline

  • D.I.C Lover
  • member icon

Reputation: 798
  • View blog
  • Posts: 6,093
  • Joined: 25-December 13

Re: JavaFx - TextField.setText() not working

Posted 14 November 2016 - 04:00 PM

Quote

why this is happening with all TextFields

Are you saying that the setText() method is not working for any of the TextFields?
Was This Post Helpful? 0
  • +
  • -

#13 ErtanTaboo   User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 47
  • Joined: 12-June 15

Re: JavaFx - TextField.setText() not working

Posted 14 November 2016 - 04:05 PM

View PostNormR, on 14 November 2016 - 04:00 PM, said:

Are you saying that the setText() method is not working for any of the TextFields?


Yep i tried with some other TextFields and still the same.
Was This Post Helpful? 0
  • +
  • -

#14 NormR   User is offline

  • D.I.C Lover
  • member icon

Reputation: 798
  • View blog
  • Posts: 6,093
  • Joined: 25-December 13

Re: JavaFx - TextField.setText() not working

Posted 14 November 2016 - 04:33 PM

I suspect there are two instances of the TextField being created. One is shown and one is not shown.
I think the loader creates one from the fxml file and the JVM creates one from the new statement.
To test my theory, remove the new statement:
@FXML
public static TextField konsumatoriPunetField; // = new TextField();


Was This Post Helpful? 0
  • +
  • -

#15 ErtanTaboo   User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 47
  • Joined: 12-June 15

Re: JavaFx - TextField.setText() not working

Posted 15 November 2016 - 05:30 AM

View PostNormR, on 14 November 2016 - 04:33 PM, said:

To test my theory, remove the new statement:
@FXML
public static TextField konsumatoriPunetField; // = new TextField();



I changed my code to:
@FXML
    public TextField konsumatoriPunetField;


also i changed my method code to:

FXMLLoader loader = new FXMLLoader(getClass().getResource("MainWindowFXML.fxml"));
        
        MainWindowController c = (MainWindowController) loader.getController();
        c.konsumatoriPunetField.textProperty().setValue("Ertani");
        main.zgjedhkonsumatorinstage.close();


And now it shows NullPointerException in this line:
c.konsumatoriPunetField.textProperty().setValue("Ertani");

Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2