我正在寻找解决方案如何通过模板限制文本字段中的输入。例如,我有一些文本字段,用户只需输入日期(dd/MM/yyyy)或时间(mm:hh)。我该怎么做呢?

我正在使用JavaFx 8。
发布于 2015-06-06 21:15:30
这是我在DatePicker的TextField对象上使用的方法:允许在文本字段中插入任何类型的文本的DatePicker对象。我认为这个解决方案也能让你止步不前。在本例中,我在DatePicker和TextField对象上执行了此操作。
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("");
}
}
};
});
}你可以使用object SimpleDateFormat来检查一个合适的时间,再见http://docs.oracle.com/javase/7/docs/api/java/text/SimpleDateFormat.html
发布于 2015-10-22 01:40:25
这个答案很容易理解,并适用于任何文本模式。实现的模式是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);
}
}https://stackoverflow.com/questions/28398010
复制相似问题