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

The following examples show how to use javax.swing.JFormattedTextField#setFormatterFactory() . 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: ValueFormatter.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 7 votes vote down vote up
static void init(int length, boolean hex, JFormattedTextField text) {
    ValueFormatter formatter = new ValueFormatter(length, hex);
    text.setColumns(length);
    text.setFormatterFactory(new DefaultFormatterFactory(formatter));
    text.setHorizontalAlignment(SwingConstants.RIGHT);
    text.setMinimumSize(text.getPreferredSize());
    text.addFocusListener(formatter);
}
 
Example 2
Source File: SkillInfoTab.java    From pcgen with GNU Lesser General Public License v2.1 7 votes vote down vote up
private SkillRankSpinnerEditor(SkillRankSpinnerModel model)
{
	super(model);
	this.model = model;

	DefaultEditor editor = new DefaultEditor(spinner);
	NumberFormatter formatter = new NumberFormatter(new DecimalFormat("#0.#")); //$NON-NLS-1$
	formatter.setValueClass(Float.class);
	DefaultFormatterFactory factory = new DefaultFormatterFactory(formatter);

	JFormattedTextField ftf = editor.getTextField();
	ftf.setEditable(true);
	ftf.setFormatterFactory(factory);
	ftf.setHorizontalAlignment(SwingConstants.RIGHT);

	spinner.setEditor(editor);
}
 
Example 3
Source File: SkillInfoTab.java    From pcgen with GNU Lesser General Public License v2.1 6 votes vote down vote up
private SkillRankSpinnerEditor(SkillRankSpinnerModel model)
{
	super(model);
	this.model = model;

	DefaultEditor editor = new DefaultEditor(spinner);
	NumberFormatter formatter = new NumberFormatter(new DecimalFormat("#0.#")); //$NON-NLS-1$
	formatter.setValueClass(Float.class);
	DefaultFormatterFactory factory = new DefaultFormatterFactory(formatter);

	JFormattedTextField ftf = editor.getTextField();
	ftf.setEditable(true);
	ftf.setFormatterFactory(factory);
	ftf.setHorizontalAlignment(SwingConstants.RIGHT);

	spinner.setEditor(editor);
}
 
Example 4
Source File: SwingNullableSpinner.java    From atdl4j with MIT License 6 votes vote down vote up
private NumberEditorNull(JSpinner spinner, DecimalFormat format) {
	super(spinner);
	if (!(spinner.getModel() instanceof SpinnerNumberModelNull)) {
		return;
	}
	
	SpinnerNumberModelNull model = (SpinnerNumberModelNull) spinner.getModel();
	NumberFormatter formatter = new NumberEditorFormatterNull(model, format);
	DefaultFormatterFactory factory = new DefaultFormatterFactory(formatter);
	JFormattedTextField ftf = getTextField();
	ftf.setEditable(true);
	ftf.setFormatterFactory(factory);
	ftf.setHorizontalAlignment(JTextField.RIGHT);
	
	try {
		String maxString = formatter.valueToString(model.getMinimum());
		String minString = formatter.valueToString(model.getMaximum());
		ftf.setColumns(Math.max(maxString.length(), minString.length()));
	}
	catch (ParseException e) {
		// TBD should throw a chained error here
	}
}
 
Example 5
Source File: ValueFormatter.java    From dragonwell8_jdk with GNU General Public License v2.0 5 votes vote down vote up
static void init(int length, boolean hex, JFormattedTextField text) {
    ValueFormatter formatter = new ValueFormatter(length, hex);
    text.setColumns(length);
    text.setFormatterFactory(new DefaultFormatterFactory(formatter));
    text.setHorizontalAlignment(SwingConstants.RIGHT);
    text.setMinimumSize(text.getPreferredSize());
    text.addFocusListener(formatter);
}
 
Example 6
Source File: ValueFormatter.java    From jdk8u_jdk with GNU General Public License v2.0 5 votes vote down vote up
static void init(int length, boolean hex, JFormattedTextField text) {
    ValueFormatter formatter = new ValueFormatter(length, hex);
    text.setColumns(length);
    text.setFormatterFactory(new DefaultFormatterFactory(formatter));
    text.setHorizontalAlignment(SwingConstants.RIGHT);
    text.setMinimumSize(text.getPreferredSize());
    text.addFocusListener(formatter);
}
 
