void (@NotNull JFormattedTextField textField) { NumberFormat format = NumberFormat.getIntegerInstance(); format.setParseIntegerOnly(true); format.setGroupingUsed(false); NumberFormatter numberFormatter = new NumberFormatter(format); numberFormatter.setMinimum(0); textField.setFormatterFactory(new DefaultFormatterFactory(numberFormatter)); textField.setHorizontalAlignment(SwingConstants.TRAILING); textField.setColumns(4); }