Example usage for javax.swing JSpinner getEditor

List of usage examples for javax.swing JSpinner getEditor

Introduction

In this page you can find the example usage for javax.swing JSpinner getEditor.

Prototype

public JComponent getEditor() 

Source Link

Document

Returns the component that displays and potentially changes the model's value.

Usage

From source file:Main.java

public static void main(String[] argv) throws Exception {
    Calendar calendar = new GregorianCalendar();
    calendar.set(Calendar.HOUR_OF_DAY, 13); // 1pm

    SpinnerDateModel dateModel = new SpinnerDateModel(calendar.getTime(), null, null, Calendar.HOUR_OF_DAY);
    JSpinner spinner = new JSpinner(dateModel);

    JFormattedTextField tf = ((JSpinner.DefaultEditor) spinner.getEditor()).getTextField();
    DefaultFormatterFactory factory = (DefaultFormatterFactory) tf.getFormatterFactory();
    DateFormatter formatter = (DateFormatter) factory.getDefaultFormatter();

    // Change the date format to only show the hours
    formatter.setFormat(new SimpleDateFormat("hh:00 a"));
    //formatter.setFormat(new SimpleDateFormat("HH:00 a"));
}

From source file:Main.java

public static void main(String[] argv) throws Exception {
    JSpinner spinner = new JSpinner();

    // Get the text field
    JFormattedTextField tf = ((JSpinner.DefaultEditor) spinner.getEditor()).getTextField();

    // Set the margin (add two spaces to the left and right side of the value)
    int top = 0;/*from  ww  w. j  av a2  s.  co  m*/
    int left = 2;
    int bottom = 0;
    int right = 2;
    Insets insets = new Insets(top, left, bottom, right);
    tf.setMargin(insets);

}

From source file:Main.java

public static void main(String[] argv) throws Exception {
    JSpinner spinner = new JSpinner();

    // Disable keyboard edits in the spinner
    JFormattedTextField tf = ((JSpinner.DefaultEditor) spinner.getEditor()).getTextField();
    tf.setEditable(false);/* w  w w . ja va  2  s. c  o  m*/
    tf.setBackground(Color.white);

    // The value of the spinner can still be programmatically changed
    spinner.setValue(new Integer(100));
}

From source file:Main.java

public static DefaultFormatter getSpinnerFormatter(JSpinner spinner) {
    JComponent comp = spinner.getEditor();
    JFormattedTextField field = (JFormattedTextField) comp.getComponent(0);
    return (DefaultFormatter) field.getFormatter();
}

From source file:Main.java

/**
 * Installs a workaround for bug #4699955 in a JSpinner.
 * // www.  j  a v a  2s.c o  m
 * @param spinner
 *            The spinner to fix
 */
public static void installSpinnerBugWorkaround(final JSpinner spinner) {
    ((DefaultEditor) spinner.getEditor()).getTextField().addFocusListener(new FocusAdapter() {
        @Override
        public void focusGained(final FocusEvent e) {
            if (e.getSource() instanceof JTextComponent) {
                final JTextComponent text = ((JTextComponent) e.getSource());
                SwingUtilities.invokeLater(new Runnable() {
                    @Override
                    public void run() {
                        text.selectAll();
                    }
                });
            }
        }
    });
    spinner.addFocusListener(new FocusAdapter() {
        @Override
        public void focusGained(final FocusEvent e) {
            if (e.getSource() instanceof JSpinner) {
                final JTextComponent text = ((DefaultEditor) ((JSpinner) e.getSource()).getEditor())
                        .getTextField();
                SwingUtilities.invokeLater(new Runnable() {
                    @Override
                    public void run() {
                        text.requestFocus();
                    }
                });
            }
        }
    });
}

From source file:Main.java

/**
 * Installs a workaround for bug #4699955 in a JSpinner.
 *
 * @param spinner/* w  ww.  j a v  a 2  s .  c o m*/
 *            The spinner to fix
 */

