我正在使用焦点事件监听器为所有 JTextComponent(JTextField、JTextArea ...)实现占位符解决方案,我的实现如下
public class PlaceHolderDecorator {
public static void decorate(JTextField field, String placeHoldingText) {
field.setForeground(Color.GRAY);
field.addFocusListener(new FocusListener() {
@Override
public void focusGained(FocusEvent e) {
if (field.getText().equals(placeHoldingText)) {
field.setText("");
field.setForeground(Color.BLACK);
}
}
@Override
public void focusLost(FocusEvent e) {
if (field.getText().isEmpty()) {
field.setForeground(Color.GRAY);
field.setText(placeHoldingText);
}
}
});
}
public static void decorate(JTextField field, String placeHoldingText,Color placeHolderColor,Color textColor) {
field.setForeground(placeHolderColor);
field.addFocusListener(new FocusListener() {
@Override
public void focusGained(FocusEvent e) {
if (field.getText().equals(placeHoldingText)) {
field.setText("");
field.setForeground(textColor);
}
}
@Override
public void focusLost(FocusEvent e) {
if (field.getText().isEmpty()) {
field.setForeground(placeHolderColor);
field.setText(placeHoldingText);
}
}
});
}
final public static int DATE_FIELD_P = 10;
final public static int PHONE_FIELD_P = 14;
public static void decorate(JFormattedTextField field,String placeHoldingText,Color placeHolderColor,Color textColor,int checkFieldProperty){
field.setForeground(placeHolderColor);
final JFormattedTextField.AbstractFormatterFactory formatterFactory = field.getFormatterFactory();
field.setFormatterFactory(null);
field.setText(placeHoldingText);
field.addFocusListener(new FocusListener() {
@Override
public void focusGained(FocusEvent e) {
if (field.getText().equals(placeHoldingText)) {
field.setText("");
field.setForeground(textColor);
field.setFormatterFactory(formatterFactory);
}
}
@Override
public void focusLost(FocusEvent e) {
if (field.getText().trim().length()!=checkFieldProperty ) {
field.setFormatterFactory(null);
field.setText("");
}
if (field.getText().isEmpty()) {
field.setFormatterFactory(null);
field.setForeground(placeHolderColor);
field.setText(placeHoldingText);
}
}
});
}
}
要使用我的占位符工具,我执行以下操作
final Color writingColor = new Color(45, 45, 45);
final Color holdingColor = new Color(127, 127, 127);
PlaceHolderDecorator.decorate(jFTFDateDepot, "Date dépot du dossier", holdingColor, writingColor, PlaceHolderDecorator.DATE_FIELD_P);
PlaceHolderDecorator.decorate(jtfNom, "Nom", holdingColor, writingColor);
这样,当焦点发生时,将显示占位符,之后我需要为我的输入创建一个验证机制,所以我想到了像这样检查我的字段:
public class RequiredFieldValidator {
final private List<JTextComponent> components;
private ErrorDialog dialog;
public RequiredFieldValidator() {
components = new ArrayList<>();
}
public void add(JTextComponent jTextComponent) {
components.add(jTextComponent);
}
public boolean validate() {
for (final JTextComponent component : components) {
String placeHolder = component.getText();
System.out.println("placeholder : " + placeHolder);
component.requestFocus();
if (component.getText().trim().isEmpty()) {
System.out.println("validation started");
dialog = new ErrorDialog(null, true);
dialog.showErrorDialog("Veuillez remplir le champs " + placeHolder + " obligatoir");
component.requestFocus();
return false;
}
}
return true;
}
}
正如您在我的代码中看到的那样,为了摆脱保存文本的位置,我确实请求焦点,如果用户没有放置任何文本,则文本将为空,否则用户的输入将保留
我确实像这样使用这个字段 validator
我在 init 部分注册了我的必填字段
fieldValidator = new RequiredFieldValidator();
fieldValidator.add(jFTFDateDepot);
fieldValidator.add(jFTFDateNaissance);
fieldValidator.add(jFTFNumTel);
fieldValidator.add(jtfLieuNaissance);
fieldValidator.add(jtfNom);
fieldValidator.add(jtfPrenom);
然后我在创建事件上触发验证过程。
我在这段代码中的问题是,在运行之前我期待创建事件上有这个LIKE序列图
Getting current component >> requestFocus() >> fire place holding event >> do processing on the text of the component >> return to validation >> get the processed text >> if empty show alert >> if not loop to next component
但我惊讶地发现当前组件上的请求焦点监听器稍后在 swing 线程调度程序上触发
请如何确保该线程安全,我需要在获取验证文本之前触发焦点监听器。
最佳答案
您不需要请求焦点在组件上来检查组件是否有文本。当您循环遍历所有文本字段进行验证时,当焦点不断移动到最后一个文本字段时,用户不会欣赏它。
因此,使用您当前的方法,您真正需要的是一种方法来查看文本字段中的文本是否是“占位符”值。因此,也许您可以将“占位符”逻辑与验证逻辑结合起来,以便两种方法都可以访问占位符值。
另一种方法是不将文本字段的文本设置为占位符值。然后验证逻辑可以只检查文本字段是否为空。对于使用此方法的解决方案,请查看 Text Prompt 。它显示提示而不设置文本字段的文本。
关于java - 如何设置我的请求焦点事件监听器线程安全?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45804774/