下面列出了javax.swing.JFormattedTextField#setFocusLostBehavior ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
public IntegerEditor(int min, int max) {
super(new JFormattedTextField());
ftf = (JFormattedTextField) getComponent();
minimum = new Integer(min);
maximum = new Integer(max);
// Set up the editor for the integer cells.
integerFormat = NumberFormat.getIntegerInstance();
NumberFormatter intFormatter = new NumberFormatter(integerFormat);
intFormatter.setFormat(integerFormat);
intFormatter.setMinimum(minimum);
intFormatter.setMaximum(maximum);
ftf.setFormatterFactory(new DefaultFormatterFactory(intFormatter));
ftf.setValue(minimum);
ftf.setHorizontalAlignment(JTextField.TRAILING);
ftf.setFocusLostBehavior(JFormattedTextField.PERSIST);
// React when the user presses Enter while the editor is
// active. (Tab is handled as specified by
// JFormattedTextField's focusLostBehavior property.)
ftf.getInputMap().put(KeyStroke.getKeyStroke(KeyEvent.VK_ENTER, 0), "check");
ftf.getActionMap().put("check", new AbstractAction() {
public void actionPerformed(ActionEvent e) {
if (!ftf.isEditValid()) { // The text is invalid.
if (userSaysRevert()) { // reverted
ftf.postActionEvent(); // inform the editor
}
} else
try { // The text is valid,
ftf.commitEdit(); // so use it.
ftf.postActionEvent(); // stop editing
} catch (java.text.ParseException exc) {
}
}
});
}
public IntegerEditor(int min, int max)
{
super(new JFormattedTextField());
ftf = (JFormattedTextField) getComponent();
minimum = min;
maximum = max;
//Set up the editor for the integer cells.
integerFormat = NumberFormat.getIntegerInstance();
NumberFormatter intFormatter = new NumberFormatter(integerFormat);
intFormatter.setFormat(integerFormat);
intFormatter.setMinimum(minimum);
intFormatter.setMaximum(maximum);
ftf.setFormatterFactory(new DefaultFormatterFactory(intFormatter));
ftf.setValue(minimum);
ftf.setHorizontalAlignment(SwingConstants.TRAILING);
ftf.setFocusLostBehavior(JFormattedTextField.PERSIST);
//React when the user presses Enter while the editor is
//active. (Tab is handled as specified by
//JFormattedTextField's focusLostBehavior property.)
ftf.getInputMap().put(KeyStroke.getKeyStroke(KeyEvent.VK_ENTER, 0), "check");
ftf.getActionMap().put("check", new AbstractAction()
{
@Override
public void actionPerformed(ActionEvent e)
{
if (!ftf.isEditValid())
{ //The text is invalid.
if (userSaysRevert())
{ //reverted
ftf.postActionEvent(); //inform the editor
}
}
else
{
try
{ //The text is valid,
ftf.commitEdit(); //so use it.
ftf.postActionEvent(); //stop editing
}
catch (java.text.ParseException exc)
{
}
}
}
});
}
private void initComponents()
{
setLayout(new GridBagLayout());
layerUI = new FieldValidationLayerUI(getFrame());
groupLabel = new JLabel(getMember().toString());
groupLabel.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
groupLabel.addMouseListener(new LinkMouseAdapter(this));
groupLabel.setToolTipText("Double-click to look-up in FIXwiki");
if (isRequired)
{
groupLabel.setForeground(Color.BLUE);
}
JPanel groupValuePanel = new JPanel();
groupValuePanel.setLayout(new BoxLayout(groupValuePanel,
BoxLayout.X_AXIS));
groupTextField = new JFormattedTextField(
NumberFormat.getIntegerInstance());
groupTextField.setFocusLostBehavior(JFormattedTextField.COMMIT);
if (initialNoOfGroups > 0)
{
groupTextField.setText(String.valueOf(initialNoOfGroups));
}
setButton = new JButton("Set");
setButton.addActionListener(new ActionListener()
{
@Override
public void actionPerformed(ActionEvent e)
{
getFrame().displayMainPanel();
}
});
groupValuePanel.add(new JLayer<JFormattedTextField>(groupTextField,
layerUI));
groupValuePanel.add(setButton);
groupPanels = new JPanel();
groupPanels.setLayout(new GridBagLayout());
loadMembers();
add(groupLabel, createGridBagConstraints());
add(groupValuePanel, createGridBagConstraints());
add(groupPanels, createGridBagConstraints());
}
public IntegerEditor(int min, int max)
{
super(new JFormattedTextField());
ftf = (JFormattedTextField) getComponent();
minimum = min;
maximum = max;
//Set up the editor for the integer cells.
integerFormat = NumberFormat.getIntegerInstance();
NumberFormatter intFormatter = new NumberFormatter(integerFormat);
intFormatter.setFormat(integerFormat);
intFormatter.setMinimum(minimum);
intFormatter.setMaximum(maximum);
ftf.setFormatterFactory(new DefaultFormatterFactory(intFormatter));
ftf.setValue(minimum);
ftf.setHorizontalAlignment(SwingConstants.TRAILING);
ftf.setFocusLostBehavior(JFormattedTextField.PERSIST);
//React when the user presses Enter while the editor is
//active. (Tab is handled as specified by
//JFormattedTextField's focusLostBehavior property.)
ftf.getInputMap().put(KeyStroke.getKeyStroke(KeyEvent.VK_ENTER, 0), "check");
ftf.getActionMap().put("check", new AbstractAction()
{
@Override
public void actionPerformed(ActionEvent e)
{
if (!ftf.isEditValid())
{ //The text is invalid.
if (userSaysRevert())
{ //reverted
ftf.postActionEvent(); //inform the editor
}
}
else
{
try
{ //The text is valid,
ftf.commitEdit(); //so use it.
ftf.postActionEvent(); //stop editing
}
catch (java.text.ParseException exc)
{
}
}
}
});
}
protected void installComponents() {
field = new JFormattedTextField(createFormatterFactory());
field.setValue(peer.getDate());
field.setFocusLostBehavior(peer.getFocusLostBehavior());
field.setEditable(peer.isFieldEditable());
field.setToolTipText(peer.getToolTipText());
// button
button = new JButton();
button.setFocusable(false);
button.setMargin(new Insets(0, 0, 0, 0));
button.setToolTipText(peer.getToolTipText());
setSimpeLook(false);
// calendar
calendarPane = new CalendarPane(peer.getStyle());
calendarPane.setShowTodayButton(peer.isShowTodayButton());
calendarPane.setFocusLostBehavior(JFormattedTextField.REVERT);
calendarPane.setFocusCycleRoot(true);
calendarPane.setBorder(BorderFactory.createEmptyBorder(1, 3, 0, 3));
calendarPane.setStripTime(false);
calendarPane.setLocale(peer.getLocale());
calendarPane.setZone(peer.getZone());
calendarPane.setFocusable(peer.isDropdownFocusable());
calendarPane.setColorOverrideMap(peer.getColorOverrideMap());
// popup
popup = new JPopupMenu();
popup.setLayout(new BorderLayout());
popup.add(calendarPane, BorderLayout.CENTER);
popup.setLightWeightPopupEnabled(true);
// add
peer.setLayout(new BorderLayout());
switch (peer.getPickerStyle()) {
case DatePicker.PICKER_STYLE_FIELD_AND_BUTTON:
peer.add(field, BorderLayout.CENTER);
peer.add(button, BorderLayout.EAST);
break;
case DatePicker.PICKER_STYLE_BUTTON:
peer.add(button, BorderLayout.EAST);
break;
}
peer.revalidate();
peer.repaint();
componentListener = new ComponentListener();
button.addActionListener(componentListener);
field.addPropertyChangeListener(componentListener);
calendarPane.addPropertyChangeListener(componentListener);
calendarPane.addCommitListener(componentListener);
calendarPane.addActionListener(componentListener);
peerDateChanged(peer.getDate());
}