001package jmri.util.swing; 002 003import javax.swing.JSpinner; 004import javax.swing.text.DefaultFormatter; 005 006/** 007 * Utility class providing common methods for working with {@link JSpinner} 008 * components in Swing. 009 * @author Steve Young Copyright(C) 2025 010 */ 011public class JSpinnerUtil { 012 013 private JSpinnerUtil(){} // Class only supplies static methods. 014 015 /** 016 * Sets whether the {@link JSpinner}'s text editor commits the value to the 017 * spinner model. 018 * <p> 019 * By default, JSpinners only update their value when enter is pressed 020 * or focus leaves the JFormattedTextField. 021 * </p> 022 * 023 * @param spinner the JSpinner to modify. 024 * @param newVal true to commit on valid edits, else false ( default JSpinner ) 025 */ 026 public static void setCommitsOnValidEdit( JSpinner spinner, boolean newVal) { 027 JSpinner.DefaultEditor editor = (JSpinner.DefaultEditor)spinner.getEditor(); 028 var field = editor.getTextField(); 029 DefaultFormatter formatter = (DefaultFormatter) field.getFormatter(); 030 formatter.setCommitsOnValidEdit(newVal); 031 } 032 033}