Java 如何使JFormattedTextField在带有默认按钮的窗体上正常运行?
当表单包含Java 如何使JFormattedTextField在带有默认按钮的窗体上正常运行?,java,swing,jformattedtextfield,Java,Swing,Jformattedtextfield,当表单包含JFormattedTextField时,我使用表单默认按钮遇到问题。 在有这样一个字段的表单上,如果它恰好有焦点(并且被更改),您必须按OK两次才能“按下”默认按钮。我想我知道为什么会发生这种情况——这是因为first Enter在提交处理中被消耗掉了 我还能够做一个变通方法-如果您更改格式化程序以提交每个有效编辑,那么您将获得正确的行为,但这a)强制您明确指定格式化程序,b)不可能恢复为“旧”值(例如使用转义或编程) 下面的代码演示了:当您运行顶部的it字段时,每次编辑都会提交,并
JFormattedTextField
时,我使用表单默认按钮遇到问题。
在有这样一个字段的表单上,如果它恰好有焦点(并且被更改),您必须按OK两次才能“按下”默认按钮。我想我知道为什么会发生这种情况——这是因为first Enter在提交处理中被消耗掉了
我还能够做一个变通方法-如果您更改格式化程序以提交每个有效编辑,那么您将获得正确的行为,但这a)强制您明确指定格式化程序,b)不可能恢复为“旧”值(例如使用转义或编程)
下面的代码演示了:当您运行顶部的it字段时,每次编辑都会提交,并使用单回车键(但无法还原),底部的字段允许还原,但如果编辑,则需要两个回车键
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
(因此输入经过验证,但只有一次)并且如果成功验证了,激活默认按钮(只需按一下)?对于日期/数字实例(在大多数情况下)更好地使用而不是,然后,您只能使用Locale
或SimpleDateFormat
设置
对于带有Number
实例的JSpinner
(对JTextField
有效),您必须添加Enter通常用于接受当前值,因此
进入JFTF也可以做到这一点。
您可以通过执行以下操作来禁用它:
ff_1.getInputMap().put(KeyStroke.getKeyStroke("ENTER"), new Object());
ff_2.getInputMap().put(KeyStroke.getKeyStroke("ENTER"), new Object());
再见,冷静点;) 最基本的技巧是愚弄键绑定机制,使其相信击键不是由textField处理的。为了实现这一点,我们需要对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());
你的回答都没有解决我的申请问题。然而,在我的案例中,问题解决方案似乎要简单得多:
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.
}
检查Otnhanks上@Walter Laan的这条连接线是否有线索。事实上,我在那里交叉张贴,如果有任何答案,我会报告。谢谢你的帮助:-)不幸的是,你的建议并不能解决问题。。。