Java Code Examples for javax.swing.JSpinner#DefaultEditor

The following examples show how to use javax.swing.JSpinner#DefaultEditor . 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: 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 3
Source File: CgSpinnerDouble.java    From Course_Generator with GNU General Public License v3.0 6 votes vote down vote up
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);
}
 
Example 4
Source File: CgSpinner.java    From Course_Generator with GNU General Public License v3.0 6 votes vote down vote up
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);
}
 
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: FlatBorder.java    From FlatLaf with Apache License 2.0 5 votes vote down vote up
protected boolean isFocused( Component c ) {
	if( c instanceof JScrollPane ) {
		JViewport viewport = ((JScrollPane)c).getViewport();
		Component view = (viewport != null) ? viewport.getView() : null;
		if( view != null ) {
			if( FlatUIUtils.isPermanentFocusOwner( view ) )
				return true;

			if( (view instanceof JTable && ((JTable)view).isEditing()) ||
				(view instanceof JTree && ((JTree)view).isEditing()) )
			{
				Component focusOwner = KeyboardFocusManager.getCurrentKeyboardFocusManager().getFocusOwner();
				if( focusOwner != null )
					return SwingUtilities.isDescendingFrom( focusOwner, view );
			}
		}
		return false;
	} else if( c instanceof JComboBox && ((JComboBox<?>)c).isEditable() ) {
		Component editorComponent = ((JComboBox<?>)c).getEditor().getEditorComponent();
		return (editorComponent != null) ? FlatUIUtils.isPermanentFocusOwner( editorComponent ) : false;
	} else if( c instanceof JSpinner ) {
		if( FlatUIUtils.isPermanentFocusOwner( c ) )
			return true;

		JComponent editor = ((JSpinner)c).getEditor();
		if( editor instanceof JSpinner.DefaultEditor ) {
			JTextField textField = ((JSpinner.DefaultEditor)editor).getTextField();
			if( textField != null )
				return FlatUIUtils.isPermanentFocusOwner( textField );
		}
		return false;
	} else
		return FlatUIUtils.isPermanentFocusOwner( c );
}
 
Example 7
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 8
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 9
Source File: IntegerListSpinner.java    From libreveris with GNU Lesser General Public License v3.0 5 votes vote down vote up
/**
 * Creates a new IntegerListSpinner object.
 */
public IntegerListSpinner ()
{
    setModel(new SpinnerListModel());

    // Right alignment
    JSpinner.DefaultEditor editor;
    editor = (JSpinner.DefaultEditor) getEditor();
    editor.getTextField()
            .setHorizontalAlignment(JTextField.RIGHT);
}
 
Example 10
Source File: DropAmountChooser.java    From stendhal with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Get the editable text field component of the spinner.
 *
 * @return text field
 */
private JTextField getTextField() {
	// There really seems to be no simpler way to do this
	JComponent editor = spinner.getEditor();
	if (editor instanceof JSpinner.DefaultEditor) {
		return ((JSpinner.DefaultEditor) editor).getTextField();
	} else {
		Logger.getLogger(DropAmountChooser.class).error("Unknown editor type", new Throwable());
		// This will not work, but at least it won't crash the client
		return new JTextField();
	}
}
 
Example 11
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 12
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 13
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 14
Source File: IntegerListSpinner.java    From audiveris with GNU Affero General Public License v3.0 5 votes vote down vote up
/**
 * Creates a new IntegerListSpinner object.
 */
public IntegerListSpinner ()
{
    setModel(new SpinnerListModel());

    // Right alignment
    JSpinner.DefaultEditor editor;
    editor = (JSpinner.DefaultEditor) getEditor();
    editor.getTextField().setHorizontalAlignment(JTextField.RIGHT);
}
 
Example 15
Source File: RDefaultEditor.java    From marathonv5 with Apache License 2.0 5 votes vote down vote up
@Override
public String getText() {
    if (component instanceof JSpinner.DefaultEditor) {
        String text = ((JSpinner.DefaultEditor) component).getTextField().getText();
        return text;
    }
    return null;
}
 
Example 16
Source File: RSpinner.java    From marathonv5 with Apache License 2.0 5 votes vote down vote up
private String getSpinnerText() {
    JComponent editor = ((JSpinner) component).getEditor();

    if (editor == null) {
    } else {
        RComponentFactory finder = new RComponentFactory(omapConfig);
        if (editor instanceof JSpinner.DefaultEditor) {
            RComponent rComponent = finder.findRawRComponent(editor, null, recorder);
            return rComponent.getText();
        }
    }
    return null;
}
 
Example 17
Source File: JSpinnerJavaElement.java    From marathonv5 with Apache License 2.0 5 votes vote down vote up
private Component getEditor() {
    JComponent editorComponent = ((JSpinner) component).getEditor();
    if (editorComponent == null) {
        throw new JavaAgentException("Null value returned by getEditor() on spinner", null);
    }
    if (editorComponent instanceof JSpinner.DefaultEditor) {
        editorComponent = ((JSpinner.DefaultEditor) editorComponent).getTextField();
    }
    return editorComponent;
}
 
Example 18
Source File: FlatSpinnerUI.java    From FlatLaf with Apache License 2.0 4 votes vote down vote up
private static JTextField getEditorTextField( JComponent editor ) {
	return editor instanceof JSpinner.DefaultEditor
		? ((JSpinner.DefaultEditor)editor).getTextField()
		: null;
}