Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add focus listener to JSpinner text #13

Merged
merged 1 commit into from
Apr 30, 2015
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 49 additions & 0 deletions src/main/java/org/scijava/ui/swing/widget/SwingNumberWidget.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand All @@ -64,6 +70,9 @@ public class SwingNumberWidget extends SwingInputWidget<Number> implements
NumberWidget<JPanel>, AdjustmentListener, ChangeListener
{

@Parameter
private ThreadService threadService;

private JScrollBar scrollBar;
private JSlider slider;
private JSpinner spinner;
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -200,6 +212,43 @@ private void fixSpinner(final Class<?> type) {
}
}

/**
* Adapted from <a href=
* "http://stackoverflow.com/q/20971050/1027800"
* >this SO post</a>.
*
* 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) {
Expand Down