Java Code Examples for javax.swing.JSpinner#getEditor()

The following examples show how to use javax.swing.JSpinner#getEditor() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: SpinnerUtil.java    From libreveris with GNU Lesser General Public License v3.0 6 votes vote down vote up
/**
 * Workaround for a swing bug : when the user enters an illegal value, the
 * text is forced to the last value.
 *
 * @param spinner the spinner to update
 */
public static void fixIntegerList (final JSpinner spinner)
{
    JSpinner.DefaultEditor editor;
    editor = (JSpinner.DefaultEditor) spinner.getEditor();

    final JFormattedTextField ftf = editor.getTextField();
    ftf.getInputMap()
            .put(KeyStroke.getKeyStroke("ENTER"), "enterAction");
    ftf.getActionMap()
            .put(
            "enterAction",
            new AbstractAction()
    {
        @Override
        public void actionPerformed (ActionEvent e)
        {
            try {
                spinner.setValue(Integer.parseInt(ftf.getText()));
            } catch (Exception ex) {
                // Reset to last value
                ftf.setText(ftf.getValue().toString());
            }
        }
    });
}
 
Example 2
Source File: bug6421058.java    From openjdk-jdk9 with GNU General Public License v2.0 6 votes vote down vote up
private static void testDefaultFont(final JFrame frame) {
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    JSpinner spinner = new JSpinner();
    frame.add(spinner);
    frame.setSize(300, 100);
    frame.setVisible(true);

    final DefaultEditor editor = (DefaultEditor) spinner.getEditor();
    final Font editorFont = editor.getTextField().getFont();

    /*
     * Validate that the font of the text field is changed to the
     * font of JSpinner if the font of text field was not set by the
     * user.
     */

    if (!(editorFont instanceof UIResource)) {
        throw new RuntimeException("Font must be UIResource");
    }
    if (!editorFont.equals(spinner.getFont())) {
        throw new RuntimeException("Wrong FONT");
    }
}
 
Example 3
Source File: SpinnerUtil.java    From audiveris with GNU Affero General Public License v3.0 6 votes vote down vote up
/**
 * Workaround for a swing bug : when the user enters an illegal value, the
 * text is forced to the last value.
 *
 * @param spinner the spinner to update
 */
public static void fixIntegerList (final JSpinner spinner)
{
    JSpinner.DefaultEditor editor;
    editor = (JSpinner.DefaultEditor) spinner.getEditor();

    final JFormattedTextField ftf = editor.getTextField();
    ftf.getInputMap().put(KeyStroke.getKeyStroke("ENTER"), "enterAction");
    ftf.getActionMap().put("enterAction", new AbstractAction()
                   {
                       @Override
                       public void actionPerformed (ActionEvent e)
                       {
                           try {
                               spinner.setValue(Integer.parseInt(ftf.getText()));
                           } catch (NumberFormatException ex) {
                               // Reset to last value
                               ftf.setText(ftf.getValue().toString());
                           }
                       }
                   });
}
 
Example 4
Source File: SpinnerUtil.java    From audiveris with GNU Affero General Public License v3.0 5 votes vote down vote up
/**
 * Make the spinner text field editable, or not
 *
 * @param spinner the spinner to update
 * @param bool    true if editable, false otherwise
 */
public static void setEditable (JSpinner spinner,
                                boolean bool)
{
    JSpinner.DefaultEditor editor;
    editor = (JSpinner.DefaultEditor) spinner.getEditor();
    editor.getTextField().setEditable(bool);
}
 
