Skip to content

Commit

Permalink
Fix issue with caret position in spinner
Browse files Browse the repository at this point in the history
  • Loading branch information
jdhoek committed May 6, 2020
1 parent a0a4193 commit 50d0b09
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 3 deletions.
7 changes: 4 additions & 3 deletions src/nl/jeroenhoek/josm/gridify/ui/GridSizePanel.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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());
}
}
Expand All @@ -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());
}
}
Expand Down
12 changes: 12 additions & 0 deletions src/nl/jeroenhoek/josm/gridify/ui/PositiveSpinner.java
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down

0 comments on commit 50d0b09

Please sign in to comment.