Results 1 to 4 of 4
Like Tree2Likes
  • 1 Post By Norm
  • 1 Post By benji2505

Thread: Problem with text field and getText

  1. #1
    perlfan is offline Member
    Join Date
    Dec 2015
    Posts
    2
    Rep Power
    0

    Default Problem with text field and getText

    Hi there & Merry Christmas,

    I just started with Netbeans and JavaFx and I'm already facing problems that may seem very trivial to some of you. I'm simply trying to read from a text field and to paste the input into a label.
    Java FX Code:
    label.setText(useruser.getText());
    Unfortunately, it doesn't work. I don't see anything in the label.
    However,
    Java FX Code:
    label.setText("text input");
    works when clickling on a button.

    Below is my complete controller code. Thanks a lot for help!

    FRANK

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

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

    Default Re: Problem with text field and getText

    Does the setText() method get executed?
    Is there anything in the useruser field?

    Can you make a small, complete program that compiles, executes and shows the problem?
    perlfan likes this.
    If you don't understand my response, don't ignore it, ask a question.

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

    Default Re: Problem with text field and getText

    I have tested that snippet here and your getText() and setText() method are correct as is. If you instantiate your TextField useruser with an initial value, this value should show up in the label. If the text changes you have to call that method in order to update the label. Alternatively JavaFX offers the sleek method to bind the value of the label to the text in the TextField.

    As for the event handling: passing the event into this method is probably a reminiscent of the original code, it is not needed here. You can set the event handling methods either in the fxml file (e.g. in SceneBuilder) or you can write your own JavaFX specific eventhandler here and hook up the button to it:

    Java FX Code:
    @FXML
    public Button goButton = new Button();
    goButton.setOnAction(buttonPressed);
    
    EventHandler<ActionEvent> buttonPressed = new EventHandler<ActionEvent>(){
    			public void handle(ActionEvent event) {
    				doSomethingNiceItsXmas();
    			}
    		};
    perlfan likes this.

  4. #4
    perlfan is offline Member
    Join Date
    Dec 2015
    Posts
    2
    Rep Power
    0

    Default Re: Problem with text field and getText

    Sorry for the late reply - I was away during Christmas. In the meantime I found out what was wrong in the code - apparently @FXML is needed before every single declaration of a variable:
    @FXML
    private Label label;
    @FXML
    private TextField useruser;

    One @FXML was missing in my old code which is why useruser wasn't initialized. So thanks again for taking a look and trying to help!
    FRANK

Similar Threads

  1. problem with check box and text field
    By mercy_angel in forum AWT / Swing
    Replies: 2
    Last Post: 05-24-2013, 11:10 PM
  2. Alignment and Text Field size problem of Frame.
    By Javasubbu in forum AWT / Swing
    Replies: 2
    Last Post: 12-02-2011, 04:16 PM
  3. getText(); problem
    By The_Sponzy_Paradox in forum Java Applets
    Replies: 5
    Last Post: 04-22-2010, 03:07 AM
  4. Problem Reading in data from Text Field
    By markious in forum AWT / Swing
    Replies: 2
    Last Post: 02-12-2010, 02:29 AM
  5. Problem with text field patern
    By Prashant.surwade in forum Advanced Java
    Replies: 1
    Last Post: 09-05-2009, 02:45 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
  •