How to limit a value between 0 and 10 with java fx textfield

0

Goodbye everyone, I'm new to this and I'm developing an application with java fx, but I've run into a problem and I do not know how to limit my text with a value range from 0 to 10. I pass the code generated:

import AlmacenNota.AlmacenNota;
import java.net.URL;
import java.util.ResourceBundle;


import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.Button;

import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;
import javafx.scene.control.TextField;

import javafx.scene.control.cell.PropertyValueFactory;
import javafx.scene.input.KeyEvent;


public class vistaController implements Initializable {

     //Añadimos todos  nuestros componentes implementados en el Scener Builder
    //Declaracion de los TextField
    @FXML private TextField txfDNI;
    @FXML//Restriccion de no superar en mas de 8 caracteres para el DNI textField
private void handleKeyTyped(KeyEvent event) {
    System.out.println(event.getText());
    if (txfDNI.getText().length() >= 8) {
        event.consume();
    }

}

private void handleTxfNota(ActionEvent event){
if(Integer.parseInt(txfNota.getText())>0) {

   event.consume();

}


}


    @FXML private TextField txfModulo;
    @FXML private TextField txfNota;
    @FXML private TextField txfRecuperacion;
    //Declaración del boton
    @FXML private Button btnGuardarNota;

    //Declaramos las tablas y columnas
    @FXML private TableView <AlmacenNota>TvTabla;//La tabla tendrá los objetos de la tabla notas
    @FXML private TableColumn TClDNI;
    @FXML private TableColumn TClModulo;
    @FXML private TableColumn TClNota;
    @FXML private TableColumn TClRecuperacion;
    //Declaracion parecido a un ArrayList para visualizar los datos
    ObservableList<AlmacenNota>Notas;

     //Restricciones:
    private final String estiloFondo = "-fx-control-inner-background: ";
    private final String colorCorrecto  = " #009846";
    private final String colorError= "#E60026";

    @FXML
    private void handleButtonAction(ActionEvent event) {
        AlmacenNota almacenNota = new AlmacenNota();
        almacenNota.DNI.set(txfDNI.getText());//nos da los contenidos que se introduzcan en el textField
        almacenNota.Modulo.set(txfModulo.getText());//nos da los contenidos que se introduzcan en el textField
        almacenNota.Nota.set(Integer.parseInt(txfNota.getText()));//Debemos parsear a integer
        almacenNota.Recuperacion.set(Integer.parseInt(txfRecuperacion.getText()));//Debemos parsear a integer
        Notas.add(almacenNota);//Se van añadiendo nuestros objetos creados






         try{

                txfNota.setText("");
                txfNota.setStyle(estiloFondo + colorCorrecto);
                txfRecuperacion.setText("");
                txfRecuperacion.setStyle(estiloFondo + colorCorrecto);
}
       catch (Exception e){
                 txfNota.setStyle(estiloFondo + colorError);
                 txfRecuperacion.setStyle(estiloFondo + colorError);

}




    }


     //Llamamos al metodo this inicializar tabla
       public void inicializarTabla(){
        TClDNI.setCellValueFactory(new PropertyValueFactory<>("DNI"));//inicializamos el atributo DNI
        TClModulo.setCellValueFactory(new PropertyValueFactory<>("Modulo"));//inicializamos el atributo Modulo
        TClNota.setCellValueFactory(new PropertyValueFactory<>("Nota"));//inicializamos el atributo Nota
        TClRecuperacion.setCellValueFactory(new PropertyValueFactory<>("Recuperacion"));//inicializamos el atributo Recuperacion

//Asignar los datos al observableList y en la tabla ponemos los items
        Notas = FXCollections.observableArrayList();
        TvTabla.setItems(Notas);



}


    @Override
    public void initialize(URL url, ResourceBundle rb) {
      this.inicializarTabla();

}
    }    
    
asked by user66800 19.11.2017 в 13:13
source

1 answer

0

I use regular expressions to make the TextField match, so in addition to the size I can check the type I want to allow to be introduced

Pattern.compile(Type.NUMERIC.equals(type) ? String.format("-?([0-9]{0,%d})?", length) : String.format(".{0,%d}", length)).matcher("");
    
answered by 28.11.2017 в 15:57