Search code examples
javaswingjtextfield

Get input from a jtextfield


How would I get user input from a jtextfield in my GUI? Would I use java.util.Scanner or another class?

JTextField a = new JTextField("Input Here")
//Put calculation formula here

Solution

  • A GUI is an event driven environment. You will need to listen to the events that the component is producing.

    For example, JTextField raises ActionEvents (amongst others) which tells you that the user has (on most systems) pressed the Enter key.

    Start by taking a look at How to use Textfields

    When your ready, you can use getText to get the String value that the user entered...