Java Code Examples for javax.swing.JFormattedTextField#setFocusLostBehavior()

The following examples show how to use javax.swing.JFormattedTextField#setFocusLostBehavior() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: IntegerEditor.java    From marathonv5 with Apache License 2.0 5 votes vote down vote up
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) {
                }
        }
    });
}
 
Example 2
Source File: IntegerEditor.java    From pcgen with GNU Lesser General Public License v2.1 4 votes vote down vote up
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)
				{
				}
			}
		}

	});
}
 
Example 3
Source File: GroupPanel.java    From quickfix-messenger with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
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());
}
 
Example 4
Source File: IntegerEditor.java    From pcgen with GNU Lesser General Public License v2.1 4 votes vote down vote up
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)
				{
				}
			}
		}

	});
}
 
Example 5
Source File: BasicDatePickerUI.java    From microba with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
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());
}