JFormattedTextField issues

On Mac OS, the default behavior of the UI delegate, com.apple.laf.AquaTextFieldF, is similar to CaretPositionListener: Tab or Shift–Tab: place the caret at the beginning of the field. Click: briefly place the caret at the beginning of the field and then move it to the click point. IMO, the CaretPositionListener does the latter much more smoothly. … Read more

Make JSpinner only read numbers but also detect backspace

you are right JFormattedTextField isn’t correctly implemented to JSpinner, you have implements DocumentFilter for filtering of un_wanted Chars typed from keyboad or pasted from ClipBoard, (thanks to @StanislavL) you have solve by yourself issues with selectAll() on focusGained() wrapped into invokeLater(), example import java.awt.BorderLayout; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JSpinner; import javax.swing.SpinnerNumberModel; import javax.swing.SwingUtilities; import … Read more

jFormattedTextField’s Formatter.setCommitsOnValidEdit(true) doesn’t work at first focus

Actually, setCommitOnValidEdit should work always as you expect (and does in the code snippet below), no need for a DocumentListener, after all, the method is exactly for that purpose. So I suspect something else is wrong in your context. Or for some reason the very first edit isn’t parsed to anything valid? NumberFormatter numberFormatter = … Read more

How to rendering fraction in Swing JComponents

On reflection, Unicode fractions among the Latin-1 Supplement and Number Forms offer limited coverage, and fancy equations may be overkill. This example uses HTML in Swing Components. Addendum: The approach shown lends itself fairly well to rendering mixed numbers. For editing, key bindings to + and / could be added for calculator-style input in a … Read more

JFormattedTextField : input time duration value

Here’s an example of using InputVerifier to accommodate multiple input formats. import java.awt.EventQueue; import java.text.NumberFormat; import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.ArrayList; import java.util.Date; import java.util.List; import javax.swing.Box; import javax.swing.InputVerifier; import javax.swing.JComponent; import javax.swing.JFormattedTextField; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.text.DateFormatter; import javax.swing.text.DefaultFormatterFactory; public class FormattedFields { public static void main(String[] args) { EventQueue.invokeLater(new Runnable() { //@Override … Read more