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?
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);
If you just want to get input from the user the easiest way is to use a dialog:
double value = Double.parseDouble(
JOptionPane.showInputDialog("please enter value"));
The correct way to use JTextField is
JTextField box = new JTextField(" Enter Initial Velocity");
String velocity_str = box.getText();
Than parse into a double
double initialvelocity = Double.parseDouble(velocity_str);
See this tutorial for using a jTextField here.
You want to instantiate the box like:
JTextField myTextField = new JTextField("enter your initial velocity");
//Probably should do some kind of validation on the input if you only want numbers
//get The Text and parse it as a double
double initVelocity = Double.parseDouble(myTextField.getText());
Related
I have a program which calculates an integer and then uses the value within a JLabel. On intital creation everything is fine with the initialized value, but when I change the value of the int within the label I can't seem to find a way to update the JLabel. The relevant code is as follows:
JLabel carbLbl;
int totCarbs = 0;
public Main() {
carbLbl = new JLabel("Total Carbs: " + totCarbs);
carbLbl.setFont(new Font("KidSans", Font.PLAIN, 38));
carbLbl.setAlignmentX(Component.RIGHT_ALIGNMENT);
void addFoodToTable() {
String[] s = new String[3];
s = (String[]) foodData.get(foodChoice.getSelectedIndex());
foodList.addRow(s);
totCarbs += Integer.parseInt(s[2]);
carbLbl.repaint();
}
}
There's obviously much more code, but it's too long to include the entire script. Is there a way I can have the label update whenever I invoke the addFoodToTable() method?
The JLabel is not "bound" to your integer variable. When you change the integer you need to update the JLabel using carbLbl.setText(String.valueOf(totCarbs))
Is there not a way to simply update the JLabel using the initial constructor parameters?
carbLbl = new JLabel("Total Carbs: " + totCarbs);
What parameters? There is only a single parameter, the String to be displayed.
The compiler concatenates the hard coded String with the value of your "totCarbs" variable to create a single String.
The compiler will essentially treat the above code like:
String text = "Total Carbs" + totCarbs;
carbLbl = new JLabel( text );
The JLabel has no idea how the String was created (ie. that a variable was used to build the string).
I understand the concept of concatenation, but I just feel like that's a workaround
It is not a work around. The API of the setText(...) method states you provide a single String. So, if you want to update the label you need to provide the entire String.
How to convert JTextField to String and String to JTextField in Java?
how to convert JTextField to string
and string to JTextField in java
If you mean how to get and set String from jTextField then you can use following methods:
String str = jTextField.getText() // get string from jtextfield
and
jTextField.setText(str) // set string to jtextfield
//or
new JTextField(str) // set string to jtextfield
You should check JavaDoc for JTextField
// to string
String text = textField.getText();
// to JTextField
textField.setText(text);
You can also create a new text field: new JTextField(text)
Note that this is not conversion. You have two objects, where one has a property of the type of the other one, and you just set/get it.
Reference: javadocs of JTextField
The JTextField offers a getText() and a setText() method - those are for getting and setting the content of the text field.
JTextField allows us to getText() and setText() these are used to get and set the contents of the text field, for example.
text = texfield.getText();
hope this helps
I want to pass the JLabel to Integer, The following code does not work even with Integer.valueOf() and Integer.parse()
This are the following code I've Tried:
Test 1:
JLabel life = new JLabel("204");
int x = Integer.valueOf(life).intValue();
Test 2:
JLabel life = new JLabel("204");
int x = Integer.parseInt(life);
No. You can't magically convert a Label to Integer.
However you can get the string of that label and then convert.
JLabel life = new JLabel("204");
int x = Integer.parseInt(life.getText());
Note that, you'll be succeed when there is proper text. For ex
"203", "34343" works but not "A2342"
I am trying to use a button in order to add doubles each time and have used got this at the moment.
btnAnswer3 = new JButton("C");
btnAnswer3.setBackground(Color.YELLOW);
btnAnswer3.setHorizontalAlignment(SwingConstants.LEFT);
btnAnswer3.addActionListener(new ActionListener() {double scoreAdder, currentScore, ans;
scoreAdder = 30000.00;
currentScore = 0.0;
ans=scoreAdder+currentScore;
currentScoretxt.setText(Double.toString(ans)); //This is textfield in which I wish to display the doubles.
}
});
It already displays a number but once I want it to keep adding up each time the Jbutton is clicked. Please let me know how to do this using my code. Regards.
You need to declare this variables out of the ActionListener
double scoreAdder, currentScore, ans;
if not you are just overwriting the math operation and not acumulating the values...
move those declarations and place it as class variables...
So, im making a program for calculating water catchment. And I have 2 textareas(JFrame) and I need to convert them to a double, so, basically I want to do this!
double a = textarea1;
double b = textarea2; // textarea1 and textarea2 are JTextArea's
double c = a * b * 0.0632
How to convert JTextArea to double or how to make a double with the same value as what the user put in the JTextArea?
I think you need this:-
double a = Double.parseDouble(textarea1.getText());