Example 7
Source File: ValueFormatter.java    From openjdk-8 with GNU General Public License v2.0 5 votes vote down vote up
static void init(int length, boolean hex, JFormattedTextField text) {
    ValueFormatter formatter = new ValueFormatter(length, hex);
    text.setColumns(length);
    text.setFormatterFactory(new DefaultFormatterFactory(formatter));
    text.setHorizontalAlignment(SwingConstants.RIGHT);
    text.setMinimumSize(text.getPreferredSize());
    text.addFocusListener(formatter);
}
 
Example 8
Source File: ValueFormatter.java    From jdk8u-jdk with GNU General Public License v2.0 5 votes vote down vote up
static void init(int length, boolean hex, JFormattedTextField text) {
    ValueFormatter formatter = new ValueFormatter(length, hex);
    text.setColumns(length);
    text.setFormatterFactory(new DefaultFormatterFactory(formatter));
    text.setHorizontalAlignment(SwingConstants.RIGHT);
    text.setMinimumSize(text.getPreferredSize());
    text.addFocusListener(formatter);
}
 
Example 9
Source File: NoGroupingSpinner.java    From microba with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
public NoGroupingNumberEditor(JSpinner spinner, SpinnerModel model) {
	super(spinner);
	JFormattedTextField ftf = (JFormattedTextField) this
			.getComponent(0);
	NumberFormat fmt = NumberFormat.getIntegerInstance();
	fmt.setGroupingUsed(false);
	ftf.setFormatterFactory(new DefaultFormatterFactory(
			new NumberFormatter(fmt)));
	revalidate();
}
 
Example 10
Source File: LHexaSpinner.java    From audiveris with GNU Affero General Public License v3.0 5 votes vote down vote up
HexaEditor (JSpinner spinner)
{
    super(spinner);

    JFormattedTextField ftf = getTextField();
    ftf.setEditable(true);
    ftf.setFormatterFactory(new HexaFormatterFactory());
}
 
Example 11
Source File: ValueFormatter.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
static void init(int length, boolean hex, JFormattedTextField text) {
    ValueFormatter formatter = new ValueFormatter(length, hex);
    text.setColumns(length);
    text.setFormatterFactory(new DefaultFormatterFactory(formatter));
    text.setHorizontalAlignment(SwingConstants.RIGHT);
    text.setMinimumSize(text.getPreferredSize());
    text.addFocusListener(formatter);
}
 
Example 12
Source File: ValueFormatter.java    From Bytecoder with Apache License 2.0 5 votes vote down vote up
static void init(int length, boolean hex, JFormattedTextField text) {
    ValueFormatter formatter = new ValueFormatter(length, hex);
    text.setColumns(length);
    text.setFormatterFactory(new DefaultFormatterFactory(formatter));
    text.setHorizontalAlignment(SwingConstants.RIGHT);
    text.setMinimumSize(text.getPreferredSize());
    text.addFocusListener(formatter);
}
 
Example 13
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 14
Source File: ValueFormatter.java    From openjdk-jdk8u with GNU General Public License v2.0 5 votes vote down vote up
static void init(int length, boolean hex, JFormattedTextField text) {
    ValueFormatter formatter = new ValueFormatter(length, hex);
    text.setColumns(length);
    text.setFormatterFactory(new DefaultFormatterFactory(formatter));
    text.setHorizontalAlignment(SwingConstants.RIGHT);
    text.setMinimumSize(text.getPreferredSize());
    text.addFocusListener(formatter);
}
 
Example 15
Source File: ValueFormatter.java    From jdk8u60 with GNU General Public License v2.0 5 votes vote down vote up
static void init(int length, boolean hex, JFormattedTextField text) {
    ValueFormatter formatter = new ValueFormatter(length, hex);
    text.setColumns(length);
    text.setFormatterFactory(new DefaultFormatterFactory(formatter));
    text.setHorizontalAlignment(SwingConstants.RIGHT);
    text.setMinimumSize(text.getPreferredSize());
    text.addFocusListener(formatter);
}
 
