diff --git a/src/main/java/org/scijava/ui/swing/widget/SwingNumberWidget.java b/src/main/java/org/scijava/ui/swing/widget/SwingNumberWidget.java index c0636a7..028175d 100644 --- a/src/main/java/org/scijava/ui/swing/widget/SwingNumberWidget.java +++ b/src/main/java/org/scijava/ui/swing/widget/SwingNumberWidget.java @@ -31,9 +31,12 @@ package org.scijava.ui.swing.widget; import java.awt.Adjustable; +import java.awt.Component; import java.awt.Dimension; import java.awt.event.AdjustmentEvent; import java.awt.event.AdjustmentListener; +import java.awt.event.FocusEvent; +import java.awt.event.FocusListener; import java.math.BigDecimal; import java.math.BigInteger; import java.text.DecimalFormat; @@ -44,11 +47,14 @@ import javax.swing.JScrollBar; import javax.swing.JSlider; import javax.swing.JSpinner; +import javax.swing.JTextField; import javax.swing.SpinnerNumberModel; import javax.swing.event.ChangeEvent; import javax.swing.event.ChangeListener; +import org.scijava.plugin.Parameter; import org.scijava.plugin.Plugin; +import org.scijava.thread.ThreadService; import org.scijava.util.NumberUtils; import org.scijava.widget.InputWidget; import org.scijava.widget.NumberWidget; @@ -64,6 +70,9 @@ public class SwingNumberWidget extends SwingInputWidget implements NumberWidget, AdjustmentListener, ChangeListener { + @Parameter + private ThreadService threadService; + private JScrollBar scrollBar; private JSlider slider; private JSpinner spinner; @@ -121,6 +130,9 @@ else if (model.isStyle(NumberWidget.SLIDER_STYLE)) { final SpinnerNumberModel spinnerModel = new SpinnerNumberModelFactory().createModel(value, min, max, stepSize); spinner = new JSpinner(spinnerModel); + + fixSpinnerFocus(); + fixSpinner(type); setToolTip(spinner); getComponent().add(spinner); @@ -200,6 +212,43 @@ private void fixSpinner(final Class type) { } } + /** + * Adapted from this SO post. + * + * Tries to ensure that the text of a {@link JSpinner} is selected when it + * has focus. + */ + private void fixSpinnerFocus() { + for (final Component c : spinner.getEditor().getComponents()) { + if (JTextField.class.isAssignableFrom(c.getClass())) { + c.addFocusListener(new FocusListener() { + + @Override + public void focusGained(final FocusEvent e) { + queueSelection(); + } + + @Override + public void focusLost(final FocusEvent e) { + queueSelection(); + } + + private void queueSelection() { + threadService.queue(new Runnable() { + @Override + public void run() { + ((JTextField) c).selectAll(); + } + }); + } + + }); + } + } + } + /** Sets slider values to match the spinner. */ private void syncSliders() { if (slider != null) {