Java GUI, take input from JText class

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Koji
    New Member
    • Oct 2006
    • 11

    Java GUI, take input from JText class

    Ok. So, I don't know which class from javax.swing to use for this. I think it's JTextField, but anyway...

    I'm looking for a class that allows the user to type in, in this case, integer or double values, which I'll be able to take as a string, change to type I need, then perform operations with them.

    Example: 3 text fields. Miles, Gallons, and MPG. If the user puts in info for Miles, and Gallons, I should be able to take the user input, do my operation, then display the result (value for MPG) in the MPG text box.

    If it's the JTextField class, I don't know which methods to use for reading the input in the box or the method to display new text in the box after its initial construction.

    If anyone can help, it would be greatly appreciated.
  • Koji
    New Member
    • Oct 2006
    • 11

    #2
    Omg. I feel so stupid....

    .getText() and .setText().... I couldn't find them in the API because they were inherited methods. Omg... Sorry guys, scratch that question. Hahaha.

    Comment

    • r035198x
      MVP
      • Sep 2006
      • 13225

      #3
      Originally posted by Koji
      Ok. So, I don't know which class from javax.swing to use for this. I think it's JTextField, but anyway...

      I'm looking for a class that allows the user to type in, in this case, integer or double values, which I'll be able to take as a string, change to type I need, then perform operations with them.

      Example: 3 text fields. Miles, Gallons, and MPG. If the user puts in info for Miles, and Gallons, I should be able to take the user input, do my operation, then display the result (value for MPG) in the MPG text box.

      If it's the JTextField class, I don't know which methods to use for reading the input in the box or the method to display new text in the box after its initial construction.

      If anyone can help, it would be greatly appreciated.
      For input, use JTextfield. To get the data from the textfield use field.getText()

      For output do not use a textfield. You just want to display the data so use a JLabel. You can have its initial text to "" (nothing) then when your calculation is complete, use label.setText(r esult); where result is a string represantation of your result.

      Comment

      • r035198x
        MVP
        • Sep 2006
        • 13225

        #4
        Originally posted by Koji
        Omg. I feel so stupid....

        .getText() and .setText().... I couldn't find them in the API because they were inherited methods. Omg... Sorry guys, scratch that question. Hahaha.
        Guess I was just a bit late then!

        Comment

        • Koji
          New Member
          • Oct 2006
          • 11

          #5
          Thanks for replying though! Oh, and my homework requires that we display the answers in the TextField for the missing information, not in a JLabel. :D

          Thanks for your delayed help, hehe.

          Comment

          Working...