當前位置: 首頁>>代碼示例>>Java>>正文


Java JFormattedTextField.setFormatterFactory方法代碼示例

本文整理匯總了Java中javax.swing.JFormattedTextField.setFormatterFactory方法的典型用法代碼示例。如果您正苦於以下問題:Java JFormattedTextField.setFormatterFactory方法的具體用法?Java JFormattedTextField.setFormatterFactory怎麽用?Java JFormattedTextField.setFormatterFactory使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在javax.swing.JFormattedTextField的用法示例。


在下文中一共展示了JFormattedTextField.setFormatterFactory方法的11個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。

示例1: IntegerEditor

import javax.swing.JFormattedTextField; //導入方法依賴的package包/類
/**
 * Construct a <code>JSpinner</code> editor that supports displaying and
 * editing the value of a <code>SpinnerIntModel</code> with a
 * <code>JFormattedTextField</code>. <code>This</code>
 * <code>IntegerEditor</code> becomes both a <code>ChangeListener</code>
 * on the spinner and a <code>PropertyChangeListener</code> on the new
 * <code>JFormattedTextField</code>.
 *
 * @param spinner the spinner whose model <code>this</code> editor will
 *            monitor
 * @param format the <code>NumberFormat</code> object that's used to
 *            display and parse the value of the text field.
 * @exception IllegalArgumentException if the spinners model is not an
 *                instance of <code>SpinnerIntModel</code>
 *
 * @see #getTextField
 * @see SpinnerIntModel
 * @see java.text.DecimalFormat
 */
private IntegerEditor(JSpinner spinner, NumberFormat format) {
	super(spinner);
	if (!(spinner.getModel() instanceof SpinnerIntModel)) {
		throw new IllegalArgumentException("model not a SpinnerIntModel");
	}

	format.setGroupingUsed(false);
	format.setMaximumFractionDigits(0);
	SpinnerIntModel model = (SpinnerIntModel) spinner.getModel();
	NumberFormatter formatter = new IntegerEditorFormatter(model, format);
	DefaultFormatterFactory factory = new DefaultFormatterFactory(formatter);
	JFormattedTextField ftf = getTextField();
	ftf.setEditable(true);
	ftf.setFormatterFactory(factory);
	ftf.setHorizontalAlignment(JTextField.RIGHT);

	try {
		String minString = formatter.valueToString(model.getMinimum());
		String maxString = formatter.valueToString(model.getMaximum());
		// Trying to approximate the width difference between "m" and "0" by multiplying with 0.7
		ftf.setColumns((int) Math.round(0.7 * Math.max(maxString.length(), minString.length())));
	} catch (ParseException e) {
		// Nothing to do, the component width will simply be the default
	}
}
 
開發者ID:DigitalMediaServer,項目名稱:DigitalMediaServer,代碼行數:45,代碼來源:CustomJSpinner.java

示例2: setupIntegerFieldTrackingValue

import javax.swing.JFormattedTextField; //導入方法依賴的package包/類
/**
 * Sets integer number format to JFormattedTextField instance,
 * sets value of JFormattedTextField instance to object's field value,
 * synchronizes object's field value with the value of JFormattedTextField instance.
 *
 * @param textField  JFormattedTextField instance
 * @param owner      an object whose field is synchronized with {@code textField}
 * @param property   object's field name for synchronization
 */
public static void setupIntegerFieldTrackingValue(final JFormattedTextField textField,
                                                  final InspectionProfileEntry owner,
                                                  final String property) {
    NumberFormat formatter = NumberFormat.getIntegerInstance();
    formatter.setParseIntegerOnly(true);
    textField.setFormatterFactory(new DefaultFormatterFactory(new NumberFormatter(formatter)));
    textField.setValue(getPropertyValue(owner, property));
    final Document document = textField.getDocument();
    document.addDocumentListener(new DocumentAdapter() {
        @Override
        public void textChanged(DocumentEvent e) {
            try {
                textField.commitEdit();
                setPropertyValue(owner, property,
                        ((Number) textField.getValue()).intValue());
            } catch (ParseException e1) {
                // No luck this time
            }
        }
    });
}
 
開發者ID:jskierbi,項目名稱:intellij-ce-playground,代碼行數:31,代碼來源:SingleIntegerFieldOptionsPanel.java

示例3: IntegerEditor

