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: JTextField (JDoubleField etc)

  1. #1
    Junior Member
    Join Date
    Mar 2014
    Location
    sweden
    Posts
    29
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default JTextField (JDoubleField etc)

    Hello,

    I'm working with a project there ill need to enter some values for an Object and for this i'm using JTextField. However some of the variables are of the types double, int and bool. Is there a different method of reading thees types?

    for example:
    double playTime = this.playTime.getText();
    This wont work because playTime is of type double and .getText(); takes a String.

    How will i handle this? Id rather not convert from String to Double if its possible.


  2. #2
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    4,509
    My Mood
    Mellow
    Thanks
    155
    Thanked 614 Times in 601 Posts

    Default Re: JTextField (JDoubleField etc)

    getText() returns a String, and there's no way around that. Converting the String to the actual data type is the only way. In a way, this gives the programmer maximum flexibility, allowing the user to put anything into a JTextField and the programmer the option to interpret the input as needed, generating an error if the input is invalid. Maximum flexibility can be a blessing and a curse.

    Good luck!

  3. #3
    Junior Member
    Join Date
    Mar 2014
    Location
    sweden
    Posts
    29
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: JTextField (JDoubleField etc)

    I ended up going with this method:
    double playTime = Double.parseDouble(this.title.getText());

    I think this will work somewhat good tho ill need to cast some error statements in order for it to run safely.

    Thanks for the input.

  4. #4
    Member
    Join Date
    Jul 2013
    Location
    Europe
    Posts
    400
    Thanks
    0
    Thanked 68 Times in 57 Posts

    Default Re: JTextField (JDoubleField etc)

    You could also use a JSpinner. These might be better suited when dealing with numbers as input.

Similar Threads

  1. Can't change value in JTextField
    By iHank in forum AWT / Java Swing
    Replies: 1
    Last Post: December 4th, 2013, 09:43 AM
  2. JTextField
    By DR_W in forum What's Wrong With My Code?
    Replies: 3
    Last Post: November 25th, 2013, 10:47 AM
  3. [SOLVED] JTextfield
    By usherlad in forum Java Theory & Questions
    Replies: 4
    Last Post: August 22nd, 2012, 11:24 AM
  4. JTextField
    By Karthik Prabhu in forum AWT / Java Swing
    Replies: 7
    Last Post: June 20th, 2012, 10:54 AM
  5. Need help int JTextField
    By n00b in forum What's Wrong With My Code?
    Replies: 7
    Last Post: November 1st, 2011, 09:57 AM