diff --git a/src/nl/jeroenhoek/josm/gridify/ui/GridSizePanel.java b/src/nl/jeroenhoek/josm/gridify/ui/GridSizePanel.java index 8138606..e6e3bcd 100644 --- a/src/nl/jeroenhoek/josm/gridify/ui/GridSizePanel.java +++ b/src/nl/jeroenhoek/josm/gridify/ui/GridSizePanel.java @@ -5,15 +5,14 @@ import javax.swing.BoxLayout; import javax.swing.JLabel; import javax.swing.JPanel; -import javax.swing.JSpinner; import java.awt.Font; /** * UI widget that allows the user to set the grid division; i.e., how many rows and columns. */ public class GridSizePanel extends JPanel { - private final JSpinner spinnerRows; - private final JSpinner spinnerColumns; + private final PositiveSpinner spinnerRows; + private final PositiveSpinner spinnerColumns; private final ChangeCallback changeCallback; @@ -60,6 +59,7 @@ void nudgeRowCount(Nudge direction) { if (direction == Nudge.INCREMENT || this.rows > 1) { this.rows += direction == Nudge.INCREMENT ? 1 : -1; this.spinnerRows.setValue(rows); + this.spinnerRows.caretToEnd(); changeCallback.changed(getRowCount(), getColumnCount()); } } @@ -68,6 +68,7 @@ void nudgeColumnCount(Nudge direction) { if (direction == Nudge.INCREMENT || this.columns > 1) { this.columns += direction == Nudge.INCREMENT ? 1 : -1; this.spinnerColumns.setValue(columns); + this.spinnerColumns.caretToEnd(); changeCallback.changed(getRowCount(), getColumnCount()); } } diff --git a/src/nl/jeroenhoek/josm/gridify/ui/PositiveSpinner.java b/src/nl/jeroenhoek/josm/gridify/ui/PositiveSpinner.java index 5807d98..f225fc0 100644 --- a/src/nl/jeroenhoek/josm/gridify/ui/PositiveSpinner.java +++ b/src/nl/jeroenhoek/josm/gridify/ui/PositiveSpinner.java @@ -91,6 +91,18 @@ void callbackIfChanged() { } } + @Override + public void setValue(Object value) { + // No need to update the value if nothing changes. This prevents the caret being placed at an awkward position. + if (Objects.equals(value, lastValue)) return; + + super.setValue(value); + } + + public void caretToEnd() { + field.setCaretPosition(field.getText().length()); + } + @Override public Dimension getMaximumSize() { return getPreferredSize();