Example 16
Source File: ValueFormatter.java    From TencentKona-8 with GNU General Public License v2.0 5 votes vote down vote up
static void init(int length, boolean hex, JFormattedTextField text) {
    ValueFormatter formatter = new ValueFormatter(length, hex);
    text.setColumns(length);
    text.setFormatterFactory(new DefaultFormatterFactory(formatter));
    text.setHorizontalAlignment(SwingConstants.RIGHT);
    text.setMinimumSize(text.getPreferredSize());
    text.addFocusListener(formatter);
}
 
Example 17
Source File: ValueFormatter.java    From jdk1.8-source-analysis with Apache License 2.0 5 votes vote down vote up
static void init(int length, boolean hex, JFormattedTextField text) {
    ValueFormatter formatter = new ValueFormatter(length, hex);
    text.setColumns(length);
    text.setFormatterFactory(new DefaultFormatterFactory(formatter));
    text.setHorizontalAlignment(SwingConstants.RIGHT);
    text.setMinimumSize(text.getPreferredSize());
    text.addFocusListener(formatter);
}
 
Example 18
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 19
Source File: CellTypeTextFieldDefaultImpl.java    From rapidminer-studio with GNU Affero General Public License v3.0 4 votes vote down vote up
/**
 * Creates a {@link JFormattedTextField} for the specified cell. If a formatter is given, will
 * apply it to the field. Does not validate the model, so make sure this call works!
 * 
 * @param model
 * @param rowIndex
 * @param columnIndex
 * @param cellClass
 * @param formatter
 *            the formatter or <code>null</code> if none is required
 * @param hideUnavailableContentAssist
 * @return
 */
public CellTypeTextFieldDefaultImpl(final TablePanelModel model, final int rowIndex, final int columnIndex,
		final Class<? extends CellType> cellClass, AbstractFormatter formatter, boolean hideUnavailableContentAssist) {
	super();

	final JFormattedTextField field = CellTypeImplHelper.createFormattedTextField(model, rowIndex, columnIndex);
	setLayout(new BorderLayout());
	add(field, BorderLayout.CENTER);

	// otherwise 'null' would be restored
	Object value = model.getValueAt(rowIndex, columnIndex);
	String text = value != null ? String.valueOf(value) : "";

	// specical handling when formatter is given
	if (formatter != null) {
		field.setFormatterFactory(new DefaultFormatterFactory(formatter));
	}
	field.setText(text);

	// set syntax assist if available
	String syntaxHelp = model.getSyntaxHelpAt(rowIndex, columnIndex);
	if (syntaxHelp != null && !"".equals(syntaxHelp.trim())) {
		SwingTools.setPrompt(syntaxHelp, field);
	}

	// see if content assist is possible for this field, if so add it
	ImageIcon icon = SwingTools.createIcon("16/"
			+ I18N.getMessageOrNull(I18N.getGUIBundle(), "gui.action.content_assist.icon"));
	JButton contentAssistButton = new JButton();
	contentAssistButton.setIcon(icon);
	if (field.isEnabled() && model.isContentAssistPossibleForCell(rowIndex, columnIndex)) {
		contentAssistButton.setToolTipText(I18N.getMessageOrNull(I18N.getGUIBundle(),
				"gui.action.content_assist_enabled.tip"));
		CellTypeImplHelper.addContentAssist(model, rowIndex, columnIndex, field, contentAssistButton, cellClass);
	} else {
		contentAssistButton.setToolTipText(I18N.getMessageOrNull(I18N.getGUIBundle(),
				"gui.action.content_assist_disabled.tip"));
		contentAssistButton.setEnabled(false);
	}
	if (contentAssistButton.isEnabled() || (!contentAssistButton.isEnabled() && !hideUnavailableContentAssist)) {
		add(contentAssistButton, BorderLayout.EAST);
	}

	// set size so panels don't grow larger when they get the chance
	setPreferredSize(new Dimension(300, 20));
	setMinimumSize(new Dimension(100, 15));
	setMaximumSize(new Dimension(1600, 30));
}
 
Example 20
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)
				{
				}
			}
		}

	});
}