Answer the question
In order to leave comments, you need to log in
How to give a specific number from a text field a specific string name?
I am writing a java application using javafx and scene builder.
In one of the fields you need to enter a number from 0 to 5.
How can I make each number correspond to a specific name?
that is: -
assign a name to each number. For example: -
get a number from the text field -
put the number into a variable that will display the name corresponding to the number
0 - "none"
1 - "development"
2 - "management"
3 - "accounting"
4 - "sales"
5 - "SEO"
In the future, I will process the variable of each number, for example, display it in another text field
. Here's how I got the text from the field.String departments = department.getText();
Controller class code:
package Email;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.TextField;
import javafx.scene.control.Label;
import javafx.event.ActionEvent;
import javafx.scene.input.MouseEvent;
import java.net.URL;
import java.util.ResourceBundle;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Controller implements Initializable {
//initialize fx:id fields
@FXML
private TextField name, lastName, company, department;
@FXML
private Label displayName, displayEmail, displayPassword, errorName, errorLastName, errorCompany, errorDepartment;
//The Method that shows result by clicking the button
@FXML
private void showResult(ActionEvent event) {
//Take input from text fields
String names = name.getText();
String lastnames = lastName.getText();
String companies = company.getText();
String departments = department.getText();
//create operator IF that checks if all fields are not empty and print result
if (name.getText() == null || name.getText().trim().isEmpty()
|| lastName.getText() == null || lastName.getText().trim().isEmpty()
|| company.getText() == null || company.getText().trim().isEmpty()
|| department.getText() == null || department.getText().trim().isEmpty())
{
display();
} else {
displayName.setText("Name : " + names + " " + lastnames);
displayEmail.setText("Email : " + names + "." + lastnames + "@");
displayPassword.setText("Password : ");
}
//adding the method checkFields to every field, just to simplify the code
checkFields(name, errorName);
checkFields(company, errorCompany);
checkFields(department, errorDepartment);
checkFields(lastName, errorLastName);
}
//Method that validates department textfield. This makes only numbers accepted
@FXML
boolean validateNumber(MouseEvent event) {
Pattern p = Pattern.compile("[0-5]");
Matcher m = p.matcher(department.getText());
if (m.find() && m.group().equals(department.getText())) {
department.getStyleClass().removeAll("error-field");
errorDepartment.setText("");
return true;
} else {
display();
department.getStyleClass().add("error-field");
errorDepartment.setText("Only 0-5 number is accepted!");
return false;
}
}
//Method that doesn't show result for all items when clicking a button
private void display() {
displayName.setText("");
displayEmail.setText("");
displayPassword.setText("");
}
//The method that makes checking. If a field is empty, method add red color to a field and add red label under a field
private void checkFields(TextField lastName, Label errorLastName) {
if (lastName.getText() == null || lastName.getText().trim().isEmpty()) {
lastName.getStyleClass().add("error-field");
errorLastName.setText("Field is Empty!");
} else {
lastName.getStyleClass().removeAll("error-field");
errorLastName.setText("");
}
}
//Method that provides acces to fields FXML
@Override
public void initialize(URL location, ResourceBundle resources) {
}
}
Answer the question
In order to leave comments, you need to log in
I don’t know if I understood the essence of the problem correctly, but try to take a Map:
Map<Integer, String> values = new HashMap<>();
values.put(0, "none");
values.put(1, "development");
// и т.д.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question