Int Value into text Field - java

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()));
});

Related

convert a data string to float in Java

I have the problem that in my program I have 4 annotated combo boxes and 2 of them return a number but in String type, which I need to store in a floating type variable, My combo box is called combo2 and
jComboBox4, the 2 variables that you see are the ones in which I need to store the data but I need to convert them. I would appreciate your help. Greetings.
//My combobox is combo2 and jComboBox4
// I need save in this variables
private void jButton1MouseClicked(java.awt.event.MouseEvent evt) {
int numeroextra1;
int numeroextra2;
}
You can convert string variable to float as following:
string s = "123.45";
float f = Float.parseFloat(s);
You can use following methods:
String s = "10.01";
Float fObject = Float.valueOf(s);//get Float object
float fNumber = Float.parseFloat(s);//get float number

Incrementing by 1 when pressing a button

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);
}

Need an android button to execute with an intentionally blank EditText box

I have a simple screen with two EditText Boxes and 1 button. I want users to be able to enter various integers into the boxes and the program will perform different operations based on the specific number entered into the box. I'm trying to have them only enter one number at a time but the code wont seem to execute unless both boxes have something in them. And I'm having the if statements check for nulls on the respective boxes before executing to determine which piece of code to execute.
public void button(View view) {
double x, y;
EditText freq1 = findViewById(R.id.freq1);
EditText freq2 = findViewById(R.id.freq2);
TextView str1 = findViewById(R.id.freqanswer);
TextView str2 = findViewById(R.id.injVolt);
TextView error1 = findViewById(R.id.error1);
String strf1, strf2;
strf1 = freq1.getText().toString();
strf2 = freq2.getText().toString();
try {
f1 = Double.parseDouble(strf1);
f2 = Double.parseDouble(strf2);
if ((f1 >= 225) & (f1 <= 312) & (strf1.isEmpty())) {
x = f1 + 20.6;
y = x / 4;
str1.setText(String.format("%.3f", y));
}
}
catch (Exception e){
error1.setText("splat");
}
finally {
InputMethodManager input = (InputMethodManager)
getSystemService(Context.INPUT_METHOD_SERVICE);
input.hideSoftInputFromWindow(getCurrentFocus().getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);
}
I included only 1 formula for the sake of brevity, but I'm going to be putting about 6 if statements, checking for number ranges and nulls on this button and I just need it to run with one box being empty. I know the formula works by having play with various outputs, it just won't run with the strf1.isEmpty(). Something has to be in that second box for the formula to execute.
Would appreciate any help
I think you should check before assigning:
if(strf1.isEmpty()){strf1="0";} //if assuming zero does not change the formula's output
if(strf2.isEmpty()){strf2="0";}
f1 = Double.parseDouble(strf1);
f2 = Double.parseDouble(strf2);
this way you are assured of a default value.

Getting the name of a JRadioButton in Java

I am trying to get the names of all selected JRadioButtons from my graphics interface. As such I create the allFacilities array in which I include all of my JRadioButtons.
The first for loop serves to find the number of selected radio buttons.
The second for loop aspires to get the name of each selected button.
When checking what the .getName() returns:
System.out.println("A##" + button.getName());, only null is returned for all cases.
Here is my code:
JRadioButton[] allFacilities = {restaurant, laundry, parking};
int selectedFacilitiesCounter = 0;
for(JRadioButton check : allFacilities) {
if(check.isSelected()) {
selectedFacilitiesCounter += 1;
}
}
String[] selectedFacilities = new String[selectedFacilitiesCounter];
int index = 0;
for(JRadioButton button : allFacilities) {
if(button.isSelected()) {
System.out.println("A##" + button.getName());
switch(button.getName()) {
case "restaurant":
selectedFacilities[index] = "restaurant";
break;
case "laundry":
selectedFacilities[index] = "laundry";
break;
case "parking":
selectedFacilities[index] = "parking";
break;
default:
System.out.println("Facility Not Found");
}
index += 1;
}
}
Does anybody have any ideas on how I can solve my problem?
I believe that what you want is this:
JRadioButton button = new JRadioButton("test");
System.out.println(button.getText());
Which will print test.
The method getName retrieves the name of the component, which you should've set with setName, which I believe you didn't.

Java/JavaFX8: Perform action when input in TextField has certain length

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
}
});

Categories