/** * Returns the maximum frequency spinner. * If the spinner doesn't exist it is created: * <ul> * <li>with the specified {@link #getMaxFrequencyModel() model},</li> * <li>to have specified size (80x25 pixel),</li> * <li>with the listener which updates the value of the {@link * #getMinFrequencySpinner() minimum frequency spinner} to be at least * {@code 0.01} smaller then the value of this spinner.</li></ul> * @return the maximum frequency spinner */ public JSpinner getMaxFrequencySpinner() { if (maxFrequencySpinner == null) { maxFrequencySpinner = new JSpinner(getMaxFrequencyModel()); Dimension spinnerSize = new Dimension(80,25); maxFrequencySpinner.setPreferredSize(spinnerSize); maxFrequencySpinner.setMinimumSize(spinnerSize); maxFrequencySpinner.setMaximumSize(spinnerSize); maxFrequencySpinner.addChangeListener(new ChangeListener() { @Override public void stateChanged(ChangeEvent e) { double value = ((Number) maxFrequencySpinner.getValue()).doubleValue(); double otherValue = ((Number) getMinFrequencySpinner().getValue()).doubleValue(); if ((value-0.01) < otherValue) { getMinFrequencySpinner().setValue(value - 0.01); } } }); maxFrequencySpinner.setEditor(new JSpinner.NumberEditor(maxFrequencySpinner, "0.00")); maxFrequencySpinner.setFont(maxFrequencySpinner.getFont().deriveFont(Font.PLAIN)); } return maxFrequencySpinner; }
private static int getSpinnerBaseline(JSpinner spinner, int height) { JComponent editor = spinner.getEditor(); if (editor instanceof JSpinner.DefaultEditor) { JSpinner.DefaultEditor defaultEditor = (JSpinner.DefaultEditor) editor; JTextField tf = defaultEditor.getTextField(); Insets spinnerInsets = spinner.getInsets(); Insets editorInsets = defaultEditor.getInsets(); int offset = spinnerInsets.top + editorInsets.top; height -= (offset + spinnerInsets.bottom + editorInsets.bottom); if (height <= 0) { return -1; } return offset + getSingleLineTextBaseline(tf, height); } Insets insets = spinner.getInsets(); FontMetrics fm = spinner.getFontMetrics(spinner.getFont()); return insets.top + fm.getAscent(); }