我正在寻找解决方案如何通过模板在文本字段中输入restrick。例如,我有文本字段,用户必须只输入日期(dd / MM / yyyy)或时间(mm:hh)。猫我是怎么做到的? !1
我正在使用JavaFx 8。
答案 0 :(得分:1)
这个答案很容易理解并适应任何文本模式。实施的模式是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);
}
}
答案 1 :(得分:0)
这是我在DatePicker的TextField对象上使用的solition:DatePicker对象允许在textfield中插入任何类型的文本。 我认为这个解决方案也能阻止你。 在示例中,我在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("");
}
}
};
});
}
您可以使用SimpleDateFormat对象来检查适当的时间http://docs.oracle.com/javase/7/docs/api/java/text/SimpleDateFormat.html 再见