Example 5
Source File: TimeSeriesMatrixTopComponent.java    From snap-desktop with GNU General Public License v3.0 5 votes vote down vote up
private void initUI() {
    SnapApp.getDefault().getSelectionSupport(ProductSceneView.class).addHandler(sceneViewListener);

    dateLabel = new JLabel(String.format(DATE_PREFIX + " %s", getStartDateString()));
    matrixSizeSpinner = new JSpinner(new SpinnerNumberModel(MATRIX_DEFAULT_VALUE,
                                                            MATRIX_MINIMUM, MATRIX_MAXIMUM,
                                                            MATRIX_STEP_SIZE));
    final JComponent editor = matrixSizeSpinner.getEditor();
    if (editor instanceof JSpinner.DefaultEditor) {
        ((JSpinner.DefaultEditor) editor).getTextField().setEditable(false);
    }
    matrixSizeSpinner.addChangeListener(e -> matrixModel.setMatrixSize((Integer) matrixSizeSpinner.getModel().getValue()));

    final TableLayout tableLayout = new TableLayout(2);
    tableLayout.setTablePadding(4, 4);
    tableLayout.setTableFill(TableLayout.Fill.BOTH);
    tableLayout.setTableAnchor(TableLayout.Anchor.NORTHWEST);
    tableLayout.setTableWeightX(0.0);
    tableLayout.setTableWeightY(0.0);
    tableLayout.setColumnWeightX(0, 1.0);
    tableLayout.setRowWeightY(1, 1.0);
    tableLayout.setCellColspan(0, 0, 2);

    setLayout(tableLayout);
    setBorder(BorderFactory.createEmptyBorder(4, 4, 4, 4));
    JPanel buttonPanel = createButtonPanel();
    JPanel tablePanel = createTablePanel();
    add(dateLabel);
    add(tablePanel);
    add(buttonPanel);

    setCurrentView(SnapApp.getDefault().getSelectedProductSceneView());
    setDisplayName(Bundle.CTL_TimeSeriesMatrixTopComponentName());
}
 
Example 6
Source File: SpinnerUtil.java    From libreveris with GNU Lesser General Public License v3.0 5 votes vote down vote up
/**
 * Align the spinner display to the right
 *
 * @param spinner the spinner to update
 */
public static void setRightAlignment (JSpinner spinner)
{
    JSpinner.DefaultEditor editor;
    editor = (JSpinner.DefaultEditor) spinner.getEditor();
    editor.getTextField()
            .setHorizontalAlignment(JTextField.RIGHT);
}
 
Example 7
Source File: SpinnerUtil.java    From libreveris with GNU Lesser General Public License v3.0 5 votes vote down vote up
/**
 * Make the spinner text field editable, or not
 *
 * @param spinner the spinner to update
 * @param bool    true if editable, false otherwise
 */
public static void setEditable (JSpinner spinner,
                                boolean bool)
{
    JSpinner.DefaultEditor editor;
    editor = (JSpinner.DefaultEditor) spinner.getEditor();
    editor.getTextField()
            .setEditable(bool);
}
 
Example 8
Source File: SpinnerNextButtonPainter.java    From seaglass with Apache License 2.0 5 votes vote down vote up
private void paintButton(Graphics2D g, JComponent c, int width, int height) {
    boolean useToolBarColors = isInToolBar(c);
    Shape s;

    JSpinner spinner = (JSpinner) c.getParent();
    boolean editorFocused = false;
    JComponent editor = spinner.getEditor();
    if (editor instanceof DefaultEditor) {
        editorFocused = ((DefaultEditor)editor).getTextField().isFocusOwner();
    }
    if (focused || editorFocused) {
        s = createButtonShape(0, 0, width, height, CornerSize.OUTER_FOCUS);
        g.setPaint(getFocusPaint(s, FocusType.OUTER_FOCUS, useToolBarColors));
        g.fill(s);

        s = createButtonShape(0, 1, width - 1, height - 1, CornerSize.INNER_FOCUS);
        g.setPaint(getFocusPaint(s, FocusType.INNER_FOCUS, useToolBarColors));
        g.fill(s);
    }

    s = createButtonShape(0, 2, width - 2, height - 2, CornerSize.BORDER);
    g.setPaint(getSpinnerNextBorderPaint(s, type));
    g.fill(s);

    s = createButtonShape(1, 3, width - 4, height - 4, CornerSize.INTERIOR);
    g.setPaint(getSpinnerNextInteriorPaint(s, type));
    g.fill(s);
}
 
