0

I'm looking for solution how to restrick input in textfield by template. For example I have textfields where user have to input only date (dd/MM/yyyy) or time (mm:hh). How cat I do that? !1

I'm working with JavaFx 8.

user3352926
  • 1,017
  • 1
  • 10
  • 15
  • Check out the following oracle page [DatePicker][1] [1]: http://stackoverflow.com/questions/22940371/javafx-8-datepicker-features – Inge Feb 09 '15 at 05:29
  • 1
    If you can use JDK 8 update 40 (which is in early release at the time of writing, but is due to be in GA in about a month), it has a [`TextFormatter`](http://download.java.net/jdk9/jfxdocs/index.html?javafx/scene/control/TextFormatter.html) that is designed for exactly this use case. – James_D Feb 09 '15 at 12:22

2 Answers2

0

this is the solition I used on the TextField object of my DatePicker: the DatePicker object allowed to insert any kind of text in the textfield.. I think this solution can halp you as well. In the example I did this on a DatePicker and on a TextField object.

MyControllerClass.java

@Fxml
private DatePicker datePik;
@Fxml
private TextField textF;

@Override
    public void initialize(URL url, ResourceBundle rb) {  
        String pattern = "dd-MM-yyyy";
        datePik.setPromptText(pattern.toLowerCase());      
        datePik.getEditor().focusedProperty().addListener(new ChangeListener<Boolean>()//focus on the TextField object of the DatePicker
            {   
                @Override
                public void changed(ObservableValue<? extends Boolean> arg0, Boolean oldPropertyValue, Boolean newPropertyValue){
                    if (newPropertyValue == false){
                        try {
                            SimpleDateFormat sdf = new SimpleDateFormat(datePik.getEditor().getText());
                            sdf.setLenient(false);
                            //if not valid, it will throw ParseException
                            Date date = sdf.parse(datePik.getEditor().getText());
                            //System.out.println(date);
                        } catch (Exception e) {
                            e.printStackTrace();
                            datePik.getEditor().setText("");
                        }
                            }
                        };
            }); 


        textF.focusedProperty().addListener(new ChangeListener<Boolean>()//focus on the TextField object
            {   
                @Override
                public void changed(ObservableValue<? extends Boolean> arg0, Boolean oldPropertyValue, Boolean newPropertyValue){
                    if (newPropertyValue == false){
                        try {
                            SimpleDateFormat sdf = new SimpleDateFormat(textF.getText());
                            sdf.setLenient(false);
                            //if not valid, it will throw ParseException
                            Date date = sdf.parse(textF.getText());
                            //System.out.println(date);
                        } catch (Exception e) {
                            e.printStackTrace();
                            textF.setText("");
                        }
                            }
                        };
            }); 
}

You can use the object SimpleDateFormat in order to check a proper time too http://docs.oracle.com/javase/7/docs/api/java/text/SimpleDateFormat.html bye

Matteo
  • 183
  • 1
  • 10
0

This answer is easy to understand and adapt to any Text pattern. The pattern implemented is dd.mm.yyyy

import java.awt.Toolkit;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import javafx.geometry.Side;
import javafx.scene.control.ContextMenu;
import javafx.scene.control.MenuItem;
import javafx.scene.control.TextField;

public class DateTextField extends TextField
{
Pattern patt1 = Pattern.compile("[0-3]");
Pattern patt2 = Pattern.compile("[0-3][0-9]");
Pattern patt3 = Pattern.compile("[0-3][0-9][.]");
Pattern patt4 = Pattern.compile("[0-3][0-9][.][0-1]");
Pattern patt5 = Pattern.compile("[0-3][0-9][.][0-1][0-9]");
Pattern patt6 = Pattern.compile("[0-3][0-9][.][0-1][0-9][.]");
Pattern patt7 = Pattern.compile("[0-3][0-9][.][0-1][0-9][.][1-2]");
Pattern patt8 = Pattern.compile("[0-3][0-9][.][0-1][0-9][.][1-2][0-9]");
Pattern patt9 = Pattern.compile("[0-3][0-9][.][0-1][0-9][.][1-2][0-9][0-9]");
Pattern patt10 = Pattern.compile("[0-3][0-9][.][0-1][0-9][.][1-2][0-9][0-9][0-9]");

public DateTextField()
{
    super();
}

public void replaceText(int start, int end, String text)
{       
    String text2 = this.getText()+text;
    if( compare(text2) || start != end)
    {
        super.replaceText( start, end, text );
    }
    else
    {
        Toolkit.getDefaultToolkit().beep();
        zeige();
    }

}

public void replaceSelection(String text)
{
    String text2 = this.getText()+text;
    if(compare(text2))
    {
        super.replaceSelection( text );
    }
    else
    {
        Toolkit.getDefaultToolkit().beep();
        zeige();
    }
}

private boolean compare(String text)
{
    Matcher match = patt1.matcher(text);
    if(match.matches()) return true;
    match = patt2.matcher(text);
    if(match.matches()) return true;
    match = patt3.matcher(text);
    if(match.matches()) return true;
    match = patt4.matcher(text);
    if(match.matches()) return true;
    match = patt5.matcher(text);
    if(match.matches()) return true;
    match = patt6.matcher(text);
    if(match.matches()) return true;
    match = patt7.matcher(text);
    if(match.matches()) return true;
    match = patt8.matcher(text);
    if(match.matches()) return true;
    match = patt9.matcher(text);
    if(match.matches()) return true;
    match = patt10.matcher(text);
    if(match.matches()) return true;
    return false;
}

private void zeige()
{
    final ContextMenu menu = new ContextMenu();
    menu.getItems().add(new MenuItem("dd.mm.yyyy"));
    menu.show(this, Side.BOTTOM, 0, 0);
}
}