Java – Getting user input by JTextField in Java

java

I am trying to get user inputs from a textbox so I can take the user input to calculate the displacement for a ball.

I tried this

double initialvelocity = new JTextBox("enter your initial velocity");

so I have a variable with data that I can use. I just cant get it to work and this has been a obstacle to my first proper java project.

Could anyone suggest how I can get a variable to store data using JTextField or is there other alternatives?

Best Solution

A JTextField should be stored like this:

JTextField v0TextField = new JTextField ("initial velocity");

And when you want to access the current string in the text box:

String strV0TextBox = v0TextField.getText();

Then you'll want to parse this into a double:

double initialvelocity = Double.parseDouble(strV0TextField);