Example 9
Source File: SpinnerPreviousButtonPainter.java    From seaglass with Apache License 2.0 5 votes vote down vote up
private void paintButton(Graphics2D g, JComponent c, int width, int height) {
    boolean useToolBarColors = isInToolBar(c);
    Shape s;

    JSpinner spinner = (JSpinner) c.getParent();
    boolean editorFocused = false;
    JComponent editor = spinner.getEditor();
    if (editor instanceof DefaultEditor) {
        editorFocused = ((DefaultEditor)editor).getTextField().isFocusOwner();
    }
    if (focused || editorFocused) {
        s = createButtonShape(0, 0, width, height, CornerSize.OUTER_FOCUS);
        g.setPaint(getFocusPaint(s, FocusType.OUTER_FOCUS, useToolBarColors));
        g.fill(s);

        s = createButtonShape(0, 0, width - 1, height - 1, CornerSize.INNER_FOCUS);
        g.setPaint(getFocusPaint(s, FocusType.INNER_FOCUS, useToolBarColors));
        g.fill(s);
    }

    s = createButtonShape(0, 0, width - 2, height - 2, CornerSize.BORDER);
    g.setPaint(getSpinnerPrevBorderPaint(s, type));
    g.fill(s);

    s = createButtonShape(1, 1, width - 4, height - 4, CornerSize.INTERIOR);
    g.setPaint(getSpinnerPrevInteriorPaint(s, type));
    g.fill(s);

    s = shapeGenerator.createRectangle(1, 0, width - 4, 1);
    g.setPaint(getSpinnerPrevTopLinePaint(s, type));
    g.fill(s);
}
 
Example 10
Source File: DoubleProperty.java    From triplea with GNU General Public License v3.0 5 votes vote down vote up
@Override
public JComponent getEditorComponent() {
  final JSpinner field = new JSpinner(new SpinnerNumberModel(value, min, max, 1.0));

  // NB: Workaround for JSpinner default sizing algorithm when min/max values have very large
  // magnitudes
  // (see: https://implementsblog.com/2012/11/26/java-gotcha-jspinner-preferred-size/)
  final JComponent fieldEditor = field.getEditor();
  if (fieldEditor instanceof JSpinner.DefaultEditor) {
    ((JSpinner.DefaultEditor) fieldEditor).getTextField().setColumns(10);
  }

  field.addChangeListener(e -> value = (double) field.getValue());
  return field;
}
 
Example 11
Source File: SpinnerUtil.java    From audiveris with GNU Affero General Public License v3.0 5 votes vote down vote up
/**
 * Align the spinner display to the right
 *
 * @param spinner the spinner to update
 */
public static void setRightAlignment (JSpinner spinner)
{
    JSpinner.DefaultEditor editor;
    editor = (JSpinner.DefaultEditor) spinner.getEditor();
    editor.getTextField().setHorizontalAlignment(JTextField.RIGHT);
}
 
Example 12
Source File: CustomClientResizingProfilePanel.java    From plugins with GNU General Public License v3.0 5 votes vote down vote up
private JSpinner createSpinner(int value)
{
	SpinnerModel model = new SpinnerNumberModel(value, Integer.MIN_VALUE, Integer.MAX_VALUE, 1);
	JSpinner spinner = new JSpinner(model);
	Component editor = spinner.getEditor();
	JFormattedTextField spinnerTextField = ((JSpinner.DefaultEditor) editor).getTextField();
	spinnerTextField.setColumns(4);

	return spinner;
}
 
