Example usage for javax.swing JSpinner.NumberEditor JSpinner.NumberEditor

List of usage examples for javax.swing JSpinner.NumberEditor JSpinner.NumberEditor

Introduction

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

Prototype

private NumberEditor(JSpinner spinner, DecimalFormat format) 

Source Link

Document

Construct a JSpinner editor that supports displaying and editing the value of a SpinnerNumberModel with a JFormattedTextField.

Usage

From source file:components.SpinnerDemo.java

public SpinnerDemo(boolean cycleMonths) {
    super(new SpringLayout());

    String[] labels = { "Month: ", "Year: ", "Another Date: " };
    int numPairs = labels.length;
    Calendar calendar = Calendar.getInstance();
    JFormattedTextField ftf = null;

    //Add the first label-spinner pair.
    String[] monthStrings = getMonthStrings(); //get month names
    SpinnerListModel monthModel = null;
    if (cycleMonths) { //use custom model
        monthModel = new CyclingSpinnerListModel(monthStrings);
    } else { //use standard model
        monthModel = new SpinnerListModel(monthStrings);
    }//from   w ww  .  jav a2s  .  c  o  m
    JSpinner spinner = addLabeledSpinner(this, labels[0], monthModel);
    //Tweak the spinner's formatted text field.
    ftf = getTextField(spinner);
    if (ftf != null) {
        ftf.setColumns(8); //specify more width than we need
        ftf.setHorizontalAlignment(JTextField.RIGHT);
    }

    //Add second label-spinner pair.
    int currentYear = calendar.get(Calendar.YEAR);
    SpinnerModel yearModel = new SpinnerNumberModel(currentYear, //initial value
            currentYear - 100, //min
            currentYear + 100, //max
            1); //step
    //If we're cycling, hook this model up to the month model.
    if (monthModel instanceof CyclingSpinnerListModel) {
        ((CyclingSpinnerListModel) monthModel).setLinkedModel(yearModel);
    }
    spinner = addLabeledSpinner(this, labels[1], yearModel);
    //Make the year be formatted without a thousands separator.
    spinner.setEditor(new JSpinner.NumberEditor(spinner, "#"));

    //Add the third label-spinner pair.
    Date initDate = calendar.getTime();
    calendar.add(Calendar.YEAR, -100);
    Date earliestDate = calendar.getTime();
    calendar.add(Calendar.YEAR, 200);
    Date latestDate = calendar.getTime();
    SpinnerModel dateModel = new SpinnerDateModel(initDate, earliestDate, latestDate, Calendar.YEAR);//ignored for user input
    spinner = addLabeledSpinner(this, labels[2], dateModel);
    spinner.setEditor(new JSpinner.DateEditor(spinner, "MM/yyyy"));

    //Lay out the panel.
    SpringUtilities.makeCompactGrid(this, numPairs, 2, //rows, cols
            10, 10, //initX, initY
            6, 10); //xPad, yPad
}

From source file:components.SpinnerDemo3.java

public SpinnerDemo3(boolean cycleMonths) {
    super(new SpringLayout());

    String[] labels = { "Month: ", "Year: ", "Another Date: " };
    int numPairs = labels.length;
    calendar = Calendar.getInstance();
    JFormattedTextField ftf = null;

    //Add the first label-spinner pair.
    String[] monthStrings = getMonthStrings(); //get month names
    SpinnerListModel monthModel = null;
    if (cycleMonths) { //use custom model
        monthModel = new CyclingSpinnerListModel(monthStrings);
    } else { //use standard model
        monthModel = new SpinnerListModel(monthStrings);
    }/*from   ww  w. j  a  va 2 s  . c  o m*/
    JSpinner spinner = addLabeledSpinner(this, labels[0], monthModel);
    //Tweak the spinner's formatted text field.
    ftf = getTextField(spinner);
    if (ftf != null) {
        ftf.setColumns(8); //specify more width than we need
        ftf.setHorizontalAlignment(JTextField.RIGHT);
    }

    //Add second label-spinner pair.
    int currentYear = calendar.get(Calendar.YEAR);
    SpinnerModel yearModel = new SpinnerNumberModel(currentYear, //initial value
            currentYear - 100, //min
            currentYear + 100, //max
            1); //step
    //If we're cycling, hook this model up to the month model.
    if (monthModel instanceof CyclingSpinnerListModel) {
        ((CyclingSpinnerListModel) monthModel).setLinkedModel(yearModel);
    }
    spinner = addLabeledSpinner(this, labels[1], yearModel);
    //Make the year be formatted without a thousands separator.
    spinner.setEditor(new JSpinner.NumberEditor(spinner, "#"));

    //Add the third label-spinner pair.
    Date initDate = calendar.getTime();
    calendar.add(Calendar.YEAR, -100);
    Date earliestDate = calendar.getTime();
    calendar.add(Calendar.YEAR, 200);
    Date latestDate = calendar.getTime();
    SpinnerDateModel dateModel = new SpinnerDateModel(initDate, earliestDate, latestDate, Calendar.YEAR);//ignored for user input
    dateSpinner = spinner = addLabeledSpinner(this, labels[2], dateModel);
    spinner.setEditor(new JSpinner.DateEditor(spinner, "MM/yyyy"));
    //Tweak the spinner's formatted text field.
    ftf = getTextField(spinner);
    if (ftf != null) {
        ftf.setHorizontalAlignment(JTextField.RIGHT);
        ftf.setBorder(BorderFactory.createEmptyBorder(1, 1, 1, 3));
    }
    spinner.setBorder(BorderFactory.createLineBorder(Color.BLACK, 1));
    //XXX: No easy way to get to the buttons and change their border.
    setSeasonalColor(dateModel.getDate()); //initialize color

    //Listen for changes on the date spinner.
    dateSpinner.addChangeListener(this);

    //Lay out the panel.
    SpringUtilities.makeCompactGrid(this, numPairs, 2, //rows, cols
            10, 10, //initX, initY
            6, 10); //xPad, yPad
}

