Results 1 to 3 of 3
Like Tree2Likes
  • 2 Post By chaucer345

Thread: How to get text typed into a text box into a public string

  1. #1
    chaucer345 is offline Member
    Join Date
    Dec 2012
    Posts
    39
    Rep Power
    0

    Default How to get text typed into a text box into a public string

    Okay, so this is a really basic question that I'm sure has an easy answer. I'm trying to get some sort of data that can be accessed by other parts of my program from user input. I've made a JPanel to give my program a GUI and I could basically write the whole rest of the program if I could figure out how to make this chunk of code:

    Java Code:
     private void chooseButtonActionPerformed(java.awt.event.ActionEvent evt) {                                             
        String text = userInput.getText();
        int intEntered = Integer.valueOf(text);
        }
    Put the data from the text box (userInput is a swing text box) into a publicly accessible variable. Unfortunately, the NetBeans compiler doesn't let me swap that method from private to public for some reason and when I try to copy/past that method earlier and modify the copy pasted method, I find I can't delete the original method, I can't even comment it out.

    Thus I call on more experienced programmers for advice. If I can get any publicly accessible variable at all out of user input I can write code to translate it, I just need what the user types to modify a variable accessible to the rest of the classes that inherit from the GUI class.

    What should I do? Any and all assistance would be appreciated.

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default Re: How to get text typed into a text box into a public string

    Rather than put the data into a variable that has been declared in that method and is thus visible only in that method, consider putting that data into a non-static variable of the class, one whose value can be obtained by other classes via a getter method.

  3. #3
    chaucer345 is offline Member
    Join Date
    Dec 2012
    Posts
    39
    Rep Power
    0

    Default Re: How to get text typed into a text box into a public string

    Hot dang! that worked perfectly, I'd tried something like this, but accidentally put the variable declaration in a constructor. Thank you so much Fubarable! I'm not sure what the count is on times you've pulled my butt out of the fire at this point, but it's getting up there.
    PhQ and Fubarable like this.

Similar Threads

  1. Replies: 0
    Last Post: 07-24-2012, 09:07 AM
  2. Replies: 1
    Last Post: 03-06-2012, 11:28 PM
  3. Replies: 8
    Last Post: 07-17-2011, 03:38 PM
  4. Replies: 7
    Last Post: 03-05-2011, 10:25 AM
  5. Replies: 0
    Last Post: 12-09-2010, 06:56 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
  •