import javax.swing.JFormattedTextField; //導入方法依賴的package包/類
public IntegerEditor(int min, int max) {
    super(new JFormattedTextField());
    ftf = (JFormattedTextField) getComponent();
    minimum = new Integer(min);
    maximum = new Integer(max);

    // Set up the editor for the integer cells.
    integerFormat = NumberFormat.getIntegerInstance();
    NumberFormatter intFormatter = new NumberFormatter(integerFormat);
    intFormatter.setFormat(integerFormat);
    intFormatter.setMinimum(minimum);
    intFormatter.setMaximum(maximum);

    ftf.setFormatterFactory(new DefaultFormatterFactory(intFormatter));
    ftf.setValue(minimum);
    ftf.setHorizontalAlignment(JTextField.TRAILING);
    ftf.setFocusLostBehavior(JFormattedTextField.PERSIST);

    // React when the user presses Enter while the editor is
    // active. (Tab is handled as specified by
    // JFormattedTextField's focusLostBehavior property.)
    ftf.getInputMap().put(KeyStroke.getKeyStroke(KeyEvent.VK_ENTER, 0), "check");
    ftf.getActionMap().put("check", new AbstractAction() {
        public void actionPerformed(ActionEvent e) {
            if (!ftf.isEditValid()) { // The text is invalid.
                if (userSaysRevert()) { // reverted
                    ftf.postActionEvent(); // inform the editor
                }
            } else
                try { // The text is valid,
                    ftf.commitEdit(); // so use it.
                    ftf.postActionEvent(); // stop editing
                } catch (java.text.ParseException exc) {
                }
        }
    });
}
 
開發者ID:jalian-systems,項目名稱:marathonv5,代碼行數:38,代碼來源:IntegerEditor.java

示例4: DateCellEditor

import javax.swing.JFormattedTextField; //導入方法依賴的package包/類
/**
 * Constructor.
 */
public DateCellEditor(DateFormat dateFormat) {
    super(new JFormattedTextField());
    textField = (JFormattedTextField) getComponent();

    this.dateFormat = dateFormat;
    DateFormatter dateFormatter = new DateFormatter(dateFormat);

    textField.setFormatterFactory(new DefaultFormatterFactory(dateFormatter));
    textField.setHorizontalAlignment(JTextField.TRAILING);
    textField.setFocusLostBehavior(JFormattedTextField.PERSIST);

    // React when the user presses Enter while the editor is
    // active.  (Tab is handled as specified by
    // JFormattedTextField's focusLostBehavior property.)
    textField.getInputMap().put(KeyStroke.getKeyStroke(KeyEvent.VK_ENTER, 0), "check");
    textField.getActionMap().put("check", new AbstractAction() {

        @Override
        public void actionPerformed(ActionEvent e) {
            if (!textField.isEditValid()) { //The text is invalid.
                Toolkit.getDefaultToolkit().beep();
                textField.selectAll();
            } else {
                try {              //The text is valid,
                    textField.commitEdit();     //so use it.
                    textField.postActionEvent(); //stop editing
                } catch (java.text.ParseException ex) {
                }
            }
        }
    });
}
 
開發者ID:takun2s,項目名稱:smile_1.5.0_java7,代碼行數:36,代碼來源:DateCellEditor.java

示例5: init

import javax.swing.JFormattedTextField; //導入方法依賴的package包/類
static void init(int length, boolean hex, JFormattedTextField text) {
    ValueFormatter formatter = new ValueFormatter(length, hex);
    text.setColumns(length);
    text.setFormatterFactory(new DefaultFormatterFactory(formatter));
    text.setHorizontalAlignment(SwingConstants.RIGHT);
    text.setMinimumSize(text.getPreferredSize());
    text.addFocusListener(formatter);
}
 
開發者ID:SunburstApps,項目名稱:OpenJSharp,代碼行數:9,代碼來源:ValueFormatter.java

示例6: HexaEditor

import javax.swing.JFormattedTextField; //導入方法依賴的package包/類
HexaEditor (JSpinner spinner)
{
    super(spinner);

    JFormattedTextField ftf = getTextField();
    ftf.setEditable(true);
    ftf.setFormatterFactory(new HexaFormatterFactory());
}
 
開發者ID:Audiveris,項目名稱:audiveris,代碼行數:9,代碼來源:LHexaSpinner.java

示例7: CellTypeTextFieldDefaultImpl

import javax.swing.JFormattedTextField; //導入方法依賴的package包/類
/**
 * Creates a {@link JFormattedTextField} for the specified cell. If a formatter is given, will
 * apply it to the field. Does not validate the model, so make sure this call works!
 * 
 * @param model
 * @param rowIndex
 * @param columnIndex
 * @param cellClass
 * @param formatter
 *            the formatter or <code>null</code> if none is required
 * @param hideUnavailableContentAssist
 * @return
 */
public CellTypeTextFieldDefaultImpl(final TablePanelModel model, final int rowIndex, final int columnIndex,
		final Class<? extends CellType> cellClass, AbstractFormatter formatter, boolean hideUnavailableContentAssist) {
	super();

	final JFormattedTextField field = CellTypeImplHelper.createFormattedTextField(model, rowIndex, columnIndex);
	setLayout(new BorderLayout());
	add(field, BorderLayout.CENTER);

	// otherwise 'null' would be restored
	Object value = model.getValueAt(rowIndex, columnIndex);
	String text = value != null ? String.valueOf(value) : "";

	// specical handling when formatter is given
	if (formatter != null) {
		field.setFormatterFactory(new DefaultFormatterFactory(formatter));
	}
	field.setText(text);

	// set syntax assist if available
	String syntaxHelp = model.getSyntaxHelpAt(rowIndex, columnIndex);
	if (syntaxHelp != null && !"".equals(syntaxHelp.trim())) {
		PromptSupport.setForeground(Color.LIGHT_GRAY, field);
		PromptSupport.setPrompt(syntaxHelp, field);
		PromptSupport.setFontStyle(Font.ITALIC, field);
		PromptSupport.setFocusBehavior(FocusBehavior.SHOW_PROMPT, field);
	}

	// see if content assist is possible for this field, if so add it
	ImageIcon icon = SwingTools.createIcon("16/"
			+ I18N.getMessageOrNull(I18N.getGUIBundle(), "gui.action.content_assist.icon"));
	JButton contentAssistButton = new JButton();
	contentAssistButton.setIcon(icon);
	if (field.isEnabled() && model.isContentAssistPossibleForCell(rowIndex, columnIndex)) {
		contentAssistButton.setToolTipText(I18N.getMessageOrNull(I18N.getGUIBundle(),
				"gui.action.content_assist_enabled.tip"));
		CellTypeImplHelper.addContentAssist(model, rowIndex, columnIndex, field, contentAssistButton, cellClass);
	} else {
		contentAssistButton.setToolTipText(I18N.getMessageOrNull(I18N.getGUIBundle(),
				"gui.action.content_assist_disabled.tip"));
		contentAssistButton.setEnabled(false);
	}
	if (contentAssistButton.isEnabled() || (!contentAssistButton.isEnabled() && !hideUnavailableContentAssist)) {
		add(contentAssistButton, BorderLayout.EAST);
	}

	// set size so panels don't grow larger when they get the chance
	setPreferredSize(new Dimension(300, 20));
	setMinimumSize(new Dimension(100, 15));
	setMaximumSize(new Dimension(1600, 30));
}
 
開發者ID:transwarpio,項目名稱:rapidminer,代碼行數:64,代碼來源:CellTypeTextFieldDefaultImpl.java

示例8: IntegerCellEditor

import javax.swing.JFormattedTextField; //導入方法依賴的package包/類
/**
 * Constructor.
 * @param min the minimum of valid values.
 * @param max the maximum of valid values. 
 */
public IntegerCellEditor(int min, int max) {
    super(new JFormattedTextField());
    textField = (JFormattedTextField) getComponent();
    minimum = new Integer(min);
    maximum = new Integer(max);

    // Set up the editor for the integer cells.
    integerFormat = NumberFormat.getIntegerInstance();
    NumberFormatter intFormatter = new NumberFormatter(integerFormat);
    intFormatter.setFormat(integerFormat);
    intFormatter.setOverwriteMode(false);
    intFormatter.setMinimum(minimum);
    intFormatter.setMaximum(maximum);

    textField.setFormatterFactory(new DefaultFormatterFactory(intFormatter));
    textField.setValue(minimum);
    textField.setHorizontalAlignment(JTextField.TRAILING);
    textField.setFocusLostBehavior(JFormattedTextField.PERSIST);

    // React when the user presses Enter while the editor is
    // active.  (Tab is handled as specified by
    // JFormattedTextField's focusLostBehavior property.)
    textField.getInputMap().put(KeyStroke.getKeyStroke(KeyEvent.VK_ENTER, 0), "check");
    textField.getActionMap().put("check", new AbstractAction() {

        @Override
        public void actionPerformed(ActionEvent e) {
            if (!textField.isEditValid()) { //The text is invalid.
                Toolkit.getDefaultToolkit().beep();
                textField.selectAll();
            } else {
                try {              //The text is valid,
                    textField.commitEdit();     //so use it.
                    textField.postActionEvent(); //stop editing
                } catch (java.text.ParseException ex) {
                }
            }
        }
    });
}
 
開發者ID:takun2s,項目名稱:smile_1.5.0_java7,代碼行數:46,代碼來源:IntegerCellEditor.java

示例9: DoubleCellEditor

import javax.swing.JFormattedTextField; //導入方法依賴的package包/類
/**
 * Constructor.
 * @param min the minimum of valid values.
 * @param max the maximum of valid values. 
 */
public DoubleCellEditor(double min, double max) {
    super(new JFormattedTextField());
    textField = (JFormattedTextField) getComponent();
    minimum = new Double(min);
    maximum = new Double(max);

    // Set up the editor for the double cells.
    doubleFormat = NumberFormat.getNumberInstance();
    NumberFormatter doubleFormatter = new NumberFormatter(doubleFormat);
    doubleFormatter.setFormat(doubleFormat);
    doubleFormatter.setOverwriteMode(false);
    doubleFormatter.setMinimum(minimum);
    doubleFormatter.setMaximum(maximum);

    textField.setFormatterFactory(new DefaultFormatterFactory(doubleFormatter));
    textField.setValue(minimum);
    textField.setHorizontalAlignment(JTextField.TRAILING);
    textField.setFocusLostBehavior(JFormattedTextField.PERSIST);

    // React when the user presses Enter while the editor is
    // active.  (Tab is handled as specified by
    // JFormattedTextField's focusLostBehavior property.)
    textField.getInputMap().put(KeyStroke.getKeyStroke(KeyEvent.VK_ENTER, 0), "check");
    textField.getActionMap().put("check", new AbstractAction() {

        @Override
        public void actionPerformed(ActionEvent e) {
            if (!textField.isEditValid()) { //The text is invalid.
                Toolkit.getDefaultToolkit().beep();
                textField.selectAll();
            } else {
                try {              //The text is valid,
                    textField.commitEdit();     //so use it.
                    textField.postActionEvent(); //stop editing
                } catch (java.text.ParseException ex) {
                }
            }
        }
    });
}
 
開發者ID:takun2s,項目名稱:smile_1.5.0_java7,代碼行數:46,代碼來源:DoubleCellEditor.java

示例10: CameraOptionsPanel

import javax.swing.JFormattedTextField; //導入方法依賴的package包/類
public CameraOptionsPanel(JSONObject jo) {
    setLayout(new GridBagLayout());

    GridBagConstraints c = new GridBagConstraints();
    c.anchor = GridBagConstraints.CENTER;
    c.fill = GridBagConstraints.HORIZONTAL;
    c.weightx = 1;
    c.weighty = 1;
    c.gridx = 0;
    c.gridy = 0;

    JPanel radioPanel = new JPanel(new FlowLayout(FlowLayout.LEADING, 5, 0));
    radioPanel.add(new JLabel("View", JLabel.RIGHT));
    for (CameraMode mode : CameraMode.values()) {
        JRadioButton radio = mode.radio;
        if (mode == CameraMode.Observer)
            radio.setSelected(true);
        radio.addItemListener(e -> {
            if (radio.isSelected())
                changeCamera(mode);
        });
        radioPanel.add(radio);
        modeGroup.add(radio);
    }
    add(radioPanel, c);

    JideButton info = new JideButton(Buttons.info);
    info.setToolTipText("Show viewpoint info");
    info.addActionListener(e -> new TextDialog("Viewpoint options information", explanation, false).showDialog());

    c.gridx = 1;
    c.weightx = 0;
    add(info, c);

    // create panels before potential camera change
    JSONObject joExpert = null;
    JSONObject joEquatorial = null;
    if (jo != null) {
        joExpert = jo.optJSONObject("expert");
        joEquatorial = jo.optJSONObject("equatorial");
    }
    expertOptionPanel = new CameraOptionPanelExpert(joExpert, UpdateViewpoint.expert, "HEEQ", true);
    equatorialOptionPanel = new CameraOptionPanelExpert(joEquatorial, UpdateViewpoint.equatorial, "HEEQ", false);

    double fovMin = 0, fovMax = 180;
    if (jo != null) {
        fovAngle = MathUtils.clip(jo.optDouble("fovAngle", fovAngle), fovMin, fovMax);
        try {
            CameraMode.valueOf(jo.optString("mode")).radio.setSelected(true);
        } catch (Exception ignore) {
        }
        JSONObject jc = jo.optJSONObject("camera");
        if (jc != null)
            Displayer.getCamera().fromJson(jc);
    }

    JPanel fovPanel = new JPanel(new FlowLayout(FlowLayout.LEADING, 5, 0));
    fovPanel.add(new JLabel("FOV angle", JLabel.RIGHT));

    JSpinner fovSpinner = new JSpinner(new SpinnerNumberModel(Double.valueOf(fovAngle), Double.valueOf(fovMin), Double.valueOf(fovMax), Double.valueOf(0.01)));
    fovSpinner.setMaximumSize(new Dimension(6, 22));
    fovSpinner.addChangeListener(e -> {
        fovAngle = (Double) fovSpinner.getValue();
        Displayer.display();
    });

    JFormattedTextField f = ((JSpinner.DefaultEditor) fovSpinner.getEditor()).getTextField();
    f.setFormatterFactory(new TerminatedFormatterFactory("%.2f", "\u00B0", fovMin, fovMax));

    WheelSupport.installMouseWheelSupport(fovSpinner);
    fovPanel.add(fovSpinner);

    c.weightx = 1;
    c.gridx = 0;
    c.gridy = 1;
    add(fovPanel, c);

    ComponentUtils.smallVariant(this);
}
 
開發者ID:Helioviewer-Project,項目名稱:JHelioviewer-SWHV,代碼行數:80,代碼來源:CameraOptionsPanel.java

示例11: GridLayerOptions

import javax.swing.JFormattedTextField; //導入方法依賴的package包/類
GridLayerOptions(GridLayer _grid) {
    grid = _grid;
    createGridResolutionX();
    createGridResolutionY();

    setLayout(new GridBagLayout());

    GridBagConstraints c0 = new GridBagConstraints();
    c0.fill = GridBagConstraints.HORIZONTAL;
    c0.weightx = 1.;
    c0.weighty = 1.;

    c0.gridy = 0;

    c0.gridx = 1;
    c0.anchor = GridBagConstraints.EAST;
    JCheckBox axis = new JCheckBox("Solar axis", grid.getShowAxis());
    axis.setHorizontalTextPosition(SwingConstants.LEFT);
    axis.addActionListener(e -> {
        grid.showAxis(axis.isSelected());
        Displayer.display();
    });
    add(axis, c0);

    c0.gridx = 3;
    c0.anchor = GridBagConstraints.EAST;
    JCheckBox labels = new JCheckBox("Grid labels", grid.getShowLabels());
    labels.setHorizontalTextPosition(SwingConstants.LEFT);
    labels.addActionListener(e -> {
        grid.showLabels(labels.isSelected());
        Displayer.display();
    });
    add(labels, c0);

    c0.gridy = 1;

    c0.gridx = 1;
    c0.anchor = GridBagConstraints.EAST;
    JCheckBox radial = new JCheckBox("Radial grid", grid.getShowRadial());
    radial.setHorizontalTextPosition(SwingConstants.LEFT);
    radial.addActionListener(e -> {
        grid.showRadial(radial.isSelected());
        Displayer.display();
    });
    add(radial, c0);

    c0.gridx = 2;
    c0.anchor = GridBagConstraints.EAST;
    add(new JLabel("Grid type", JLabel.RIGHT), c0);
    c0.gridx = 3;
    c0.anchor = GridBagConstraints.WEST;
    createGridTypeBox();
    add(gridTypeBox, c0);

    c0.gridy = 2;

    c0.gridx = 0;
    c0.anchor = GridBagConstraints.EAST;
    add(new JLabel("Longitude", JLabel.RIGHT), c0);

    JFormattedTextField fx = ((JSpinner.DefaultEditor) gridResolutionXSpinner.getEditor()).getTextField();
    fx.setFormatterFactory(new TerminatedFormatterFactory("%.1f", "\u00B0", min, max));

    c0.gridx = 1;
    c0.anchor = GridBagConstraints.WEST;
    add(gridResolutionXSpinner, c0);

    c0.gridx = 2;
    c0.anchor = GridBagConstraints.EAST;
    add(new JLabel("Latitude", JLabel.RIGHT), c0);

    JFormattedTextField fy = ((JSpinner.DefaultEditor) gridResolutionYSpinner.getEditor()).getTextField();
    fy.setFormatterFactory(new TerminatedFormatterFactory("%.1f", "\u00B0", min, max));

    c0.gridx = 3;
    c0.anchor = GridBagConstraints.WEST;
    add(gridResolutionYSpinner, c0);

    ComponentUtils.smallVariant(this);
}
 
開發者ID:Helioviewer-Project,項目名稱:JHelioviewer-SWHV,代碼行數:81,代碼來源:GridLayerOptions.java


注:本文中的javax.swing.JFormattedTextField.setFormatterFactory方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。