Results 1 to 18 of 18
  1. #1
    ErtanTaboo is offline Member
    Join Date
    Jun 2015
    Posts
    14
    Rep Power
    0

    Default JavaFx - TextField.SetText() Not Working

    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
    Java FX Code:
    @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
    Java FX Code:
    <TextField fx:id="konsumatoriPunetField" editable="false" onMouseClicked="#zgjedhKonsumatorin" promptText="Kliko per te zgjedhur" GridPane.columnIndex="1" GridPane.rowIndex="1" />
    ZgjedhKonsumatorinController.java
    Java FX Code:
    @FXML
    public void zgjedhKonsumatorin()
    {
        MainWindowController.zgjedhkonsumatorinstage.close();
        MainWindowController.konsumatoriPunetField.setText("ertani");
    
    }
    ZgjedhKonsumatorinFXML.fxml
    Java FX Code:
    <Button mnemonicParsing="false" onAction="#zgjedhKonsumatorin" prefWidth="150.0" text="Zgjedh Konsumatorin" />

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    19,966
    Rep Power
    31

    Default Re: JavaFx - TextField.SetText() Not Working

    If you don't understand my response, don't ignore it, ask a question.

  3. #3
    ErtanTaboo is offline Member
    Join Date
    Jun 2015
    Posts
    14
    Rep Power
    0

    Default Re: JavaFx - TextField.SetText() Not Working

    Quote Originally Posted by Norm View Post
    Yep , but i thought that somebody here can help me too , because i have no idea how ti fix it.

  4. #4
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    19,966
    Rep Power
    31

    Default Re: JavaFx - TextField.SetText() Not Working

    What happened when you removed the new statement?
    If you don't understand my response, don't ignore it, ask a question.

  5. #5
    ErtanTaboo is offline Member
    Join Date
    Jun 2015
    Posts
    14
    Rep Power
    0

    Default Re: JavaFx - TextField.SetText() Not Working

    Quote Originally Posted by Norm View Post
    What happened when you removed the new statement?
    Which statement?

  6. #6
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    19,966
    Rep Power
    31

    Default Re: JavaFx - TextField.SetText() Not Working

    If you don't understand my response, don't ignore it, ask a question.

  7. #7
    benji2505 is offline Senior Member
    Join Date
    Sep 2014
    Location
    MA, USA
    Posts
    398
    Rep Power
    5

    Default Re: JavaFx - TextField.SetText() Not Working

    My understanding is that you want to launch 2 windows and both windows are in fxml files and both windows have (obviously) an associated controller class.

    Since the code is not shown in its entire beauty there may be a couple of issues, but before we go into the nitty-gritty, some comments:

    - I am sorry if I explain too much detail below, from the code snippets it is difficult to judge where the program stands.
    - have you made sure that the controller class show up in the fxml file(s) with the parameter fx:controller ?
    - In line 8 you tell the program to get all the graphics from an fxml file, but in line 12 you set the scene to an empty BorderPane created in line 9. Please keep in mind that all these graphical setups are in the fxml file now.
    - after you have created your fxml file in the IDE, I would suggest that you right click the fxml file and open it with SceneBuilder. This makes it much easier to get all the prefix (taglibs??) right. I am just mentioning it because the fxml files are not shown here entirely, I am sure you had it right.

    To get started, I would suggest to open one window for the time being and get all the coding right, the second window will be a breeze then. I will post a running example below. The only difference to the plain vanilla fxml setup will be that I get an instance of the controller, which you will need if you want to pass values from one window to the other (or from any other part of the program).
    There will be 3 classes:
    -the Main Class will setup the window basics, set the fxml file as graphic source and get the controller
    - the controller class controls all the graphics. Here you would find methods that add a Textfield or change text in a textfield. If you want to add an initialize method in that controller class, the class has to implement the Initializable Interface (the example does not do that). The initialize method is called after all fxml graphics has been processed
    - the fxml file. I hope the java-forums window is wide enough to show the fx:controller parameter

  8. #8
    benji2505 is offline Senior Member
    Join Date
    Sep 2014
    Location
    MA, USA
    Posts
    398
    Rep Power
    5

    Default Re: JavaFx - TextField.SetText() Not Working

    Main Class:

    Java FX Code:
    public class fxmlMain extends Application {
    	
    	Parent root;
    	FXMLLoader loader;
    	public  static fxmlController fxController;
    	
    	@Override
    	public void start(Stage primaryStage) throws IOException {
    		
    		loader = new FXMLLoader(getClass().getResource("fxmlGraphic.fxml"));
    		try {root = loader.load();} catch (IOException e) {e.printStackTrace();}
    		fxController=loader.getController();
    		
    		
    		Scene scene = new Scene(root, 800, 500, Color.AQUA);
    		primaryStage.setTitle("FXML Test Area 51");
    		primaryStage.setScene(scene);
    		primaryStage.show();
    	}
    	
    
    
    	public static void main(String[] args) {
    		launch(args);
    	}
    }
    fxmlController

    Java FX Code:
     public class fxmlController {
    
    	public GridPane rootPane;// = new GridPane();
    	public Button fxButton1;// = new Button();
    	public RadioButton radioButton1;// = new RadioButton();
    	public GridPane gridCalc;// = new GridPane();
    	
    	@FXML
    	public void buttonPressed(){
    		System.out.println("you have pressed the button, Congrats!");
    		if (fxButton1.getText()!="pressed"){
    			fxButton1.setText("pressed");
    		}
    		else 
    			fxButton1.setText("try again");
    		
    				
    		String[] keys = new String[]{"1","2","3","4","5","6","7","8","9","*","0","#" };
    		for (int i=0;i<12;i++){
    			Button button = new Button(keys[i]);
    			gridCalc.add(button,i%3,(int)Math.ceil(i/3));}
    		
    	}
    	
    	@FXML
    	public void radioButtonPressed(){
    		if (radioButton1.isSelected()==true){
    		System.out.println("you have turned on the radio!");
    			}
    		else System.out.println("you have turned off the radio");
    	}
    	
    }

    fxmlGraphic.fxml
    Java FX Code:
     <?xml version="1.0" encoding="UTF-8"?>
    
    <?import javafx.geometry.*?>
    <?import javafx.scene.control.*?>
    <?import java.lang.*?>
    <?import javafx.scene.layout.*?>
    <?import javafx.scene.layout.GridPane?>
    
    <GridPane fx:id="rootPane" alignment="CENTER" maxHeight="-Infinity"
    	maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity"
    	prefHeight="400.0" prefWidth="600.0" xmlns="http://javafx.com/javafx/8"
    	xmlns:fx="http://javafx.com/fxml/1" fx:controller="fxmlTest.fxmlController">
    	<columnConstraints>
    		<ColumnConstraints hgrow="SOMETIMES" minWidth="10.0"
    			prefWidth="100.0" />
    		<ColumnConstraints hgrow="SOMETIMES" minWidth="10.0"
    			prefWidth="100.0" />
    	</columnConstraints>
    	<rowConstraints>
    		<RowConstraints minHeight="10.0" prefHeight="30.0"
    			vgrow="SOMETIMES" />
    		<RowConstraints minHeight="10.0" prefHeight="30.0"
    			vgrow="SOMETIMES" />
    		<RowConstraints minHeight="10.0" prefHeight="30.0"
    			vgrow="SOMETIMES" />
    	</rowConstraints>
    	<children>
    		<Button fx:id="fxButton1" alignment="CENTER_RIGHT"
    			contentDisplay="CENTER" mnemonicParsing="false" onAction="#buttonPressed"
    			text="Button" GridPane.halignment="CENTER" GridPane.valignment="CENTER">
    			<GridPane.margin>
    				<Insets bottom="5.0" left="5.0" right="5.0" top="5.0" />
    			</GridPane.margin>
    			<padding>
    				<Insets bottom="5.0" left="5.0" right="5.0" top="5.0" />
    			</padding>
    		</Button>
    		<RadioButton fx:id="radioButton1" mnemonicParsing="false"
    			onAction="#radioButtonPressed" text="RadioButton"
    			GridPane.halignment="CENTER" GridPane.rowIndex="1"
    			GridPane.valignment="CENTER">
    			<GridPane.margin>
    				<Insets bottom="5.0" left="5.0" right="5.0" top="5.0" />
    			</GridPane.margin>
    		</RadioButton>
    		<GridPane fx:id="gridCalc" GridPane.columnIndex="1">
    			<columnConstraints>
    				<ColumnConstraints hgrow="SOMETIMES" minWidth="10.0"
    					prefWidth="100.0" />
    				<ColumnConstraints hgrow="SOMETIMES" minWidth="10.0"
    					prefWidth="100.0" />
    				<ColumnConstraints hgrow="SOMETIMES" minWidth="10.0"
    					prefWidth="100.0" />
    			</columnConstraints>
    			<rowConstraints>
    				<RowConstraints minHeight="10.0" prefHeight="30.0"
    					vgrow="SOMETIMES" />
    				<RowConstraints minHeight="10.0" prefHeight="30.0"
    					vgrow="SOMETIMES" />
    				<RowConstraints minHeight="10.0" prefHeight="30.0"
    					vgrow="SOMETIMES" />
    			</rowConstraints>
    		</GridPane>
    	</children>
    </GridPane>
    Last edited by benji2505; 11-15-2016 at 03:34 AM.

  9. #9
    ErtanTaboo is offline Member
    Join Date
    Jun 2015
    Posts
    14
    Rep Power
    0

    Default Re: JavaFx - TextField.SetText() Not Working

    Quote Originally Posted by Norm View Post
    I replied you there.

  10. #10
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    19,966
    Rep Power
    31

    Default Re: JavaFx - TextField.SetText() Not Working

    Changing more than one thing at a time can introduce new errors.
    If you don't understand my response, don't ignore it, ask a question.

  11. #11
    ErtanTaboo is offline Member
    Join Date
    Jun 2015
    Posts
    14
    Rep Power
    0

    Default Re: JavaFx - TextField.SetText() Not Working

    Quote Originally Posted by benji2505 View Post
    Since the code is not shown in its entire beauty there may be a couple of issues, but before we go into the nitty-gritty, some comments:
    Here is entire code:
    MainWindowController.java
    Java FX Code:
    public class MainWindowController {
        static Stage zgjedhkonsumatorinstage = new Stage();
        @FXML
        public TextField konsumatoriPunetField;
        
         @FXML
        private void initialize() throws IOException, ClassNotFoundException, SQLException
        {
            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
    Java FX Code:
    <BorderPane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="700.0" prefWidth="1200.0" xmlns="http://javafx.com/javafx/8.0.65" xmlns:fx="http://javafx.com/fxml/1" fx:controller="main.MainWindowController">
       <center>
          <SplitPane dividerPositions="0.2370617696160267" prefHeight="160.0" prefWidth="200.0" BorderPane.alignment="CENTER">
            <items>
              <AnchorPane minHeight="0.0" minWidth="0.0" prefHeight="160.0" prefWidth="100.0">
                   <children>
                      <VBox layoutX="42.0" layoutY="55.0" prefHeight="673.0" prefWidth="261.0" spacing="1.0" AnchorPane.bottomAnchor="0.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="0.0">
                         <padding>
                            <Insets bottom="10.0" left="10.0" right="10.0" top="10.0" />
                         </padding>
    					 <GridPane>
                         <children>
                                  <TextField fx:id="konsumatoriPunetField" editable="false" onMouseClicked="#zgjedhKonsumatorin" promptText="Kliko per te zgjedhur" GridPane.columnIndex="1" GridPane.rowIndex="1" />
                               </children>
                            </GridPane>
                         </VBox>
                   </children></AnchorPane>
            </items>
          </SplitPane>
       </center>
    </BorderPane>
    ZgjedhKonsumatorinController.java
    Java FX Code:
    public class ZgjedhKonsumatorinController {
        MainWindowController main = new MainWindowController();
        
    	public void initialize() throws ClassNotFoundException, SQLException
        {
            
        }
        @FXML
        public void zgjedhKonsumatorin()
        {
            FXMLLoader loader = new FXMLLoader(getClass().getResource("MainWindowFXML.fxml"));
            
            MainWindowController c = (MainWindowController) loader.getController();
            c.konsumatoriPunetField.textProperty().setValue("Ertani");
            main.zgjedhkonsumatorinstage.close();
        }
    }
    ZgjedhKonsumatorinFXML.fxml
    Java FX Code:
    <BorderPane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="500.0" prefWidth="800.0" xmlns="http://javafx.com/javafx/8.0.65" xmlns:fx="http://javafx.com/fxml/1" fx:controller="main.ZgjedhKonsumatorinController">
       <center>
          <VBox BorderPane.alignment="CENTER">
             <children>
                <HBox alignment="CENTER" prefHeight="52.0" prefWidth="800.0" spacing="30.0">
                   <children>
                      <Button mnemonicParsing="false" onAction="#zgjedhKonsumatorin" prefWidth="150.0" text="Zgjedh Konsumatorin" />
                   </children>
                </HBox>
             </children>
          </VBox>
       </center>
    </BorderPane>

  12. #12
    benji2505 is offline Senior Member
    Join Date
    Sep 2014
    Location
    MA, USA
    Posts
    398
    Rep Power
    5

    Default Re: JavaFx - TextField.SetText() Not Working

    What is your package name?

  13. #13
    benji2505 is offline Senior Member
    Join Date
    Sep 2014
    Location
    MA, USA
    Posts
    398
    Rep Power
    5

    Default Re: JavaFx - TextField.SetText() Not Working

    Can you post the fxml file(s) including the prefix and the imports?

  14. #14
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    19,966
    Rep Power
    31

    Default Re: JavaFx - TextField.SetText() Not Working

    Can you add the import statements so the code can compile without errors?
    If you don't understand my response, don't ignore it, ask a question.

  15. #15
    ErtanTaboo is offline Member
    Join Date
    Jun 2015
    Posts
    14
    Rep Power
    0

    Default Re: JavaFx - TextField.SetText() Not Working

    Problem Solved!

    All i needed to do was:
    ZgjedhKonsumatorinController.java
    Java FX Code:
    public class ZgjedhKonsumatorinController {
    
        @FXML
        public void zgjedhKonsumatorin()
        {
            MainWindowController.zgjedhkonsumatorinstage.close();
        }
    
        public String getText() {
            return "ertani" ;
        }
    }
    MainWindowController.java
    Java FX Code:
    public class MainWindowController {
    
        @FXML
        private TextField konsumatoriPunetField ;
    
        private ZgjedhKonsumatorinController zgjedhKonsumatorinController ;
    
        @FXML
        private void initialize()
        {
            FXMLLoader loader5 = new FXMLLoader();
            loader5.setLocation(getClass().getResource("ZgjedhKonsumatorinFXML.fxml"));
            BorderPane border5 = new BorderPane();
            border5 = loader5.load();
            zgjedhKonsumatorinController = loader.getController();
            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();
            konsumatoriPunetField.setText(zgjedhKonsumatorinController.getText());
        }
    
    }

  16. #16
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    19,966
    Rep Power
    31

    Default Re: JavaFx - TextField.SetText() Not Working

    All i needed to do was:
    Can you explain what you did to solve your problem?
    Last edited by Norm; 11-15-2016 at 05:43 PM.
    If you don't understand my response, don't ignore it, ask a question.

  17. #17
    ErtanTaboo is offline Member
    Join Date
    Jun 2015
    Posts
    14
    Rep Power
    0

    Default Re: JavaFx - TextField.SetText() Not Working

    In JavaFX8 doesnt work anymore to inject a TextField to a static field.
    To allow access to instance data, just need to have a reference to the instance. and FXMLLoader has a method .getController() that allows to retrieve a reference to the controller.

    So i loaded controller in line 15. And since im using .showAndWait() for the new window , so in order to setText to MainWindowController i added the SetText() after showAndWait(). Because showAndWait() blocks execution until the window is closed, so the text field won't change until the window is closed.

    Also added a method getText() to ZgjedhKonsumatorinCotroller that returns the Text that is needed to add to TextField.

  18. #18
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    19,966
    Rep Power
    31

    Default Re: JavaFx - TextField.SetText() Not Working

    Ok, thanks for the update.
    If you don't understand my response, don't ignore it, ask a question.

Similar Threads

  1. Replies: 0
    Last Post: 11-30-2015, 01:42 AM
  2. Replies: 0
    Last Post: 12-07-2014, 06:27 PM
  3. Replies: 2
    Last Post: 10-14-2014, 04:20 PM
  4. Problem TextField setText
    By RaymondF in forum AWT / Swing
    Replies: 2
    Last Post: 06-11-2014, 05:42 PM
  5. ButtonListener setText not working (no errors)
    By cc11rocks in forum AWT / Swing
    Replies: 19
    Last Post: 02-07-2011, 04:41 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •