本文整理汇总了Java中javax.swing.text.NumberFormatter类的典型用法代码示例。如果您正苦于以下问题:Java NumberFormatter类的具体用法?Java NumberFormatter怎么用?Java NumberFormatter使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
NumberFormatter类属于javax.swing.text包,在下文中一共展示了NumberFormatter类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: initializeTextFields
import javax.swing.text.NumberFormatter; //导入依赖的package包/类
private void initializeTextFields() {
DecimalFormatSymbols symbols = new DecimalFormatSymbols();
symbols.setDecimalSeparator('.');
NumberFormat format = new DecimalFormat("0.00", symbols);
format.setMaximumFractionDigits(2);
NumberFormatter formatter = new NumberFormatter(format);
formatter.setMinimum(0.0);
formatter.setMaximum(10000000.0);
formatter.setAllowsInvalid(false);
this.txtPeso = new JFormattedTextField(formatter);
this.txtPeso.setValue(0.0);
GridBagConstraints gbc_textField = new GridBagConstraints();
gbc_textField.gridwidth = 10;
gbc_textField.insets = new Insets(0, 0, 5, 0);
gbc_textField.fill = GridBagConstraints.BOTH;
gbc_textField.gridx = 2;
gbc_textField.gridy = 10;
this.panelSecond.add(this.txtPeso, gbc_textField);
this.txtPeso.setColumns(10);
}
示例2: createNumberFormatterDisplay
import javax.swing.text.NumberFormatter; //导入依赖的package包/类
/**
* Creates a {@link NumberFormatter} for display-mode from a given {@link NumberFormat}.
*
* @param format for the formatter
* @return created {@link NumberFormatter}
* @see NumberFormat
* @see NumberFormatter
* @since 0.0.1
*/
public static NumberFormatter createNumberFormatterDisplay(final NumberFormat format) {
if (log.isDebugEnabled()) log.debug(HelperLog.methodStart(format));
if (null == format) {
throw new RuntimeExceptionIsNull("format"); //$NON-NLS-1$
}
final NumberFormatter result = new NumberFormatter(format) {
private static final long serialVersionUID = -8279122736069976223L;
{
setCommitsOnValidEdit(true);
}
};
if (log.isDebugEnabled()) log.debug(HelperLog.methodExit(result));
return result;
}
示例3: WettDialog
import javax.swing.text.NumberFormatter; //导入依赖的package包/类
/**
* Creates a new WettDialog and displays the given array of snails.
* @param parent The Form opening this dialog
* @param schneggen The array of snails to display
* @param wettbueroFactor The factor the Wettbuero uses.
*/
public WettDialog(Frame parent, ArrayList<Rennschnecke> schneggen, double wettbueroFactor) {
super(parent, true);
initComponents();
NumberFormat format = NumberFormat.getCurrencyInstance();
format.setMinimumFractionDigits(2);
format.setMaximumFractionDigits(2);
NumberFormatter nf = new NumberFormatter(format);
nf.setMinimum(0.02);
// The maximum bet value is, well, pretty high.
nf.setMaximum(Double.MAX_VALUE / wettbueroFactor);
nf.setAllowsInvalid(false);
nf.setCommitsOnValidEdit(true);
nf.setOverwriteMode(false);
einsatzInput.setFormatterFactory(
new DefaultFormatterFactory(nf)
);
result = null;
snailList.setModel(new DefaultListModel<>());
snailList.setListData(schneggen.toArray(new Rennschnecke[schneggen.size()]));
}
示例4: NumberPropertyEditor
import javax.swing.text.NumberFormatter; //导入依赖的package包/类
public NumberPropertyEditor(Class type) {
if (!Number.class.isAssignableFrom(type)) {
throw new IllegalArgumentException("type must be a subclass of Number");
}
editor = new JFormattedTextField();
this.type = type;
((JFormattedTextField)editor).setValue(getDefaultValue());
((JFormattedTextField)editor).setBorder(LookAndFeelTweaks.EMPTY_BORDER);
// use a custom formatter to have numbers with up to 64 decimals
NumberFormat format = NumberConverters.getDefaultFormat();
((JFormattedTextField) editor).setFormatterFactory(
new DefaultFormatterFactory(new NumberFormatter(format))
);
}
示例5: IntegerEditor
import javax.swing.text.NumberFormatter; //导入依赖的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
}
}
示例6: formatNumber
import javax.swing.text.NumberFormatter; //导入依赖的package包/类
public static String formatNumber(Number num, int digits) {
String s = "0.";
for (int i = 0; i < digits; i++) {
s += "0";
}
DecimalFormat df = new DecimalFormat(s);
NumberFormatter nf = new NumberFormatter(df);
String out = "Illegal Format";
try {
out = nf.valueToString(num);
} catch (ParseException e) {
}
return out;
}
示例7: getValorTitulo
import javax.swing.text.NumberFormatter; //导入依赖的package包/类
public String getValorTitulo() {
String zeros = "0000000000";
DefaultFormatter formatter = new NumberFormatter(new DecimalFormat("#,##0.00"));
String valor = "";
try {
valor = formatter.valueToString(getValorTituloBigDecimal());
}
catch (Exception ex) {
}
valor = valor.replace(",", "").replace(".", "");
String valorTitulo = zeros.substring(0, zeros.length() - valor.length()) + valor;
return valorTitulo;
}
示例8: JNumberField
import javax.swing.text.NumberFormatter; //导入依赖的package包/类
public JNumberField(double defaultNumber, double minValue, double maxValue, double increment)
{
super(new SpinnerNumberModel(defaultNumber, minValue, maxValue, increment));
final NumberEditor editor = (NumberEditor) this.getEditor();
editor.getFormat().setMinimumFractionDigits(2);
editor.getFormat().setMaximumFractionDigits(6);
final JFormattedTextField spinnerTextEditor = editor.getTextField();
spinnerTextEditor.setColumns(6);
spinnerTextEditor.setHorizontalAlignment(JTextField.CENTER);
final NumberFormatter numberFormatter = new NumberFormatter(editor.getFormat());
numberFormatter.setValueClass(Double.class);
numberFormatter.setMinimum(0d);
numberFormatter.setMaximum(Double.MAX_VALUE);
numberFormatter.setAllowsInvalid(false);
spinnerTextEditor.setFormatterFactory(new JFormattedTextField(numberFormatter).getFormatterFactory());
}
示例9: getImageSizePanel
import javax.swing.text.NumberFormatter; //导入依赖的package包/类
private JPanel getImageSizePanel() {
JPanel imageSizePanel = new JPanel();
imageSizePanel.setLayout(new GridLayout(1, 4));
NumberFormat format = NumberFormat.getInstance();
format.setGroupingUsed(false);
NumberFormatter formatter = new NumberFormatter(format);
formatter.setValueClass(Integer.class);
formatter.setMinimum(0);
formatter.setMaximum(Integer.MAX_VALUE);
formatter.setCommitsOnValidEdit(true);
widthTF = new JFormattedTextField(formatter);
widthTF.setValue(1200);
addFocusListener(widthTF);
heightTF = new JFormattedTextField(formatter);
heightTF.setValue(1000);
addFocusListener(heightTF);
imageSizePanel.add(new JLabel(" Width:"));
imageSizePanel.add(widthTF);
imageSizePanel.add(new JLabel(" Height:"));
imageSizePanel.add(heightTF);
return imageSizePanel;
}
示例10: getImageSizePanel
import javax.swing.text.NumberFormatter; //导入依赖的package包/类
private JPanel getImageSizePanel() {
JPanel imageSizePanel = new JPanel();
imageSizePanel.setLayout(new GridLayout(1, 4));
NumberFormat format = NumberFormat.getInstance();
format.setGroupingUsed(false);
NumberFormatter formatter = new NumberFormatter(format);
formatter.setValueClass(Integer.class);
formatter.setMinimum(0);
formatter.setMaximum(Integer.MAX_VALUE);
formatter.setCommitsOnValidEdit(true);
widthTF = new JFormattedTextField(formatter);
widthTF.setValue(1200);
widthTF.addFocusListener(FOCUS_LISTENER);
heightTF = new JFormattedTextField(formatter);
heightTF.setValue(1000);
heightTF.addFocusListener(FOCUS_LISTENER);
imageSizePanel.add(new JLabel(" Width:"));
imageSizePanel.add(widthTF);
imageSizePanel.add(new JLabel(" Height:"));
imageSizePanel.add(heightTF);
return imageSizePanel;
}
示例11: getIntegerTextField
import javax.swing.text.NumberFormatter; //导入依赖的package包/类
public JFormattedTextField getIntegerTextField(Integer min, Integer max, Integer value) {
NumberFormat format = NumberFormat.getInstance();
NumberFormatter formatter = new NumberFormatter(format);
formatter.setValueClass(Integer.class);
formatter.setMinimum(min);
formatter.setMaximum(max);
formatter.setAllowsInvalid(true);
formatter.setCommitsOnValidEdit(false);
JFormattedTextField field = new JFormattedTextField(formatter);
field.setValue(value);
field.setFont(REGULAR_FONT.deriveFont(scale * 18));
field.setFocusLostBehavior(JFormattedTextField.PERSIST);
field.setForeground(AppThemeColor.TEXT_DEFAULT);
field.setCaretColor(AppThemeColor.TEXT_DEFAULT);
field.setBorder(BorderFactory.createCompoundBorder(
BorderFactory.createLineBorder(AppThemeColor.BORDER, 1),
BorderFactory.createLineBorder(AppThemeColor.TRANSPARENT, 3)
));
field.setBackground(AppThemeColor.HEADER);
return field;
}
示例12: JIntegerTextField
import javax.swing.text.NumberFormatter; //导入依赖的package包/类
public JIntegerTextField() {
super();
DecimalFormat df = new DecimalFormat("####");
nf = new NumberFormatter(df) {
@Override
public String valueToString(Object iv) throws ParseException {
if ((iv == null) || (((Number) iv).intValue() == -1)) {
return "";
}
return super.valueToString(iv);
}
@Override
public Object stringToValue(String text) throws ParseException {
if ("".equals(text)) {
return null;
}
return super.stringToValue(text);
}
};
nf.setValueClass(Integer.class);
this.setFormatter(nf);
}
示例13: setupIntegerFieldTrackingValue
import javax.swing.text.NumberFormatter; //导入依赖的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
}
}
});
}
示例14: PropertyDoubleEditor
import javax.swing.text.NumberFormatter; //导入依赖的package包/类
/**
* Constructor.
* @param property The property which should be edited.
* @param configuration The configuration which should be edited.
* @param client YouScope client
* @param server YouScope server.
* @throws GenericException
*/
public PropertyDoubleEditor(Property property, Configuration configuration, YouScopeClient client, YouScopeServer server) throws GenericException
{
super(property, configuration, client, server, Double.class, double.class);
field = new JFormattedTextField(new NumberFormatter(getDoubleFormat()));
field.setValue(getValue(Double.class));
addLabel();
add(field);
field.addActionListener(new ActionListener()
{
@Override
public void actionPerformed(ActionEvent e)
{
try
{
commitEdits();
notifyPropertyValueChanged();
}
catch (GenericException e1)
{
sendErrorMessage("Could not set value of property " + getProperty().getName() + ".", e1);
}
}
});
}
示例15: NumberPropertyEditor
import javax.swing.text.NumberFormatter; //导入依赖的package包/类
public NumberPropertyEditor(Class<?> type) {
if (!Number.class.isAssignableFrom(type)) {
throw new IllegalArgumentException("type must be a subclass of Number");
}
editor = new JFormattedTextField();
this.type = type;
((JFormattedTextField) editor).setValue(getDefaultValue());
((JFormattedTextField) editor).setBorder(LookAndFeelTweaks.EMPTY_BORDER);
// use a custom formatter to have numbers with up to 64 decimals
NumberFormat format = NumberConverters.DEFAULT_FORMAT;
((JFormattedTextField) editor).setFormatterFactory(
new DefaultFormatterFactory(new NumberFormatter(format))
);
}