Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 4 of 4

Thread: Passing JTextField as String parameter

  1. #1
    Junior Member
    Join Date
    Feb 2010
    Posts
    5
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Passing JTextField as String parameter

    Hello,

    I made a GUI for a simple class that stores accountnumbers and customernames in an arraylist.
    In the class I made a public String getAccountNumber(customername), this returns the accountnumber that is linked to a customer with "customername".

    In the GUI I made a JTextField where one can provide a customername and a button that triggers the getAccountNumber with parameter: JTextField.getText().
    However, it doesn't work with parameter: JTextField.getText(). When I type the name of the customer myself (hardcoded) it does work (???).

    Is there something else I have to convert/... to pass the JTextField.getText() to my getter so it returns the result correctly?

    Best regards,
    Christophe


  2. #2
    Member
    Join Date
    Apr 2010
    Location
    The Hague, Netherlands
    Posts
    91
    Thanks
    3
    Thanked 10 Times in 10 Posts

    Default Re: Passing JTextField as String parameter

    What is the error you get?

    I'm not sure but try: JTextField.getText().toString()

  3. #3
    Junior Member
    Join Date
    Feb 2010
    Posts
    5
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: Passing JTextField as String parameter

    Hi,

    The ...toString() doesn't work either.

    I am not getting a specific error, it just doesn't find any results and returns the String 'no result found' (which I programmed to do if no matches are found).
    I put my code in attachment, feel free to try, maybe one of you guys knows what the problem is. I am quite new at Java (and programming in general).

    Best regards,
    Christophe
    Attached Files Attached Files

  4. #4
    Junior Member
    Join Date
    Feb 2010
    Posts
    5
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: Passing JTextField as String parameter

    Hello,

    I found the solution for this problem.
    In my procedure I used this code:

    if('string1' == 'string2') {...} => this is wrong since I want to compare the content of the strings, I had to use:

    if('string1.equals(string2)) {...} => this is correct, this compares the content of both strings.

    That's it.

    Best regards,
    Christophe
    Last edited by Christophe; April 11th, 2010 at 07:01 AM.

Similar Threads

  1. [SOLVED] how can i resize JTextField, JButton using GridBagLayout,... TNX
    By sny in forum AWT / Java Swing
    Replies: 0
    Last Post: March 4th, 2010, 09:57 AM
  2. Passing in more than one flag
    By anon181 in forum Loops & Control Statements
    Replies: 3
    Last Post: February 2nd, 2010, 06:37 AM
  3. Passing objects as a constructor parameter
    By derky in forum Object Oriented Programming
    Replies: 2
    Last Post: October 27th, 2009, 05:31 AM
  4. Issue with JTextField Locking
    By PekinSOFT.Systems in forum AWT / Java Swing
    Replies: 0
    Last Post: October 1st, 2009, 12:12 PM
  5. [SOLVED] JTextField not visible in swing
    By Sterzerkmode in forum AWT / Java Swing
    Replies: 4
    Last Post: May 21st, 2009, 08:37 AM