multiplying 2 digits in TextField - Java FX
up vote
0
down vote
favorite
I am struggling with a bug in my application.
General idea was to multiply base price by slider argument, for example if slide is set to 2, price = 2* price
The problem is that is I switch slider again math operations are accumulating,
for Example I switched slider to 2, then to 3, after that to 4
My result is price * 2 * 3 * 4 but my expectation is to have price * 4 remembered, how can I fix it?
daysSlider.valueProperty().addListener(new ChangeListener() {
@Override
public void changed(ObservableValue arg0, Object arg1, Object arg2) {
int i = Integer.parseInt(priceTextField.getText());
priceTextField.textProperty().setValue(
String.valueOf((int) daysSlider.getValue()* i ));
}
});
java javafx
add a comment |
up vote
0
down vote
favorite
I am struggling with a bug in my application.
General idea was to multiply base price by slider argument, for example if slide is set to 2, price = 2* price
The problem is that is I switch slider again math operations are accumulating,
for Example I switched slider to 2, then to 3, after that to 4
My result is price * 2 * 3 * 4 but my expectation is to have price * 4 remembered, how can I fix it?
daysSlider.valueProperty().addListener(new ChangeListener() {
@Override
public void changed(ObservableValue arg0, Object arg1, Object arg2) {
int i = Integer.parseInt(priceTextField.getText());
priceTextField.textProperty().setValue(
String.valueOf((int) daysSlider.getValue()* i ));
}
});
java javafx
add a comment |
up vote
0
down vote
favorite
up vote
0
down vote
favorite
I am struggling with a bug in my application.
General idea was to multiply base price by slider argument, for example if slide is set to 2, price = 2* price
The problem is that is I switch slider again math operations are accumulating,
for Example I switched slider to 2, then to 3, after that to 4
My result is price * 2 * 3 * 4 but my expectation is to have price * 4 remembered, how can I fix it?
daysSlider.valueProperty().addListener(new ChangeListener() {
@Override
public void changed(ObservableValue arg0, Object arg1, Object arg2) {
int i = Integer.parseInt(priceTextField.getText());
priceTextField.textProperty().setValue(
String.valueOf((int) daysSlider.getValue()* i ));
}
});
java javafx
I am struggling with a bug in my application.
General idea was to multiply base price by slider argument, for example if slide is set to 2, price = 2* price
The problem is that is I switch slider again math operations are accumulating,
for Example I switched slider to 2, then to 3, after that to 4
My result is price * 2 * 3 * 4 but my expectation is to have price * 4 remembered, how can I fix it?
daysSlider.valueProperty().addListener(new ChangeListener() {
@Override
public void changed(ObservableValue arg0, Object arg1, Object arg2) {
int i = Integer.parseInt(priceTextField.getText());
priceTextField.textProperty().setValue(
String.valueOf((int) daysSlider.getValue()* i ));
}
});
java javafx
java javafx
edited Nov 11 at 16:07
asked Nov 11 at 14:51
Tomek Młynarski
66
66
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
up vote
0
down vote
accepted
So the problem that you are having is getting the String from the Text Field and doing math operations with it.
The way that you can go around this problem is by parsing the String to an Integer by doing the following:
int baseCost = Integer.parseInt(textField.getValue());
This will give you the base cost as an Integer and then, you can multiply it by the number in the slider by using the similar method. Now, you can set the value in priceTextField
by doing the following:
//totalCost is an integer value calculated by multiplying the base cost and the slider's value.
priceTextField.textProperty().setValue(Integer.toString(totalCost));
I hope this helps!
With your new problem, this is why it is happening:
The reason that you are getting the problem is because each time the value in the slider is changed, you are setting priceTextField
to daySlider
times i
. Then, the next time that there is a change, you are setting i
equal to the value currently in the priceTextField
. This is why it is doing multiple multiplications.
If I remember correctly, what you were trying to do is multiply the TextField value by the Slider Value and display it in another TextField. Therefore, what you can do is say int i = Integer.parseInt(textField.getValue())
to get the value for your base cost. Then, you can multiply it with the daysSlider
value and do priceTextField.textProperty().setValue(
Integer.toString(i * sliderValue);
To solve your new problem:
ArrayList<Integer> baseCost = new ArrayList<>();
priceTextField.valueProperty().addListener(new ChangeListener(){
@Override
public void changed(ObservableValue arg0, Object arg1, Object arg2) {
baseCost.add(Integer.parseInt(priceTextField.getText()));
}
}
daysSlider.valueProperty().addListener(new ChangeListener() {
@Override
public void changed(ObservableValue arg0, Object arg1, Object arg2) {
priceTextField.textProperty().setValue(
String.valueOf((int) daysSlider.getValue() * baseCost.get(0) ));
}
});
ANSWER to your 3rd question (can be found in the chat):
This is how you make a runnable:
daysSlider.valueProperty().addListener(new ChangeListener() {
@Override
public void changed(ObservableValue arg0, Object arg1, Object arg2) {
Runnable r = new Runnable() {
@Override
public void run() {
priceTextField.textProperty().setValue(
String.valueOf((int) daysSlider.getValue() * baseCost.get(0) ));
}
};
Handler handler = new Handler();
//Run the runnable after 3000 milliseconds or 3 seconds.
handler.postDelayed(runnable, 3000);
}
});
Comments are not for extended discussion; this conversation has been moved to chat.
– Samuel Liew♦
Nov 11 at 22:37
add a comment |
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
0
down vote
accepted
So the problem that you are having is getting the String from the Text Field and doing math operations with it.
The way that you can go around this problem is by parsing the String to an Integer by doing the following:
int baseCost = Integer.parseInt(textField.getValue());
This will give you the base cost as an Integer and then, you can multiply it by the number in the slider by using the similar method. Now, you can set the value in priceTextField
by doing the following:
//totalCost is an integer value calculated by multiplying the base cost and the slider's value.
priceTextField.textProperty().setValue(Integer.toString(totalCost));
I hope this helps!
With your new problem, this is why it is happening:
The reason that you are getting the problem is because each time the value in the slider is changed, you are setting priceTextField
to daySlider
times i
. Then, the next time that there is a change, you are setting i
equal to the value currently in the priceTextField
. This is why it is doing multiple multiplications.
If I remember correctly, what you were trying to do is multiply the TextField value by the Slider Value and display it in another TextField. Therefore, what you can do is say int i = Integer.parseInt(textField.getValue())
to get the value for your base cost. Then, you can multiply it with the daysSlider
value and do priceTextField.textProperty().setValue(
Integer.toString(i * sliderValue);
To solve your new problem:
ArrayList<Integer> baseCost = new ArrayList<>();
priceTextField.valueProperty().addListener(new ChangeListener(){
@Override
public void changed(ObservableValue arg0, Object arg1, Object arg2) {
baseCost.add(Integer.parseInt(priceTextField.getText()));
}
}
daysSlider.valueProperty().addListener(new ChangeListener() {
@Override
public void changed(ObservableValue arg0, Object arg1, Object arg2) {
priceTextField.textProperty().setValue(
String.valueOf((int) daysSlider.getValue() * baseCost.get(0) ));
}
});
ANSWER to your 3rd question (can be found in the chat):
This is how you make a runnable:
daysSlider.valueProperty().addListener(new ChangeListener() {
@Override
public void changed(ObservableValue arg0, Object arg1, Object arg2) {
Runnable r = new Runnable() {
@Override
public void run() {
priceTextField.textProperty().setValue(
String.valueOf((int) daysSlider.getValue() * baseCost.get(0) ));
}
};
Handler handler = new Handler();
//Run the runnable after 3000 milliseconds or 3 seconds.
handler.postDelayed(runnable, 3000);
}
});
Comments are not for extended discussion; this conversation has been moved to chat.
– Samuel Liew♦
Nov 11 at 22:37
add a comment |
up vote
0
down vote
accepted
So the problem that you are having is getting the String from the Text Field and doing math operations with it.
The way that you can go around this problem is by parsing the String to an Integer by doing the following:
int baseCost = Integer.parseInt(textField.getValue());
This will give you the base cost as an Integer and then, you can multiply it by the number in the slider by using the similar method. Now, you can set the value in priceTextField
by doing the following:
//totalCost is an integer value calculated by multiplying the base cost and the slider's value.
priceTextField.textProperty().setValue(Integer.toString(totalCost));
I hope this helps!
With your new problem, this is why it is happening:
The reason that you are getting the problem is because each time the value in the slider is changed, you are setting priceTextField
to daySlider
times i
. Then, the next time that there is a change, you are setting i
equal to the value currently in the priceTextField
. This is why it is doing multiple multiplications.
If I remember correctly, what you were trying to do is multiply the TextField value by the Slider Value and display it in another TextField. Therefore, what you can do is say int i = Integer.parseInt(textField.getValue())
to get the value for your base cost. Then, you can multiply it with the daysSlider
value and do priceTextField.textProperty().setValue(
Integer.toString(i * sliderValue);
To solve your new problem:
ArrayList<Integer> baseCost = new ArrayList<>();
priceTextField.valueProperty().addListener(new ChangeListener(){
@Override
public void changed(ObservableValue arg0, Object arg1, Object arg2) {
baseCost.add(Integer.parseInt(priceTextField.getText()));
}
}
daysSlider.valueProperty().addListener(new ChangeListener() {
@Override
public void changed(ObservableValue arg0, Object arg1, Object arg2) {
priceTextField.textProperty().setValue(
String.valueOf((int) daysSlider.getValue() * baseCost.get(0) ));
}
});
ANSWER to your 3rd question (can be found in the chat):
This is how you make a runnable:
daysSlider.valueProperty().addListener(new ChangeListener() {
@Override
public void changed(ObservableValue arg0, Object arg1, Object arg2) {
Runnable r = new Runnable() {
@Override
public void run() {
priceTextField.textProperty().setValue(
String.valueOf((int) daysSlider.getValue() * baseCost.get(0) ));
}
};
Handler handler = new Handler();
//Run the runnable after 3000 milliseconds or 3 seconds.
handler.postDelayed(runnable, 3000);
}
});
Comments are not for extended discussion; this conversation has been moved to chat.
– Samuel Liew♦
Nov 11 at 22:37
add a comment |
up vote
0
down vote
accepted
up vote
0
down vote
accepted
So the problem that you are having is getting the String from the Text Field and doing math operations with it.
The way that you can go around this problem is by parsing the String to an Integer by doing the following:
int baseCost = Integer.parseInt(textField.getValue());
This will give you the base cost as an Integer and then, you can multiply it by the number in the slider by using the similar method. Now, you can set the value in priceTextField
by doing the following:
//totalCost is an integer value calculated by multiplying the base cost and the slider's value.
priceTextField.textProperty().setValue(Integer.toString(totalCost));
I hope this helps!
With your new problem, this is why it is happening:
The reason that you are getting the problem is because each time the value in the slider is changed, you are setting priceTextField
to daySlider
times i
. Then, the next time that there is a change, you are setting i
equal to the value currently in the priceTextField
. This is why it is doing multiple multiplications.
If I remember correctly, what you were trying to do is multiply the TextField value by the Slider Value and display it in another TextField. Therefore, what you can do is say int i = Integer.parseInt(textField.getValue())
to get the value for your base cost. Then, you can multiply it with the daysSlider
value and do priceTextField.textProperty().setValue(
Integer.toString(i * sliderValue);
To solve your new problem:
ArrayList<Integer> baseCost = new ArrayList<>();
priceTextField.valueProperty().addListener(new ChangeListener(){
@Override
public void changed(ObservableValue arg0, Object arg1, Object arg2) {
baseCost.add(Integer.parseInt(priceTextField.getText()));
}
}
daysSlider.valueProperty().addListener(new ChangeListener() {
@Override
public void changed(ObservableValue arg0, Object arg1, Object arg2) {
priceTextField.textProperty().setValue(
String.valueOf((int) daysSlider.getValue() * baseCost.get(0) ));
}
});
ANSWER to your 3rd question (can be found in the chat):
This is how you make a runnable:
daysSlider.valueProperty().addListener(new ChangeListener() {
@Override
public void changed(ObservableValue arg0, Object arg1, Object arg2) {
Runnable r = new Runnable() {
@Override
public void run() {
priceTextField.textProperty().setValue(
String.valueOf((int) daysSlider.getValue() * baseCost.get(0) ));
}
};
Handler handler = new Handler();
//Run the runnable after 3000 milliseconds or 3 seconds.
handler.postDelayed(runnable, 3000);
}
});
So the problem that you are having is getting the String from the Text Field and doing math operations with it.
The way that you can go around this problem is by parsing the String to an Integer by doing the following:
int baseCost = Integer.parseInt(textField.getValue());
This will give you the base cost as an Integer and then, you can multiply it by the number in the slider by using the similar method. Now, you can set the value in priceTextField
by doing the following:
//totalCost is an integer value calculated by multiplying the base cost and the slider's value.
priceTextField.textProperty().setValue(Integer.toString(totalCost));
I hope this helps!
With your new problem, this is why it is happening:
The reason that you are getting the problem is because each time the value in the slider is changed, you are setting priceTextField
to daySlider
times i
. Then, the next time that there is a change, you are setting i
equal to the value currently in the priceTextField
. This is why it is doing multiple multiplications.
If I remember correctly, what you were trying to do is multiply the TextField value by the Slider Value and display it in another TextField. Therefore, what you can do is say int i = Integer.parseInt(textField.getValue())
to get the value for your base cost. Then, you can multiply it with the daysSlider
value and do priceTextField.textProperty().setValue(
Integer.toString(i * sliderValue);
To solve your new problem:
ArrayList<Integer> baseCost = new ArrayList<>();
priceTextField.valueProperty().addListener(new ChangeListener(){
@Override
public void changed(ObservableValue arg0, Object arg1, Object arg2) {
baseCost.add(Integer.parseInt(priceTextField.getText()));
}
}
daysSlider.valueProperty().addListener(new ChangeListener() {
@Override
public void changed(ObservableValue arg0, Object arg1, Object arg2) {
priceTextField.textProperty().setValue(
String.valueOf((int) daysSlider.getValue() * baseCost.get(0) ));
}
});
ANSWER to your 3rd question (can be found in the chat):
This is how you make a runnable:
daysSlider.valueProperty().addListener(new ChangeListener() {
@Override
public void changed(ObservableValue arg0, Object arg1, Object arg2) {
Runnable r = new Runnable() {
@Override
public void run() {
priceTextField.textProperty().setValue(
String.valueOf((int) daysSlider.getValue() * baseCost.get(0) ));
}
};
Handler handler = new Handler();
//Run the runnable after 3000 milliseconds or 3 seconds.
handler.postDelayed(runnable, 3000);
}
});
edited Nov 11 at 22:51
answered Nov 11 at 14:55
Ishaan
7131417
7131417
Comments are not for extended discussion; this conversation has been moved to chat.
– Samuel Liew♦
Nov 11 at 22:37
add a comment |
Comments are not for extended discussion; this conversation has been moved to chat.
– Samuel Liew♦
Nov 11 at 22:37
Comments are not for extended discussion; this conversation has been moved to chat.
– Samuel Liew♦
Nov 11 at 22:37
Comments are not for extended discussion; this conversation has been moved to chat.
– Samuel Liew♦
Nov 11 at 22:37
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53249882%2fmultiplying-2-digits-in-textfield-java-fx%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown