下面列出了怎么用javax.swing.SpinnerNumberModel的API类实例代码及写法,或者点击链接到github查看源代码。
public Option(String text, int max) {
spinner = new JSpinner(new SpinnerNumberModel(0, 0, max, 5));
spinner.addChangeListener(changeListener);
/*
* this tries out Tim Boudreaux's new slider UI. It's a good UI, but
* I think for the JColorPicker the numeric controls are more
* useful. That is: users who want click-and-drag control to choose
* their colors don't need any of these Option objects at all; only
* power users who may have specific RGB values in mind will use
* these controls: and when they do limiting them to a slider is
* unnecessary. That's my current position... of course it may not
* be true in the real world... :)
*/
// slider = new JSlider(0,max);
// slider.addChangeListener(changeListener);
// slider.setUI(new
// org.netbeans.paint.api.components.PopupSliderUI());
label = new JLabel(text);
radioButton.addActionListener(actionListener);
}
private void fontActionPerformed() {
final FontInfo[] fontsInfo = glyphManager.getFonts();
final String fontName = (String) fontNameSp.getSelectedItem();
final int fontStyle = cbBold.isSelected() ? Font.BOLD : Font.PLAIN;
final int fontSize = ((SpinnerNumberModel) fontSizeSp.getModel()).getNumber().intValue();
final FontInfo fi = fontsInfo[0];
fontsInfo[0] = new FontInfo(fontName, fontStyle, fontSize, fi.mustHave, fi.mustNotHave);
glyphManager.setFonts(fontsInfo);
glyphManager.createBackgroundGlyph(0.5f);
showTextureBuffer();
final String line = getLine();
glyphManager.renderTextAsLigatures(line, null);
repaint();
}
private ControlsPanel() {
cmbPaint = createCombo(this, paintType);
cmbPaint.setSelectedIndex(1);
cmbCycle = createCombo(this, cycleMethod);
cmbSpace = createCombo(this, colorSpace);
cmbShape = createCombo(this, shapeType);
cmbXform = createCombo(this, xformType);
int max = COLORS.length;
SpinnerNumberModel model = new SpinnerNumberModel(max, 2, max, 1);
spinNumColors = new JSpinner(model);
spinNumColors.addChangeListener(new ChangeListener() {
public void stateChanged(ChangeEvent e) {
numColors = ((Integer)spinNumColors.getValue()).intValue();
gradientPanel.updatePaint();
}
});
add(spinNumColors);
cbAntialias = createCheck(this, "Antialiasing");
cbRender = createCheck(this, "Render Quality");
}
public CgSpinner(int start, int min, int max, int step) {
super();
this.min = min;
this.max = max;
this.step = step;
model = new SpinnerNumberModel(start, // initial value
min, // min
max, // max
step); // step
setModel(model);
addMouseWheelListener(new MouseWheelListener() {
public void mouseWheelMoved(MouseWheelEvent mwe) {
MouseWheelAction(mwe.getWheelRotation());
}
});
// Center
JSpinner.DefaultEditor spinnerEditor = (JSpinner.DefaultEditor) this.getEditor();
spinnerEditor.getTextField().setHorizontalAlignment(JTextField.CENTER);
}
/**
* Constructor.
*/
public SpinnerRangeDouble(double defValue, double minValue, double maxValue, double incValue) {
super();
this.defValue = defValue;
this.minValue = minValue;
this.maxValue = maxValue;
this.incValue = incValue;
Double def = new Double(defValue);
Double min = new Double(minValue);
Double max = new Double(maxValue);
Double inc = new Double(incValue);
model = new SpinnerNumberModel(def, min, max, inc);
setModel(model);
JFormattedTextField tf = ((JSpinner.DefaultEditor) getEditor()).getTextField();
tf.setColumns(7);
}
/**
* Constructor.
*/
public SpinnerRangeDouble(double defValue, double minValue, double maxValue, double incValue, String format) {
super();
this.defValue = defValue;
this.minValue = minValue;
this.maxValue = maxValue;
this.incValue = incValue;
Double def = new Double(defValue);
Double min = new Double(minValue);
Double max = new Double(maxValue);
Double inc = new Double(incValue);
model = new SpinnerNumberModel(def, min, max, inc);
setModel(model);
setEditor(new JSpinner.NumberEditor(this, format));
JFormattedTextField tf = ((JSpinner.DefaultEditor) getEditor()).getTextField();
tf.setColumns(7);
}
/**
* Creates new form AddLayerDialog
*/
public AddLayerDialog(Window parent, List<Layer> layers, int maxHeight) {
super(parent);
this.maxHeight = maxHeight;
initComponents();
DefaultComboBoxModel<Layer> comboBoxModel = new DefaultComboBoxModel<>(layers.toArray(new Layer[layers.size()]));
comboBoxLayer.setModel(comboBoxModel);
comboBoxLayer.setRenderer(new LayerListCellRenderer());
((SpinnerNumberModel) spinnerFrom.getModel()).setMaximum(maxHeight - 1);
spinnerFrom.setValue(maxHeight / 2);
((SpinnerNumberModel) spinnerTo.getModel()).setMaximum(maxHeight - 1);
spinnerTo.setValue(maxHeight - 1);
setControlStates();
getRootPane().setDefaultButton(buttonOK);
scaleToUI();
pack();
setLocationRelativeTo(parent);
}
/**
* Constructor.
*/
public SpinnerRangeFloat(float defValue, float minValue, float maxValue, float incValue, String format) {
super();
this.defValue = defValue;
this.minValue = minValue;
this.maxValue = maxValue;
this.incValue = incValue;
Double def = new Double(defValue);
Double min = new Double(minValue);
Double max = new Double(maxValue);
Double inc = new Double(incValue);
this.model = new SpinnerNumberModel(def, min, max, inc);
setModel(model);
setEditor(new JSpinner.NumberEditor(this, format));
JFormattedTextField tf = ((JSpinner.DefaultEditor) getEditor()).getTextField();
tf.setColumns(7);
}
/**
* Constructor.
*/
public SpinnerRangeFloat(float defValue, float minValue, float maxValue, float incValue, int visibleChars) {
super();
this.defValue = defValue;
this.minValue = minValue;
this.maxValue = maxValue;
this.incValue = incValue;
Float def = new Float(defValue);
Float min = new Float(minValue);
Float max = new Float(maxValue);
Float inc = new Float(incValue);
model = new SpinnerNumberModel(def, min, max, inc);
setModel(model);
JFormattedTextField tf = ((JSpinner.DefaultEditor) getEditor()).getTextField();
tf.setColumns(visibleChars);
}
/**
* Constructor.
*/
public SpinnerRangeInteger(int defValue, int minValue, int maxValue, int incValue) {
super();
this.defValue = defValue;
this.minValue = minValue;
this.maxValue = maxValue;
this.incValue = incValue;
Integer def = new Integer(defValue);
Integer min = new Integer(minValue);
Integer max = new Integer(maxValue);
Integer inc = new Integer(incValue);
model = new SpinnerNumberModel(def, min, max, inc);
setModel(model);
JFormattedTextField tf = ((JSpinner.DefaultEditor) getEditor()).getTextField();
tf.setColumns(7);
}
private ControlsPanel() {
cmbPaint = createCombo(this, paintType);
cmbPaint.setSelectedIndex(1);
cmbCycle = createCombo(this, cycleMethod);
cmbSpace = createCombo(this, colorSpace);
cmbShape = createCombo(this, shapeType);
cmbXform = createCombo(this, xformType);
int max = COLORS.length;
SpinnerNumberModel model = new SpinnerNumberModel(max, 2, max, 1);
spinNumColors = new JSpinner(model);
spinNumColors.addChangeListener(new ChangeListener() {
public void stateChanged(ChangeEvent e) {
numColors = ((Integer)spinNumColors.getValue()).intValue();
gradientPanel.updatePaint();
}
});
add(spinNumColors);
cbAntialias = createCheck(this, "Antialiasing");
cbRender = createCheck(this, "Render Quality");
}
/**
* Creates a new ComboBoxSearchField.
* @param id id of the search field
* @param valueVector vector of values to add to the combo box (null values are not allowed!)
*/
public ComboBoxSearchField( final Id id, final Vector< Object > valueVector, final boolean showMinOccurence ) {
super( id );
comboBox = new JComboBox<>( valueVector );
comboBox.setRenderer( new BaseLabelListCellRenderer< Object >() {
@Override
public Icon getIcon( final Object value ) {
return ComboBoxSearchField.this.getIcon( value );
}
} );
comboBox.setPreferredSize( new Dimension( 100, comboBox.getMinimumSize().height ) );
uiComponent.add( comboBox );
if ( showMinOccurence ) {
minOccurrenceSpinner = new JSpinner( new SpinnerNumberModel( 1, 1, 999, 1 ) );
uiComponent.add( new JLabel( Language.getText( "module.repSearch.tab.filters.name.minOccurrenceText" ) ) );
minOccurrenceSpinner.setEditor( new JSpinner.NumberEditor( minOccurrenceSpinner ) );
minOccurrenceSpinner.setMaximumSize( new Dimension( 50, minOccurrenceSpinner.getPreferredSize().height ) );
uiComponent.add( minOccurrenceSpinner );
}
}
private ControlsPanel() {
cmbPaint = createCombo(this, paintType);
cmbPaint.setSelectedIndex(1);
cmbCycle = createCombo(this, cycleMethod);
cmbSpace = createCombo(this, colorSpace);
cmbShape = createCombo(this, shapeType);
cmbXform = createCombo(this, xformType);
int max = COLORS.length;
SpinnerNumberModel model = new SpinnerNumberModel(max, 2, max, 1);
spinNumColors = new JSpinner(model);
spinNumColors.addChangeListener(new ChangeListener() {
public void stateChanged(ChangeEvent e) {
numColors = ((Integer)spinNumColors.getValue()).intValue();
gradientPanel.updatePaint();
}
});
add(spinNumColors);
cbAntialias = createCheck(this, "Antialiasing");
cbRender = createCheck(this, "Render Quality");
}
private ControlsPanel() {
cmbPaint = createCombo(this, paintType);
cmbPaint.setSelectedIndex(1);
cmbCycle = createCombo(this, cycleMethod);
cmbSpace = createCombo(this, colorSpace);
cmbShape = createCombo(this, shapeType);
cmbXform = createCombo(this, xformType);
int max = COLORS.length;
SpinnerNumberModel model = new SpinnerNumberModel(max, 2, max, 1);
spinNumColors = new JSpinner(model);
spinNumColors.addChangeListener(new ChangeListener() {
public void stateChanged(ChangeEvent e) {
numColors = ((Integer)spinNumColors.getValue()).intValue();
gradientPanel.updatePaint();
}
});
add(spinNumColors);
cbAntialias = createCheck(this, "Antialiasing");
cbRender = createCheck(this, "Render Quality");
}
private ControlsPanel() {
cmbPaint = createCombo(this, paintType);
cmbPaint.setSelectedIndex(1);
cmbCycle = createCombo(this, cycleMethod);
cmbSpace = createCombo(this, colorSpace);
cmbShape = createCombo(this, shapeType);
cmbXform = createCombo(this, xformType);
int max = COLORS.length;
SpinnerNumberModel model = new SpinnerNumberModel(max, 2, max, 1);
spinNumColors = new JSpinner(model);
spinNumColors.addChangeListener(new ChangeListener() {
public void stateChanged(ChangeEvent e) {
numColors = ((Integer)spinNumColors.getValue()).intValue();
gradientPanel.updatePaint();
}
});
add(spinNumColors);
cbAntialias = createCheck(this, "Antialiasing");
cbRender = createCheck(this, "Render Quality");
}
public CgSpinnerDouble(double start, double min, double max, double step) {
super();
this.min = min;
this.max = max;
this.step = step;
model = new SpinnerNumberModel(start, // initial value
min, // min
max, // max
step);
this.setModel(model);
addMouseWheelListener(new MouseWheelListener() {
public void mouseWheelMoved(MouseWheelEvent mwe) {
MouseWheelAction(mwe.getWheelRotation());
}
});
// Center
JSpinner.DefaultEditor spinnerEditor = (JSpinner.DefaultEditor) this.getEditor();
spinnerEditor.getTextField().setHorizontalAlignment(JTextField.CENTER);
}
private Component[] createTimeDeltaComponents(TableLayout tableLayout) {
final JLabel boxLabel = new JLabel("Allowed time difference:");
timeBox = new JCheckBox("Use time difference constraint");
final Component horizontalSpacer = tableLayout.createHorizontalSpacer();
final Component horizontalSpacer2 = tableLayout.createHorizontalSpacer();
timeSpinner = new JSpinner(new SpinnerNumberModel(1, 1, null, 1));
timeSpinner.setEnabled(false);
timeUnitComboBox = new JComboBox<>(new String[]{"Day(s)", "Hour(s)", "Minute(s)"});
timeUnitComboBox.setEnabled(false);
timeBox.addActionListener(e -> {
timeSpinner.setEnabled(timeBox.isSelected());
timeUnitComboBox.setEnabled(timeBox.isSelected());
});
return new Component[]{boxLabel, timeBox, horizontalSpacer, horizontalSpacer2, timeSpinner, timeUnitComboBox};
}
/**
* Sets up time chooser with default values for the spinners. Bad defaults get
* set to 0.
*
* @param hr hour
* @param min minute
* @param sec second
* @param msec msec
*/
private TimeChooser(final int hr, final int min, final int sec, final int msec) {
final int hour = ((hr > 23) || (hr < 0)) ? 0 : hr;
final int minute = ((min > 59) || (min < 0)) ? 0 : min;
final int second = ((sec > 59) || (sec < 0)) ? 0 : sec;
final int msecond = ((msec > 999) || (msec < 0)) ? 0 : msec;
setLayout(new FlowLayout(FlowLayout.LEFT, 2, 0));
h = new JSpinner(new SpinnerNumberModel(hour, 0, 23, 1));
this.add(h);
this.add(new JLabel(":"));
m = new JSpinner(new SpinnerNumberModel(minute, 0, 59, 1));
this.add(m);
this.add(new JLabel(":"));
s = new JSpinner(new SpinnerNumberModel(second, 0, 59, 1));
this.add(s);
this.add(new JLabel("."));
ms = new JSpinner(new SpinnerNumberModel(msecond, 0, 999, 1));
this.add(ms);
h.setToolTipText("h");
m.setToolTipText("m");
s.setToolTipText("s");
ms.setToolTipText("ms");
}
protected void updateSliderState(JSlider slider) {
sliderAdjusting++;
try {
boolean useSlider = false;
if (spinner.getModel() instanceof SpinnerNumberModel) {
useSlider = true;
SpinnerNumberModel numberModel = (SpinnerNumberModel) spinner
.getModel();
Number min = (Number) numberModel.getMinimum();
Number max = (Number) numberModel.getMaximum();
float range = max.floatValue() - min.floatValue();
float pos = (((Number) numberModel.getValue()).floatValue() - min
.floatValue()) / range;
int sliderRange = slider.getMaximum() - slider.getMinimum();
int sliderValue = (int) (pos * sliderRange + slider
.getMinimum());
slider.setValue(sliderValue);
}
if (useSlider != slider.isVisible())
slider.setVisible(useSlider);
if (useSlider != firstButton.isVisible())
firstButton.setVisible(useSlider);
if (useSlider != lastButton.isVisible())
lastButton.setVisible(useSlider);
if (useSlider == getLabel().isVisible())
getLabel().setVisible(!useSlider);
} finally {
sliderAdjusting--;
}
}
/**
* Sets the inputs
* @param inputs The inputs
*/
public void setInputs(LinkedList<String> inputs) {
panel.removeAll();
panel.setSize(panel.getWidth(),25*inputs.size());
panel.setLayout(new GridLayout((inputs.size() < 8) ? 8-inputs.size(): inputs.size(), 1, 5, 5));
mapSpinners.clear();
for (String input: inputs) {
JSpinner spinner = new JSpinner(new SpinnerNumberModel());
mapSpinners.put(spinner, input);
panel.add(new LabeledComponent(input+":", spinner));
spinner.addChangeListener(this);
}
}
private NumberSpinnerBuilder(V number, V min, V max, V stepSize) {
super(new JSpinner());
model = new SpinnerNumberModel(number, (Comparable) min, (Comparable) max, stepSize);
getField().setModel(model);
withGetter((field) -> (V) field.getValue());
withSetter((field, value) -> { if (value != null) {field.setValue((V) value);}});
}
public bug6463712() {
SpinnerNumberModel m1 = new SpinnerNumberModel();
JSpinner s = new JSpinner(m1);
s.addChangeListener(this);
SpinnerDateModel m2 = new SpinnerDateModel();
s.setModel(m2);
// m1 is no longer linked to the JSpinner (it has been replaced by m2), so
// the following should not trigger a call to our stateChanged() method...
m1.setValue(new Integer(1));
}
private JPanel tracingPanel() {
final JPanel tracingOptionsPanel = new JPanel(new FlowLayout(FlowLayout.LEFT));
useSnapWindow = new JCheckBox("Enable cursor [s]napping within: XY", plugin.snapCursor);
useSnapWindow.setBorder(new EmptyBorder(0, 0, 0, 0));
useSnapWindow.addItemListener(this);
tracingOptionsPanel.add(useSnapWindow);
final SpinnerModel xy_model = new SpinnerNumberModel(plugin.cursorSnapWindowXY * 2,
SimpleNeuriteTracer.MIN_SNAP_CURSOR_WINDOW_XY, SimpleNeuriteTracer.MAX_SNAP_CURSOR_WINDOW_XY * 2, 2);
snapWindowXYsizeSpinner = new JSpinner(xy_model);
((DefaultEditor) snapWindowXYsizeSpinner.getEditor()).getTextField().setEditable(false);
snapWindowXYsizeSpinner.addChangeListener(this);
tracingOptionsPanel.add(snapWindowXYsizeSpinner);
final JLabel z_spinner_label = leftAlignedLabel("Z", isStackAvailable());
z_spinner_label.setBorder(new EmptyBorder(0, 2, 0, 0));
tracingOptionsPanel.add(z_spinner_label);
final SpinnerModel z_model = new SpinnerNumberModel(plugin.cursorSnapWindowZ * 2,
SimpleNeuriteTracer.MIN_SNAP_CURSOR_WINDOW_Z, SimpleNeuriteTracer.MAX_SNAP_CURSOR_WINDOW_Z * 2, 2);
snapWindowZsizeSpinner = new JSpinner(z_model);
((DefaultEditor) snapWindowZsizeSpinner.getEditor()).getTextField().setEditable(false);
snapWindowZsizeSpinner.addChangeListener(this);
snapWindowZsizeSpinner.setEnabled(isStackAvailable());
tracingOptionsPanel.add(snapWindowZsizeSpinner);
// tracingOptionsPanel.setBorder(new EmptyBorder(0, 0, 0, 0));
return tracingOptionsPanel;
}
/**
* Creates the main frame for <code>this</code> sample.
*/
public PasswordEchoPerChar() {
super("Password echo per char");
this.setLayout(new BorderLayout());
final JPanel panel = new JPanel(new FlowLayout());
this.add(panel, BorderLayout.CENTER);
final JPasswordField jpf = new JPasswordField("sample");
jpf.setColumns(20);
panel.add(jpf);
JPanel controls = new JPanel(new FlowLayout(FlowLayout.RIGHT));
final JSpinner countSpinner = new JSpinner();
SpinnerNumberModel model = new SpinnerNumberModel(1, 1, 5, 1);
countSpinner.setModel(model);
countSpinner.addChangeListener((ChangeEvent e) -> {
// set the amount of echo per character based on the current
// value in the spinner
SubstanceCortex.ComponentScope.setNumberOfPasswordEchoesPerCharacter(jpf,
(int) countSpinner.getValue());
jpf.repaint();
});
controls.add(new JLabel("Echo per char"));
controls.add(countSpinner);
this.add(controls, BorderLayout.SOUTH);
this.setSize(400, 200);
this.setLocationRelativeTo(null);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
/**
* Prompts the user for float value
*
* @param name The name of the dialog to show
* @param currentValue The current value to be displayed
* @param description The help text to provide
* @param min The minimum value to allow
* @param max The maximum value to allow
* @return The value selected by the user
*/
static public Value floatValue (String name, final float currentValue, final float min, final float max,
final String description) {
return new DefaultValue(name, String.valueOf(currentValue)) {
public void showDialog () {
JSpinner spinner = new JSpinner(new SpinnerNumberModel(currentValue, min, max, 0.1f));
if (showValueDialog(spinner, description)) value = String.valueOf(((Double)spinner.getValue()).floatValue());
}
public Object getObject () {
return Float.valueOf(value);
}
};
}
/**
* Creates the ui.
*
* @param initialValue the initial value
* @param min the minimum value
* @param max the maximum value
* @param stepSize the step size
* @param noOfDecimalPlaces the number of decimal places
*/
private void createUI(
Double initialValue,
Double min,
Double max,
Double stepSize,
double noOfDecimalPlaces) {
SpinnerNumberModel model = new SpinnerNumberModel(initialValue, min, max, stepSize);
setModel(model);
JSpinner.NumberEditor editor = (JSpinner.NumberEditor) getEditor();
DecimalFormat format = editor.getFormat();
format.setMinimumFractionDigits((int) noOfDecimalPlaces);
final JFormattedTextField field = editor.getTextField();
DefaultFormatter formatter = (DefaultFormatter) field.getFormatter();
formatter.setCommitsOnValidEdit(true);
addChangeListener(
new ChangeListener() {
private double oldValue = Double.MAX_VALUE;
@Override
public void stateChanged(ChangeEvent e) {
Double doubleValue = DecimalSpinner.this.getDoubleValue();
if (doubleValue != oldValue) {
double oldValueCopy = oldValue;
oldValue = doubleValue;
if (minIsZero && (doubleValue < 0.0)) {
doubleValue = 0.0;
field.setValue(doubleValue);
}
notifyListeners(oldValueCopy, doubleValue);
}
}
});
}
/**
* Set the minimal and the maximal limit.
*/
public void setLimit(float minValue, float maxValue) {
this.minValue = minValue;
this.maxValue = maxValue;
float value = get();
Float min = new Float(minValue);
Float max = new Float(maxValue);
Float inc = new Float(incValue);
defValue = (value > maxValue ? maxValue : (value < minValue ? minValue : value));
Float def = new Float(defValue);
model = new SpinnerNumberModel(def, min, max, inc);
setModel(model);
}
private JSpinner createCoordinateSpinner() {
JSpinner newSpinner = new JSpinner(new SpinnerNumberModel(0, -30000000, 30000000, 25));
newSpinner.addChangeListener(e -> {
renderPreview();
});
return newSpinner;
}
public bug6463712() {
SpinnerNumberModel m1 = new SpinnerNumberModel();
JSpinner s = new JSpinner(m1);
s.addChangeListener(this);
SpinnerDateModel m2 = new SpinnerDateModel();
s.setModel(m2);
// m1 is no longer linked to the JSpinner (it has been replaced by m2), so
// the following should not trigger a call to our stateChanged() method...
m1.setValue(new Integer(1));
}
private void querySizeAndResize() {
final SpinnerNumberModel w = new SpinnerNumberModel(editedImage.getWidth(), 1, 10000, 1);
final SpinnerNumberModel h = new SpinnerNumberModel(editedImage.getHeight(), 1, 10000, 1);
final JSpinner ws = new JSpinner(w);
final JSpinner hs = new JSpinner(h);
final JPanel lab = new JPanel(new GridLayout(2, 1, 8, 8));
final JPanel spi = new JPanel(new GridLayout(2, 1, 8, 8));
lab.add(new JLabel("New Width"));
lab.add(new JLabel("New Height"));
spi.add(ws);
spi.add(hs);
final JPanel box = new JPanel(new BorderLayout(8, 8));
box.add(lab, BorderLayout.LINE_START);
box.add(spi, BorderLayout.CENTER);
final Object[] options = {"Ok", "Cancel"};
int a = JOptionPane.showOptionDialog(
COMPONENT,
box,
"Resize Image",
JOptionPane.OK_CANCEL_OPTION,
JOptionPane.PLAIN_MESSAGE,
null,
options,
options[1]);
if (a == 0) {
int newWidth = w.getNumber().intValue();
int newHeight = h.getNumber().intValue();
if (newWidth != editedImage.getWidth() || newHeight != editedImage.getHeight()) {
spawnEditor(ResizeFilter.create().filter(editedImage, newWidth, newHeight));
}
}
}