Example 13
Source File: BatchPatternDialog.java    From xdm with GNU General Public License v2.0 5 votes vote down vote up
private void transparentSpinner(JSpinner spTo2) {
	JComponent c = spTo2.getEditor();
	for (int i = 0; i < c.getComponentCount(); i++) {
		Component ct = c.getComponent(i);
		if (ct instanceof JTextField) {
			ct.setForeground(Color.WHITE);
			ct.setBackground(ColorResource.getDarkBtnColor());
		}
	}
	spTo2.setForeground(Color.WHITE);
	spTo2.setBackground(ColorResource.getDarkBgColor());
	spTo2.setBorder(null);

}
 
Example 14
Source File: DateRangeConstraintEditorTest.java    From ghidra with Apache License 2.0 5 votes vote down vote up
private JTextField findEditorForSpinner(Container container, String spinnerName) {
	Component comp = findComponentByName(container, spinnerName);
	if (comp != null) {
		JSpinner spinner = (JSpinner) comp;
		container = spinner.getEditor();
		return (JTextField) findComponentByName(container, "date.spinner.editor");
	}
	return null;
}
 
Example 15
Source File: FloatValueConstraintEditorTest.java    From ghidra with Apache License 2.0 5 votes vote down vote up
private JTextField findEditorForSpinner(Container container, String spinnerName) {
	Component comp = findComponentByName(container, spinnerName);
	if (comp != null) {
		JSpinner lowerSpinner = (JSpinner) comp;
		NumberEditor numberEditor = (NumberEditor) lowerSpinner.getEditor();
		return numberEditor.getTextField();
	}
	return null;
}
 
Example 16
Source File: FloatRangeConstraintEditorTest.java    From ghidra with Apache License 2.0 5 votes vote down vote up
private JTextField findEditorForSpinner(Container container, String spinnerName) {
	Component comp = findComponentByName(container, spinnerName);
	if (comp != null) {
		JSpinner lowerSpinner = (JSpinner) comp;
		NumberEditor numberEditor = (NumberEditor) lowerSpinner.getEditor();
		return numberEditor.getTextField();
	}
	return null;
}
 
Example 17
Source File: WrongEditorTextFieldFont.java    From openjdk-jdk9 with GNU General Public License v2.0 4 votes vote down vote up
private static void testDefaultFont(final JFrame frame) {
    final JSpinner spinner = new JSpinner();
    final JSpinner spinner_u = new JSpinner();
    frame.setLayout(new FlowLayout(FlowLayout.CENTER, 50, 50));
    frame.getContentPane().add(spinner);
    frame.getContentPane().add(spinner_u);
    frame.pack();
    frame.setLocationRelativeTo(null);
    frame.setVisible(true);
    final DefaultEditor ed = (DefaultEditor) spinner.getEditor();
    final DefaultEditor ed_u = (DefaultEditor) spinner_u.getEditor();
    ed_u.getTextField().setFont(USERS_FONT);

    for (int i = 5; i < 40; i += 5) {
        /*
         * Validate that the font of the text field is changed to the
         * font of JSpinner if the font of text field was not set by the
         * user.
         */
        final Font tff = ed.getTextField().getFont();
        if (!(tff instanceof UIResource)) {
            throw new RuntimeException("Font must be UIResource");
        }
        if (spinner.getFont().getSize() != tff.getSize()) {
            throw new RuntimeException("Rrong size");
        }
        spinner.setFont(new Font("dialog", Font.BOLD, i));
        /*
         * Validate that the font of the text field is NOT changed to the
         * font of JSpinner if the font of text field was set by the user.
         */
        final Font tff_u = ed_u.getTextField().getFont();
        if (tff_u instanceof UIResource || !tff_u.equals(USERS_FONT)) {
            throw new RuntimeException("Font must NOT be UIResource");
        }
        if (spinner_u.getFont().getSize() == tff_u.getSize()) {
            throw new RuntimeException("Wrong size");
        }
        spinner_u.setFont(new Font("dialog", Font.BOLD, i));
    }
}