My code is very long so I will only be adding snippets that are relevant.
Okay so I've been trying to increment a label by one using the following code:
btnComplete.setOnAction(new EventHandler<ActionEvent>() {
public void handle(ActionEvent e) {
//if the list has a minimum of 1 item
if (currentCartTxt.getItems().size() > 0) {
int sales=0;
sales++;
String x = Integer.toString(sales);
numberOfSalesTxt.setText(x);
}
}});
However it only changes my textfield to 1 and never increases it. Any help would be greatly appreciated.
the currentCartTxt is a listView and the numberOfSalesTxt is a textfield.
Basically to explain my app, I have a list of items that I am adding to a textfield (currentCartTxt) and I need to press the complete button whenever but there must be at least 1 item in the textfield. And every time the button is pressed the textfield(numberOfSalesTxt) increases by 1.
Thanks!
You have to:
read current value (from Label/View/TextView...)
increment it (just add 1)
set new value to view
if (currentCartTxt.getItems().size() > 0) {
// get current value
String text = numberOfSalesTxt.getText();
// convert it from "String" to "int"
int sales = Integer.parseInt(text);
// increment it
sales++;
// Convert from "int" to "String"
String x = Integer.toString(sales);
// Set new value
numberOfSalesTxt.setText(x);
}
Related
In JavaFX, I have a TextField Called "randomIntegerField" I need to auto populate this textfield JavaFX with an integer value returned by a method when i clik on a button.
Here is the method which return the int Value
private int randomNum(){
Random random = new Random();
int integerIntered = Integer.parseInt(numberField.getText());
return random.nextInt(integerIntered - 0 + 1) + 0;
}
Here is the Button
bntGo.setText("Go");
bntGo.setOnAction(e -> {
randomIntegerField = String.valueOf(randomNum()); // The error i get is String can not converted into TextField.
});
Help me please
You need to set the text property of randomIntegerField (which I assume is a TextField), for example...
bntGo.setOnAction(e -> {
randomIntegerField.setText(String.valueOf(randomNum()));
});
In my app, I need for values from buttons (the value is taken from an SQLite database) to be displayed in one of 13 JTextFields, and they can be in any order. How do I make it possible for the value to be displayed in the next available JTextField, if say the first one is empty?
The only thing I could think of was
if (textField.getText().isEmpty())
{
String text = String.valueOf(num);
textField.setText(textField.getText() + text);
}
What should I do next? How should I go around the else statement? Should I even use it?
Thanks in advance!
If I understand your question correctly,this should be the idea
JTextField[] fields = new JTextField[13];
firstText.setName("First Text") ;
.......
field[0] = firstText;
field[1] = SecondText;
//then add remaining textfields
for(JTextField txtField : fields) {
if(txtField.getText().equals("") ) {
// do whatever you want
}else{
// do whatever you want
}
}
I'm working on a program in Javafx, which allows the user to input values in TextFields. Additionaly, there is an empty Choicebox, where a value beginning from "1" is added. Everytime he presses the "Submit" button, the TextFields also get cleared and he can input another set of values in said TextFields.
Let's say he inputs five sets of values and presses "Submit", I'd like to have those values stored in an array, so when he chooses a number in the ChoiceBox, the corresponding values are shown again in the TextFields.
For this I need to store them in an array. Below is the eventhandler for the buttonclick
private void next(javafx.event.ActionEvent event) {
double c_stueck = Double.parseDouble(stueck.getText());
double c_pr_pro_einheit = Double.parseDouble(pr_pro_eh.getText());
double c_betrag = Double.parseDouble(betrag.getText());
String c_bezeichnung=bezeichnung.getText().toString();
c_betrag += c_stueck * c_pr_pro_einheit;
String c_betragval = String.valueOf(c_betrag);
betrag.setText(c_betragval);
stueck.setText("");
pr_pro_eh.setText("");
bezeichnung.setText("");
for(int j=0; j<=49;j++)
{
arr_stueck[j]= c_stueck;
arr_pr_pro_eh[j]= c_pr_pro_einheit;
arr_bezeichnung[j]= c_bezeichnung;
position.getItems().add(j);
}
}
The problem lies herein, that everytime the eventhandler is fired, the array is reset. Is there a way to save the increment, so the next time the button is pressed, the for loop continues from the last increment?
I am currently using the AutoComplete TextField functionality from ControlsFX to show suggestions when the user is typing. The amount of suggestions is large, and therefore the list doesn't fit on the page.
I would like to set the length of the list of strings to a maximum, but this is not yet possible in ControlsFX (as fas as I can conclude that). Therefore, I was thinking of a workaround, in which the list only shows up when the user has typed a string of 3 characters or more.
I have now set this action to execute when the TextField is clicked (where searchCustomer is my TextField):
#FXML
private void searchCustomer() {
//Get all customers from shop
String[][] customersOfShop = octocash.Main.databaseConnection.getData("some query",
Arrays.asList("some columname"));
//Convert 2D array to 1D array
int noOfRows = customersOfShop.length;
String[] customersForList = new String[noOfRows];
for(int k=0; k<noOfRows; k++) {
customersForList[k] = customersOfShop[k][0];
}
//Set values to AutoComplete TextField
TextFields.bindAutoCompletion(searchCustomer, customersForList);
}
How to do this in java/javaFX8?
One of approaches can be to observe text length:
IntegerBinding ib = Bindings.length(textField.textProperty());
ib.addListener((ObservableValue<? extends Number> observable, Number oldValue, Number newValue) -> {
if(newValue.intValue() >= 3) {
// trigger auto complete
}
});
OK so I have these 2 labels where if both are them are 0, a button will be disabled. This is what I have done but didn't manage to do it, please guide me thanks a lot!
int quantity = 0;
int sum = 0;
I initialize them as 0, after that going through some IF loops and working well, and there's one event which is sort of like clear, I reassign them 0 again which looks like this:
quantity = 0;
sum = 0;
Then now I have 2 Labels which I want to compare to these 2 value, if both are 0, then disable a button, This is what I have done but failed ,the button still remained enabled. Then I realized I'm comparing to string 0 instead of integer 0, how can I compare it with the quantity and sum?? thanks a lot!
if ("0".equals(jLabel4.getText()) || ("0".equals(jLabel4.getText())));
{
jButton2.setEnabled(false);
}
if you want to compare integers, why compare Strings?
0 == Integer.parseInt(jLabel4.getText());
also surround it with try-catch block
boolean equals = false;
try{
equals = ( 0 == Integer.parseInt(jLabel4.getText()));
}catch(NumberFormatException e){
//equals = false;
}
You should separate your gui code (the "view") from your non-gui logic code (the "model"). The GUI should display the state of the model, i.e., show in your JLabels the values held by the two ints of the model, but it is the model's ints which should be checked for 0 value, and then your GUI should enable or disable the button accordingly.