Results 1 to 9 of 9
  1. #1
    Yakg is offline Member
    Join Date
    Dec 2010
    Posts
    88
    Rep Power
    0

    Default Getting text from TextField

    Hey!

    For some reason I can't seem to get any text and can't see what i'm doing wrong..

    Working with Gluon and Netbeans and been keeping it as simple as can be..

    This line is problematic from some reason:
    label.setText(fnField.getText());
    The GUI looks fine and the ID there is set to match the code..

    Java FX Code:
    package fi;
    
    import java.net.URL;
    import java.util.ResourceBundle;
    import javafx.event.ActionEvent;
    import javafx.fxml.FXML;
    import javafx.fxml.Initializable;
    import javafx.scene.control.Label;
    import javafx.scene.control.TextField;
    
    /**
     *
     * @author Yakir
     */
    public class FXMLDocumentController implements Initializable {
        
        @FXML
        private Label label;
        
        @FXML
        private TextField fnField;
        
        @FXML
        private void handleButtonAction(ActionEvent event) { 
            label.setText(fnField.getText());
        }
        
        @Override
        public void initialize(URL url, ResourceBundle rb) {
            // TODO
        }    
        
    }

  2. #2
    asdfg is offline Senior Member
    Join Date
    Nov 2016
    Posts
    163
    Rep Power
    2

    Default Re: Getting text from TextField

    Not very familiar with swing, but try to to use get() and attach toString().
    Also you may get text from field.. if you use something like 'on button click'.
    Because itself java program dont have endless loop who keep refreshing.

  3. #3
    Yakg is offline Member
    Join Date
    Dec 2010
    Posts
    88
    Rep Power
    0

    Default Re: Getting text from TextField

    Quote Originally Posted by asdfg View Post
    Not very familiar with swing, but try to to use get() and attach toString().
    Also you may get text from field.. if you use something like 'on button click'.
    Because itself java program dont have endless loop who keep refreshing.
    Thanks, it isn't Swing though. JavaFX

  4. #4
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    13,541
    Rep Power
    25

    Default Re: Getting text from TextField

    Have you checked the values for each of the fields in that method?
    I would consider logging them to the console to see what they are.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  5. #5
    Yakg is offline Member
    Join Date
    Dec 2010
    Posts
    88
    Rep Power
    0

    Default Re: Getting text from TextField

    Quote Originally Posted by Tolls View Post
    Have you checked the values for each of the fields in that method?
    I would consider logging them to the console to see what they are.
    It might be the thing, I think I saw an error that the textfield object is indeed null.. But I don't understand why..
    Obviously it should be initialized but I thought it is being taken care off by GluOn..

  6. #6
    benji2505 is offline Senior Member
    Join Date
    Sep 2014
    Location
    MA, USA
    Posts
    390
    Rep Power
    4

    Default Re: Getting text from TextField

    Open your fxml file in the fxml editor and check:
    1. that the Textfield has an fx:id. If not, you have entered der fx:id in the wrong spot in Scenebuilder. Use the tab on the bottom right that says Code: Textfield and use the top textfield "fx:id".
    2. that the spelling in the fxml file and your fxml controller are exactly the same. upper/lower case does matter here.
    Save and re-run, see whether the fxml loader gives you an error upon startup again. Functionality will be flawed as long as the fxml Loader gives you an error at compile time.

    Hint:You do not have to implement the Initializable Interface in your controller class and you do not have to use Parameters in your initialize method if you use the annotation @FXML in front of your initialize() method rather than the @Override annotation.

  7. #7
    Yakg is offline Member
    Join Date
    Dec 2010
    Posts
    88
    Rep Power
    0

    Default Re: Getting text from TextField

    Quote Originally Posted by benji2505 View Post
    Open your fxml file in the fxml editor and check:
    1. that the Textfield has an fx:id. If not, you have entered der fx:id in the wrong spot in Scenebuilder. Use the tab on the bottom right that says Code: Textfield and use the top textfield "fx:id".
    2. that the spelling in the fxml file and your fxml controller are exactly the same. upper/lower case does matter here.
    Save and re-run, see whether the fxml loader gives you an error upon startup again. Functionality will be flawed as long as the fxml Loader gives you an error at compile time.

    Hint:You do not have to implement the Initializable Interface in your controller class and you do not have to use Parameters in your initialize method if you use the annotation @FXML in front of your initialize() method rather than the @Override annotation.
    I think it was the kind of textField.. I chose a fancy looking one from Gluon and when I changed to the regular one it just worked :)

    Thanks

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

    Default Re: Getting text from TextField

    sounds unusual, but hey - whatever works.
    So, the getText() method is working now?

  9. #9
    Yakg is offline Member
    Join Date
    Dec 2010
    Posts
    88
    Rep Power
    0

    Default Re: Getting text from TextField

    Quote Originally Posted by benji2505 View Post
    sounds unusual, but hey - whatever works.
    So, the getText() method is working now?
    Yeah it's working great, I think that it is probably wasn't a TextField object but something else which I can't be bothered finding out at the moment, the newer one is a more modern looking though but for now plain old textfield is good too.

Similar Threads

  1. Textfield not updating text value
    By Silvertale in forum AWT / Swing
    Replies: 16
    Last Post: 05-06-2014, 02:30 AM
  2. Checking textfield text
    By jsobel in forum New To Java
    Replies: 4
    Last Post: 06-14-2012, 04:48 AM
  3. Save text in Textfield to an array
    By Norah in forum New To Java
    Replies: 1
    Last Post: 06-12-2012, 05:00 PM
  4. Replies: 2
    Last Post: 06-14-2010, 10:18 AM
  5. accessing the text of textfield of one class in another
    By vasavi.singh in forum Advanced Java
    Replies: 3
    Last Post: 04-05-2009, 02:54 PM

Posting Permissions

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