From source file:com.vladsch.idea.multimarkdown.settings.MultiMarkdownSettingsPanel.java

private void createUIComponents() {
    // TODO: place custom component creation code here
    // create the css themes combobox, make it locale aware
    ArrayList<String> options = new ArrayList<String>(10);
    for (int i = 0;; i++) {
        String message = MultiMarkdownBundle.messageOrBlank("settings.html-theme-" + (i + 1));
        if (message.isEmpty())
            break;
        options.add(message);/*from  w ww .j av a2s  .  co  m*/
    }

    // we use the list to report but combo box if available
    htmlThemeList = new JBList(options);
    htmlThemeList.setSelectedIndex(2);

    htmlThemeComboBox = new JLabel();
    htmlThemeComboBox.setVisible(false);
    haveCustomizableEditor = false;
    try {
        htmlThemeComboBox = new ComboBox(options.toArray(new String[options.size()]));
        ((JComboBox) htmlThemeComboBox).setSelectedIndex(2);
        htmlThemeList.setVisible(false);
        haveCustomizableEditor = true;
    } catch (NoSuchMethodError e) {
        // does not exist, use list box
    } catch (NoClassDefFoundError e) {
        // does not exist, use list box
    }

    // create the CSS text edit control
    Language language = Language.findLanguageByID("CSS");
    final boolean foundCSS = language != null;

    final FileType fileType = language != null && language.getAssociatedFileType() != null
            ? language.getAssociatedFileType()
            : StdFileTypes.PLAIN_TEXT;

    // Set zoom to 0.1 increments
    final SpinnerNumberModel model = new SpinnerNumberModel(1.0, 0.2, 5.0, 0.01);
    pageZoomSpinner = new JSpinner(model);
    JSpinner.NumberEditor decimalFormat = new JSpinner.NumberEditor(pageZoomSpinner, "0.00");

    CustomizableEditorTextField.EditorCustomizationListener listener = new CustomizableEditorTextField.EditorCustomizationListener() {
        @Override
        public boolean editorCreated(@NotNull EditorEx editor, @NotNull Project project) {
            EditorSettings settings = editor.getSettings();
            settings.setRightMarginShown(true);
            //settings.setRightMargin(-1);
            if (foundCSS)
                settings.setFoldingOutlineShown(true);
            settings.setLineNumbersShown(true);
            if (foundCSS)
                settings.setLineMarkerAreaShown(true);
            settings.setIndentGuidesShown(true);
            settings.setVirtualSpace(true);

            //settings.setWheelFontChangeEnabled(false);
            editor.setHorizontalScrollbarVisible(true);
            editor.setVerticalScrollbarVisible(true);

            FileType fileTypeH = FileTypeManagerEx.getInstanceEx().getFileTypeByExtension(".css");
            FileType highlighterFileType = foundCSS ? fileType : fileTypeH;
            if (highlighterFileType != null && project != null) {
                editor.setHighlighter(HighlighterFactory.createHighlighter(project, highlighterFileType));
            }

            int lineCursorWidth = 2;
            if (haveCustomizableEditor) {
                // get the standard caret width from the registry
                try {
                    RegistryValue value = Registry.get("editor.caret.width");
                    if (value != null) {
                        lineCursorWidth = value.asInteger();
                    }
                } catch (Exception ex) {
                    // ignore
                }

                focusEditorButton
                        .setEnabled(((CustomizableEditorTextField) textCustomCss).haveSavedState(editor));
            }
            settings.setLineCursorWidth(lineCursorWidth);

            return false;
        }
    };

    if (!haveCustomizableEditor) {
        Project project = CustomizableEditorTextField.getAnyProject(null, true);
        Document document = CustomizableEditorTextField.createDocument("", fileType, project,
                new CustomizableEditorTextField.SimpleDocumentCreator());
        textCustomCss = new CustomizableLanguageEditorTextField(document, project, fileType, false, false);
        textCustomCss.setFontInheritedFromLAF(false);
        ((CustomizableLanguageEditorTextField) textCustomCss).registerListener(listener);
        //focusEditorButton.setEnabled(false);
    } else {
        // we pass a null project because we don't have one, the control will grab any project so that
        // undo works properly in the edit control.
        Project project = CustomizableEditorTextField.getAnyProject(null, true);
        textCustomCss = new CustomizableEditorTextField(fileType, project, "", false);
        textCustomCss.setFontInheritedFromLAF(false);
        ((CustomizableEditorTextField) textCustomCss).registerListener(listener);
    }
}