我有一个jFormattedTextField
,我设置setCommitsOnValidEdit
true那么我添加了一个事件监听器上的“价值”属性“属性更改”。
在那第一焦点jFormattedTextField
它打字的时候它不调用事件侦听器的方法。 但在“focusLost”它调用事件侦听器后,当它再次接收集中在打字的时候调用事件侦听器。
我想事件监听器被调用,在任何时候任何更改后jFormattedTextField
(即使在拳头焦点)。
有什么问题? 我怎样才能解决这个问题?
probably you have to look at DocumentListener example here
EDIT:
I know this issue from my 1st touch of JFormattedTextField
, here comings example that isn't works at firts focusLost
:-) and probably demonstrated your issue
minimum limit is there set at 10.000,-
for both JFormattedTextField
,
1st. JFormattedTextField
handling FocusListener
(output must be delayed into invokeLater
)
2nd. JFormattedTextField
handling DocumentListener
(works every...)
inital look
here is same problem, because I put here only 500,- and on focusLost nothing changed, correct amount must be >= 10.000,-
on 2dn. focusLost
works ....
no idea how is that possible, but solved by wrapping into invokeLater()
, then works on 1st. focusLost
(you have to uncomment these code lines)
from code
import java.awt.*;
import java.awt.event.*;
import java.math.RoundingMode;
import java.text.NumberFormat;
import javax.swing.*;
import javax.swing.border.*;
import javax.swing.event.*;
public class FormatterLimit {
private JFrame frame = new JFrame();
private JPanel pnl = new JPanel();
private JLabel focusLabel = new JLabel(" focusLost Handle ");
private JFormattedTextField formTextField;
private JLabel docLabel = new JLabel(" document Handle ");
private JFormattedTextField formTextField1;
private NumberFormat formTextFieldFormat;
private double amount = 10000.00;
public FormatterLimit() {
formTextFieldFormat = NumberFormat.getNumberInstance();
formTextFieldFormat.setMinimumFractionDigits(2);
formTextFieldFormat.setMaximumFractionDigits(2);
formTextFieldFormat.setRoundingMode(RoundingMode.HALF_UP);
focusLabel.setFont(new Font("Serif", Font.BOLD, 14));
focusLabel.setForeground(Color.blue);
focusLabel.setPreferredSize(new Dimension(120, 27));
formTextField = new JFormattedTextField(formTextFieldFormat);
formTextField.setValue(amount);
formTextField.setFont(new Font("Serif", Font.BOLD, 22));
formTextField.setForeground(Color.black);
formTextField.setBackground(Color.yellow);
formTextField.setPreferredSize(new Dimension(120, 27));
formTextField.setHorizontalAlignment(SwingConstants.RIGHT);
formTextField.addFocusListener(new FocusListener() {
@Override
public void focusGained(FocusEvent e) {
formTextField.requestFocus();
formTextField.setText(formTextField.getText());
formTextField.selectAll();
}
@Override
public void focusLost(FocusEvent e) {
//Runnable doRun = new Runnable() {
//@Override
//public void run() {
double t1a1 = (((Number) formTextField.getValue()).doubleValue());
if (t1a1 < 1000) {
formTextField.setValue(amount);
}
//}
// };
//SwingUtilities.invokeLater(doRun);
}
});
docLabel.setFont(new Font("Serif", Font.BOLD, 14));
docLabel.setForeground(Color.blue);
docLabel.setPreferredSize(new Dimension(120, 27));
formTextField1 = new JFormattedTextField(formTextFieldFormat);
formTextField1.setValue(amount);
formTextField1.setFont(new Font("Serif", Font.BOLD, 22));
formTextField1.setForeground(Color.black);
formTextField1.setBackground(Color.yellow);
formTextField1.setPreferredSize(new Dimension(120, 27));
formTextField1.setHorizontalAlignment(SwingConstants.RIGHT);
formTextField1.addFocusListener(new FocusListener() {
@Override
public void focusGained(FocusEvent e) {
formTextField1.requestFocus();
formTextField1.setText(formTextField1.getText());
formTextField1.selectAll();
}
@Override
public void focusLost(FocusEvent e) {
}
});
formTextField1.getDocument().addDocumentListener(docListener);
pnl = new JPanel();
pnl.setBorder(new EmptyBorder(2, 2, 2, 2));
pnl.setLayout(new GridLayout(2, 2));
pnl.add(focusLabel);
pnl.add(formTextField);
pnl.add(docLabel);
pnl.add(formTextField1);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(pnl, BorderLayout.CENTER);
frame.setLocation(200, 200);
frame.pack();
frame.setVisible(true);
formTextFieldFocus1();
}
//
private DocumentListener docListener = new DocumentListener() {
@Override
public void changedUpdate(DocumentEvent documentEvent) {
printIt(documentEvent);
}
@Override
public void insertUpdate(DocumentEvent documentEvent) {
printIt(documentEvent);
}
@Override
public void removeUpdate(DocumentEvent documentEvent) {
printIt(documentEvent);
}
private void printIt(DocumentEvent documentEvent) {
DocumentEvent.EventType type = documentEvent.getType();
double t1a1 = (((Number) formTextField1.getValue()).doubleValue());
if (t1a1 < 1000) {
Runnable doRun = new Runnable() {
@Override
public void run() {
formTextField1.setValue(amount);
}
};
SwingUtilities.invokeLater(doRun);
}
}
};
private void formTextFieldFocus1() {
Runnable doRun = new Runnable() {
@Override
public void run() {
formTextField1.grabFocus();
formTextField1.requestFocus();
formTextField1.setText(formTextField1.getText());
formTextField1.selectAll();
}
};
SwingUtilities.invokeLater(doRun);
}
private void formTextFieldFocus() {
Runnable doRun = new Runnable() {
@Override
public void run() {
formTextField.grabFocus();
formTextField.requestFocus();
formTextField.setText(formTextField.getText());
formTextField.selectAll();
formTextFieldFocus1();
}
};
SwingUtilities.invokeLater(doRun);
}
public static void main(String args[]) {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
FormatterLimit fl = new FormatterLimit();
}
});
}
}
其实,setCommitOnValidEdit应该总是工作像您期望的(并且确实在下面的代码片段),无需使用的DocumentListener,毕竟,该方法正是为了这个目的。 所以我怀疑其他的东西是错的在你的环境。 或者由于某种原因,第一个编辑不会被解析到任何有效?
NumberFormatter numberFormatter = new NumberFormatter(
NumberFormat.getIntegerInstance());
// allow or not doesn't make a difference
numberFormatter.setAllowsInvalid(false);
numberFormatter.setCommitsOnValidEdit(true);
JFormattedTextField readTimeOut = new JFormattedTextField(numberFormatter);
PropertyChangeListener l = new PropertyChangeListener() {
@Override
public void propertyChange(PropertyChangeEvent evt) {
LOG.info("got new value: " + evt.getNewValue());
}
};
readTimeOut.addPropertyChangeListener("value", l);
readTimeOut.setColumns(20);
readTimeOut.setHorizontalAlignment(SwingConstants.RIGHT);
JFormattedTextField other = new JFormattedTextField(numberFormatter);
other.addPropertyChangeListener("value", l);
other.setColumns(20);
other.setHorizontalAlignment(SwingConstants.RIGHT);
JPanel box = new JPanel();
box.add(readTimeOut);
box.add(other);
文章来源: jFormattedTextField's Formatter.setCommitsOnValidEdit(true) doesn't work at first focus