public static void installSpinnerBugWorkaround(final JSpinner spinner) {
    ((DefaultEditor) spinner.getEditor()).getTextField().addFocusListener(new FocusAdapter() {
        @Override
        public void focusGained(final FocusEvent e) {
            if (e.getSource() instanceof JTextComponent) {
                final JTextComponent text = (JTextComponent) e.getSource();
                SwingUtilities.invokeLater(new Runnable() {
                    @Override
                    public void run() {
                        text.selectAll();
                    }
                });
            }
        }
    });
    spinner.addFocusListener(new FocusAdapter() {
        @Override
        public void focusGained(final FocusEvent e) {
            if (e.getSource() instanceof JSpinner) {
                final JTextComponent text = ((DefaultEditor) ((JSpinner) e.getSource()).getEditor())
                        .getTextField();
                SwingUtilities.invokeLater(new Runnable() {
                    @Override
                    public void run() {
                        text.requestFocus();
                    }
                });
            }
        }
    });
}

From source file:Main.java

private static JSpinner makeDigitsOnlySpinnerUsingDocumentFilter() {
    JSpinner spinner = new JSpinner(new SpinnerNumberModel());
    JSpinner.NumberEditor jsEditor = (JSpinner.NumberEditor) spinner.getEditor();
    JFormattedTextField textField = jsEditor.getTextField();
    DocumentFilter digitOnlyFilter = new DocumentFilter() {
        @Override/*from   ww w.  ja  v  a 2  s. c o m*/
        public void insertString(FilterBypass fb, int offset, String string, AttributeSet attr)
                throws BadLocationException {
            if (stringContainsOnlyDigits(string)) {
                super.insertString(fb, offset, string, attr);
            }
        }

        @Override
        public void remove(FilterBypass fb, int offset, int length) throws BadLocationException {
            super.remove(fb, offset, length);
        }

        @Override
        public void replace(FilterBypass fb, int offset, int length, String text, AttributeSet attrs)
                throws BadLocationException {
            if (stringContainsOnlyDigits(text)) {
                super.replace(fb, offset, length, text, attrs);
            }
        }

        private boolean stringContainsOnlyDigits(String text) {
            for (int i = 0; i < text.length(); i++) {
                if (!Character.isDigit(text.charAt(i))) {
                    return false;
                }
            }
            return true;
        }
    };

    NumberFormat format = NumberFormat.getPercentInstance();
    format.setGroupingUsed(false);
    format.setGroupingUsed(true);
    format.setMaximumIntegerDigits(10);
    format.setMaximumFractionDigits(2);
    format.setMinimumFractionDigits(5);
    textField.setFormatterFactory(new DefaultFormatterFactory(new InternationalFormatter(format) {
        @Override
        protected DocumentFilter getDocumentFilter() {
            return digitOnlyFilter;
        }
    }));
    return spinner;
}

From source file:Main.java

public Main() {
    super("JSpinner Icon Test");
    setSize(300, 80);/*from w w w  .j  a v a 2 s .co m*/
    setDefaultCloseOperation(EXIT_ON_CLOSE);

    Container c = getContentPane();
    c.setLayout(new GridLayout(0, 2));

    Icon nums[] = new Icon[] { new ImageIcon("1.gif"), new ImageIcon("2.gif"), new ImageIcon("3.gif"),
            new ImageIcon("4.gif"), new ImageIcon("5.gif"), new ImageIcon("6.gif") };
    JSpinner s1 = new JSpinner(new SpinnerListModel(nums));
    s1.setEditor(new IconEditor(s1));

    System.out.println(s1.getEditor());

    c.add(new JLabel(" Icon Spinner"));
    c.add(s1);

    setVisible(true);
}

From source file:components.SpinnerDemo.java

/**
 * Return the formatted text field used by the editor, or
 * null if the editor doesn't descend from JSpinner.DefaultEditor.
 *///from w  w  w. ja  va 2s . c  om
public JFormattedTextField getTextField(JSpinner spinner) {
    JComponent editor = spinner.getEditor();
    if (editor instanceof JSpinner.DefaultEditor) {
        return ((JSpinner.DefaultEditor) editor).getTextField();
    } else {
        System.err.println("Unexpected editor type: " + spinner.getEditor().getClass()
                + " isn't a descendant of DefaultEditor");
        return null;
    }
}

From source file:TextInputDemo.java

public JFormattedTextField getTextField(JSpinner spinner) {
    JComponent editor = spinner.getEditor();
    if (editor instanceof JSpinner.DefaultEditor) {
        return ((JSpinner.DefaultEditor) editor).getTextField();
    } else {//w  w w  . j a  v a 2  s  . c om
        System.err.println("Unexpected editor type: " + spinner.getEditor().getClass()
                + " isn't a descendant of DefaultEditor");
        return null;
    }
}