无限的焦点循环在textFields上 [英] Infinite focus loop on textFields

查看:177
本文介绍了无限的焦点循环在textFields上的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有2个JTextFields:

  JTextField txtJobType,txtPriorityCode; 

这是我需要的功能:

当用户在txtJobType的管理中键入并点击选项卡(或点击)时,将进行错误检查以查看该字段是否为空或者输入的文本是否存在于数据库中。我这样做的方式是:

pre $私有空txtJobTypeFocusLost(java.awt.event.FocusEvent evt)$
System.out.println(JobType焦点丢失);
if(!checkFieldExists(txtJobType.getText(),jobType,jobCode,
JobType.class)|| txtJobType.getText()。isEmpty()){
txtJobType。 requestFocusInWindow();
txtJobType.selectAll();
} else {
}
}

不存在或文本是空的,然后返回焦点到txtJobType并突出显示所有文本(如果有的话)

这没问题。不过,我有txtPriorityCode字段需要有完全相同的行为。所以我做了:

pre prerivate $ txtPriorityCodeFocusLost(java.awt.event.FocusEvent evt){
System.out。 println(PriorityCode焦点丢失);
if(!checkFieldExists(txtPriorityCode.getText(),priority,priorityCode,
Priority.class)|| txtPriorityCode.getText()。isEmpty()){
txtPriorityCode。 requestFocusInWindow();
txtPriorityCode.selectAll();






这是问题开始的地方:如果用户离开jobType并选择优先级,然后代码尝试返回焦点回jobtype,但由于优先级也是空白在那一点上,它会试图从焦作回jobtype,导致这个输出:

  PriorityCode焦点丢失
JobType焦点丢失
PriorityCode焦点丢失
JobType焦点丢失

对于如何实现这种行为的任何帮助,我都赞赏,因为我必须为至少10个其他文本字段做这个。



谢谢!

解决方案

您不应该在丢失焦点或其他低级构造。相反,为什么不按照这个示例这个也是



例如,它可能看起来像这样:

  import javax.swing。*; 

public class InputVerifierEg {
private JPanel mainPanel = new JPanel();
private JTextField txtJobType = new JTextField(10);
private JTextField txtPriorityCode = new JTextField(10);
$ b $ public InputVerifierEg(){
txtJobType.setInputVerifier(new MyInputVerifier(jobType,jobCode,
JobType.class));
txtPriorityCode.setInputVerifier(new MyInputVerifier(priority,priorityCode,
Priority.class));

mainPanel.add(new JLabel(Job Type:));
mainPanel.add(txtJobType);
mainPanel.add(Box.createHorizo​​ntalStrut(15));
mainPanel.add(new JLabel(Priority Code:));
mainPanel.add(txtPriorityCode);


$ b public JPanel getMainPanel(){
return mainPanel;


private static void createAndShowGui(){
JFrame frame = new JFrame(InputVerifierEg);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane()。add(new InputVerifierEg()。getMainPanel());
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);


public static void main(String [] args){
SwingUtilities.invokeLater(new Runnable(){
public void run(){
createAndShowGui();
}
});
}
}

类MyInputVerifier扩展了InputVerifier {
private String fieldName;
private String codeName;
private Class<?> classType所;

public MyInputVerifier(String fieldName,String codeName,Class<?> classType){
this.fieldName = fieldName;
this.codeName = codeName;
this.classType = classType; (JTextField)输入;
}

@Override
public boolean verify(JComponent input){
JTextField tField =

//假设checkFieldExists是一个工具类的静态方法
if(!FieldCheckerUtil.checkFieldExists(tField.getText(),fieldName,
codeName,classType)) {
返回false; (tField.getText()。trim()。isEmpty()){
return false;


if
}
返回true;

$ b @Override
public boolean shouldYieldFocus(JComponent input){
JTextField tField =(JTextField)input;

if(verify(input)){
return true;
} else {
tField.selectAll();
//显示JOptionPane错误信息?
返回false;
}
}
}


I have 2 JTextFields:

JTextField txtJobType, txtPriorityCode;

This is the functionality I need:

When user types in 'administration' in txtJobType and hits tab (or clicks away) an error check is done to see whether the field is empty or if the text entered exists in database. The way I have done that is:

private void txtJobTypeFocusLost(java.awt.event.FocusEvent evt) {                                     
    System.out.println("JobType Focus Lost");
    if (!checkFieldExists(txtJobType.getText(), "jobType", "jobCode",
            JobType.class) || txtJobType.getText().isEmpty()) {
        txtJobType.requestFocusInWindow();
        txtJobType.selectAll();
    } else {
    }
} 

So if the field doesn't exist or the text is empty, then return focus to the txtJobType and highlight all the text (if any)

That works without a problem. However, I have the txtPriorityCode field which needs to have the exact same behaviour. So I did:

private void txtPriorityCodeFocusLost(java.awt.event.FocusEvent evt) {                                          
    System.out.println("PriorityCode Focus Lost");
    if (!checkFieldExists(txtPriorityCode.getText(), "priority", "priorityCode",
            Priority.class) || txtPriorityCode.getText().isEmpty()) {
        txtPriorityCode.requestFocusInWindow();
        txtPriorityCode.selectAll();
    }
}

This is where the problem starts: if user leaves jobType and tabs to Priority then the code attempts to return focus back to jobtype, but because priority is also blank at that point, it will attempt to get focus back from jobtype, resulting in this output:

PriorityCode Focus Lost
JobType Focus Lost
PriorityCode Focus Lost
JobType Focus Lost

Any help on how I could implement this behaviour is appreciated, as I have to do this for at least 10 other textfields.

Thank You!

解决方案

You shouldn't be fiddling with focus lost or other low-level constructs. Instead, why not simply use an InputVerifier as per this example and this one too?

For example, it could look something like this:

import javax.swing.*;

public class InputVerifierEg {
   private JPanel mainPanel = new JPanel();
   private JTextField txtJobType = new JTextField(10);
   private JTextField txtPriorityCode = new JTextField(10);

   public InputVerifierEg() {
      txtJobType.setInputVerifier(new MyInputVerifier("jobType", "jobCode",
            JobType.class));
      txtPriorityCode.setInputVerifier(new MyInputVerifier("priority", "priorityCode",
            Priority.class));

      mainPanel.add(new JLabel("Job Type:"));
      mainPanel.add(txtJobType);
      mainPanel.add(Box.createHorizontalStrut(15));
      mainPanel.add(new JLabel("Priority Code:"));
      mainPanel.add(txtPriorityCode);

   }

   public JPanel getMainPanel() {
      return mainPanel;
   }

   private static void createAndShowGui() {
      JFrame frame = new JFrame("InputVerifierEg");
      frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      frame.getContentPane().add(new InputVerifierEg().getMainPanel());
      frame.pack();
      frame.setLocationRelativeTo(null);
      frame.setVisible(true);
   }

   public static void main(String[] args) {
      SwingUtilities.invokeLater(new Runnable() {
         public void run() {
            createAndShowGui();
         }
      });
   }
}

class MyInputVerifier extends InputVerifier {
   private String fieldName;
   private String codeName;
   private Class<?> classType;

   public MyInputVerifier(String fieldName, String codeName, Class<?> classType) {
      this.fieldName = fieldName;
      this.codeName = codeName;
      this.classType = classType;
   }

   @Override
   public boolean verify(JComponent input) {
      JTextField tField = (JTextField) input;

      // assuming that the checkFieldExists is a static method of a utility class
      if (!FieldCheckerUtil.checkFieldExists(tField.getText(), fieldName,
            codeName, classType)) {
         return false;
      }

      if (tField.getText().trim().isEmpty()) {
         return false;
      }
      return true;
   }

   @Override
   public boolean shouldYieldFocus(JComponent input) {
      JTextField tField = (JTextField) input;

      if (verify(input)) {
         return true;
      } else {
         tField.selectAll();
         // show JOptionPane error message?
         return false;
      }
   }
}

这篇关于无限的焦点循环在textFields上的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