2012-02-01 26 views
1

当(表单)包含JFormattedTextField时,我遇到了使用表单默认按钮的问题。 在具有此类字段的表单上,如果它恰好有焦点(并且已被更改),则必须按OK 两次以获得默认按钮“已按下”。我想我知道它为什么会发生 - 这是因为第一次输入在提交处理中消耗。如何使JFormattedTextField在默认按钮的窗体上正常运行?

我也能够做一个解决方法 - 如果你改变Formatter在每一个有效的编辑提交,那么你会得到正确的行为,但这a)强制你指定格式化程序explicilty,和b)不可能恢复到'旧'值(例如使用Escape或编程方式)。下面

代码演示了:当你在上面运行现场提交每个编辑和单作品则(但不能恢复),在底部字段允许将恢复,但需要进入,如果编辑。

import java.awt.BorderLayout; 
import java.awt.EventQueue; 
import java.util.Date; 

import javax.swing.JButton; 
import javax.swing.JFormattedTextField; 
import javax.swing.JFrame; 
import javax.swing.JPanel; 
import javax.swing.border.EmptyBorder; 
import javax.swing.text.DateFormatter; 

public class ExFrame extends JFrame { 

    private JPanel contentPane; 

    /** 
    * Launch the application. 
    */ 
    public static void main(String[] args) { 
     EventQueue.invokeLater(new Runnable() { 
      public void run() { 
       try { 
        ExFrame frame = new ExFrame(); 
        frame.setVisible(true); 
       } catch (Exception e) { 
        e.printStackTrace(); 
       } 
      } 
     }); 
    } 
    /** 
    * Create the frame. 
    */ 
    public ExFrame() { 
     setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
     setBounds(100, 100, 450, 300); 
     contentPane = new JPanel(); 
     contentPane.setBorder(new EmptyBorder(5, 5, 5, 5)); 
     contentPane.setLayout(new BorderLayout(0, 0)); 
     setContentPane(contentPane); 

     JFormattedTextField ff_1, ff_2; 

     //ff_1 has modified behavior of commit on each (valid) edit 
     DateFormatter f=new DateFormatter(); 
     f.setCommitsOnValidEdit(true); 

     ff_1 = new JFormattedTextField(f); 
     ff_1.setValue(new Date()); 

     //ff_2 has default behavior 
     ff_2 = new JFormattedTextField(new Date()); 

     contentPane.add(ff_1, BorderLayout.NORTH); 
     contentPane.add(ff_2, BorderLayout.SOUTH); 

     JButton btnDefault = new JButton("I am default button"); 
     contentPane.add(btnDefault, BorderLayout.CENTER); 
     getRootPane().setDefaultButton(btnDefault); 
    } 
} 

所以现在的问题是:有没有办法让JFormattedTextField提交上输入(所以输入验证,但只有一次),如果成功地验证,激活默认按钮(单按)?

回答

2

的基本技巧是傻瓜键绑定机制,以为击键不是由文本字段处理。为此,我们需要继承JFormattedTextField并覆盖它的processKeyBindings以返回false(意思是:“无法使用”)。

喜欢的东西:

JFormattedTextField multiplex = new JFormattedTextField() { 
    KeyStroke enter = KeyStroke.getKeyStroke("ENTER"); 
    @Override 
    protected boolean processKeyBinding(KeyStroke ks, KeyEvent e, 
             int condition, boolean pressed) { 
     boolean processed = super.processKeyBinding(ks, e, condition, 
                pressed); 

     if (processed && condition != JComponent.WHEN_IN_FOCUSED_WINDOW 
       && enter.equals(ks)) { 
      // Returning false will allow further processing 
      // of the bindings, eg our parent Containers will get a 
      // crack at them and f.i. route to a default button. 
      return !isEditValid(); 
     } 
     return processed; 
    } 

}; 
multiplex.setValue(new Date()); 
+0

感谢挖掘出来! (显然,工作答案) – wmz 2012-08-15 11:32:18

1

的日期/数字实例(在大多数情况下)更好地利用JSpinner而非JFormattedTextField,那么你只能为DateSpinnerModelLocaleSimpleDateFormat设置,

JSpinnerNumber实例(有效期为JTextField )您必须添加Document for removing/filtering for unwanted chars

+0

你是对的,但是这并没有回答我的问题... – wmz 2012-02-01 19:29:57

+0

@wmz我想给你一个答案,而不会打扰... – mKorbel 2012-02-01 19:32:06

+0

赞赏,thx – wmz 2012-02-01 19:40:01

0

Enter通常用于接受当前值,因此输入JFTF中的 也可以。 您可以通过禁用它:

ff_1.getInputMap().put(KeyStroke.getKeyStroke("ENTER"), new Object()); 
ff_2.getInputMap().put(KeyStroke.getKeyStroke("ENTER"), new Object()); 

再见,冷静;)

+1

谢谢你试图有帮助:-)不幸的是,你的建议并没有解决问题... – kleopatra 2012-08-15 09:58:50

0

无论你的答案来解决我的应用程序的问题。然而,它似乎是这个问题的解决办法是在我的情况要简单得多:

private void jTextField1ActionPerformed(java.awt.event.ActionEvent evt) { 
    add(jTextField1);   //this reacts after the "ENTER" gets pressed 
    jButton1.doClick();  //this activates the button with Enter 
    jTextField1.setText(""); //this removes the text from a text-field 
    jTextField1.grabFocus(); //this sets a cursor within a text-field 

//Whenever the Enter is pressed, after the typed text, the action is performed again. 
    } 
相关问题