本文整理了Java中javax.swing.text.NumberFormatter
类的一些代码示例,展示了NumberFormatter
类的具体用法。这些代码示例主要来源于Github
/Stackoverflow
/Maven
等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。NumberFormatter
类的具体详情如下:
包路径:javax.swing.text.NumberFormatter
类名称:NumberFormatter
暂无
代码示例来源:origin: stackoverflow.com
public static void main(String[] args) {
NumberFormat format = NumberFormat.getInstance();
NumberFormatter formatter = new NumberFormatter(format);
formatter.setValueClass(Integer.class);
formatter.setMinimum(0);
formatter.setMaximum(Integer.MAX_VALUE);
formatter.setAllowsInvalid(false);
// If you want the value to be committed on each keystroke instead of focus lost
formatter.setCommitsOnValidEdit(true);
JFormattedTextField field = new JFormattedTextField(formatter);
JOptionPane.showMessageDialog(null, field);
// getValue() always returns something valid
System.out.println(field.getValue());
}
代码示例来源:origin: ballerina-platform/ballerina-lang
public TimeoutGUI() {
rows = Timeout.getTimeoutsJava().entrySet().stream().map(e -> {
NumberFormat format = NumberFormat.getInstance();
NumberFormatter formatter = new NumberFormatter(format);
format.setGroupingUsed(false);
formatter.setValueClass(Integer.class);
formatter.setMinimum(0);
formatter.setAllowsInvalid(true);
formatter.setMaximum(Integer.MAX_VALUE);
final JFormattedTextField field = new JFormattedTextField(formatter);
field.setToolTipText(FIELD_TOOLTIP);
field.setText(e.getValue().toString());
return new AbstractMap.SimpleEntry<>(e.getKey(), field);
}).collect(Collectors.toMap(AbstractMap.SimpleEntry::getKey, AbstractMap.SimpleEntry::getValue));
rootPanel = createRootPanel();
}
代码示例来源:origin: stackoverflow.com
NumberFormat longFormat = NumberFormat.getIntegerInstance();
NumberFormatter numberFormatter = new NumberFormatter(longFormat);
numberFormatter.setValueClass(Long.class); //optional, ensures you will always get a long value
numberFormatter.setAllowsInvalid(false); //this is the key!!
numberFormatter.setMinimum(0l); //Optional
JFormattedTextField field = new JFormattedTextField(numberFormatter);
代码示例来源:origin: stackoverflow.com
setUpFormats();
amountField = new JFormattedTextField(
new DefaultFormatterFactory(
new NumberFormatter(amountDisplayFormat),
new NumberFormatter(amountDisplayFormat),
new NumberFormatter(amountEditFormat)));
amountField.setValue(new Double(amount));
amountField.setColumns(10);
amountField.addPropertyChangeListener("value", this);
Object source = e.getSource();
if (source == amountField) {
amount = ((Number) amountField.getValue()).doubleValue();
System.out.println("amount = " + amount);
amountDisplayFormat = NumberFormat.getCurrencyInstance(new Locale("tr", "TR"));
System.out.println(amountDisplayFormat.format(1200));
amountDisplayFormat.setMinimumFractionDigits(0);
amountEditFormat = NumberFormat.getNumberInstance();
代码示例来源:origin: stackoverflow.com
//Locale myLocale = Locale.GERMANY; //... or better, the current Locale
Locale myLocale = Locale.getDefault(); // better still
NumberFormat numberFormatB = NumberFormat.getInstance(myLocale);
numberFormatB.setMaximumFractionDigits(2);
numberFormatB.setMinimumFractionDigits(2);
numberFormatB.setMinimumIntegerDigits(1);
edit.setFormatterFactory(new javax.swing.text.DefaultFormatterFactory(
new NumberFormatter(numberFormatB)));
代码示例来源:origin: MarginallyClever/Makelangelo-software
protected void createAndAttachFormatter(Locale locale) {
NumberFormat nFloat = NumberFormat.getNumberInstance(locale);
nFloat.setMinimumFractionDigits(1);
nFloat.setMaximumFractionDigits(3);
nFloat.setGroupingUsed(false);
NumberFormatter nff = new NumberFormatter(nFloat);
DefaultFormatterFactory factory = new DefaultFormatterFactory(nff);
setFormatterFactory(factory);
}
代码示例来源:origin: stackoverflow.com
NumberFormat format = NumberFormat.getNumberInstance();
format.setMaximumFractionDigits(2);
format.setMinimumFractionDigits(2);
format.setParseIntegerOnly(true);
format.setRoundingMode(RoundingMode.HALF_UP);
NumberFormatter formatter = new NumberFormatter(format);
formatter.setMaximum(1000);
formatter.setMinimum(0.0);
JFormattedTextField tf = new JFormattedTextField(formatter);
tf.setColumns(10);
tf.setValue(123456789.99);
JFormattedTextField tf1 = new JFormattedTextField(formatter);
tf1.setValue(1234567890.99);
JFormattedTextField tf2 = new JFormattedTextField(formatter);
代码示例来源:origin: org.boofcv/boofcv-swing
public static JFormattedTextField createTextField( int current , int min , int max ) {
NumberFormat format = NumberFormat.getInstance();
NumberFormatter formatter = new NumberFormatter(format);
formatter.setValueClass(Integer.class);
formatter.setMinimum(min);
formatter.setMaximum(max);
formatter.setAllowsInvalid(true);
// formatter.setCommitsOnValidEdit(true);
JFormattedTextField field = new JFormattedTextField(formatter);
field.setHorizontalAlignment(JTextField.RIGHT);
field.setValue(current);
return field;
}
代码示例来源:origin: Camelcade/Perl5-IDEA
debugPanel.add(debuggingHost);
NumberFormat numberFormat = NumberFormat.getInstance();
numberFormat.setMaximumIntegerDigits(6);
numberFormat.setGroupingUsed(false);
NumberFormatter formatter = new NumberFormatter(numberFormat);
formatter.setAllowsInvalid(false);
formatter.setMaximum(65535);
formatter.setMinimum(0);
myDebuggingPort = new JFormattedTextField(formatter);
myDebuggingPort.getDocument().addDocumentListener(documentAdapter);
LabeledComponent<JFormattedTextField> debuggingPort =
LabeledComponent.create(myDebuggingPort, PerlBundle.message("perl.run.option.debugger.port"));
代码示例来源:origin: stackoverflow.com
NumberFormat format = NumberFormat.getCurrencyInstance();
format.setMaximumFractionDigits(2);
format.setMinimumFractionDigits(2);
format.setParseIntegerOnly(true);
format.setRoundingMode(RoundingMode.HALF_UP);
NumberFormatter formatter = new NumberFormatter(format);
formatter.setMaximum(1000.0);
formatter.setMinimum(0.0);
formatter.setOverwriteMode(false);
JFormattedTextField tf = new JFormattedTextField(formatter);
tf.setColumns(10);
tf.setValue(123456789.99);
JFormattedTextField tf1 = new JFormattedTextField(formatter);
tf1.setValue(1234567890.99);
JFormattedTextField tf2 = new JFormattedTextField(formatter);
代码示例来源:origin: com.github.haifengl/smile-plot
super(new JFormattedTextField());
textField = (JFormattedTextField) getComponent();
minimum = new Integer(min);
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);
代码示例来源:origin: stackoverflow.com
public static void main(String[] args) {
NumberFormat format = NumberFormat.getInstance();
format.setGroupingUsed(false);
NumberFormatter formatter = new NumberFormatter(format);
formatter.setValueClass(Integer.class);
formatter.setMinimum(0);
formatter.setMaximum(Integer.MAX_VALUE);
JFormattedTextField field = new JFormattedTextField(formatter);
JOptionPane.showMessageDialog(null, field);
}
代码示例来源:origin: stackoverflow.com
NumberFormat format = NumberFormat.getInstance();
format.setGroupingUsed(false);
NumberFormatter formatter = new NumberFormatter(format);
formatter.setValueClass(Integer.class);
formatter.setMaximum(65535);
formatter.setAllowsInvalid(false);
formatter.setCommitsOnValidEdit(true);
myTextField = new JFormattedTextField(formatter);
代码示例来源:origin: Exslims/MercuryTrade
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;
}
代码示例来源:origin: stackoverflow.com
NumberFormat format = NumberFormat.getInstance();
format.setParseIntegerOnly(true);
NumberFormatter formatter = new NumberFormatter(format);
formatter.setMinimum(1);
formatter.setMaximum(100);
JFormattedTextField textField = new JFormattedTextField(formatter);
textField.setColumns(5);
textField.setValue(1);
gridPanel.add(textField);
for(Component comp : gridPanel.getComponents()) {
JFormattedTextField textField = (JFormattedTextField)comp;
Integer value = (Integer)textField.getValue();
Color foreground = list.contains(value) ? Color.RED : Color.BLACK;
textField.setForeground(foreground);
代码示例来源:origin: stackoverflow.com
NumberFormat percentDisplayFormat = NumberFormat.getPercentInstance();
NumberFormat percentEditFormat = NumberFormat.getNumberInstance();
NumberFormatter percentEditFormatter = new NumberFormatter(percentEditFormat) {
private static final long serialVersionUID = 1L;
percentEditFormatter.setMinimum(0D);
percentEditFormatter.setMaximum(100D);
JFormattedTextField field = new JFormattedTextField(
new DefaultFormatterFactory(
new NumberFormatter(percentDisplayFormat),
new NumberFormatter(percentDisplayFormat),
percentEditFormatter));
代码示例来源:origin: stackoverflow.com
NumberFormat.getCurrencyInstance(locale) : NumberFormat.getCurrencyInstance();
NumberFormatter formatter = new NumberFormatter(format);
formatter.setMinimum(Double.MIN_VALUE);
formatter.setMaximum(Double.MAX_VALUE);
formatter.setAllowsInvalid(false);
renderer = new JFormattedTextField(formatter);
NumberFormat.getNumberInstance(locale) : NumberFormat.getNumberInstance();
NumberFormatter formatter = new NumberFormatter(format);
formatter.setMinimum(Double.MIN_VALUE);
formatter.setMaximum(Double.MAX_VALUE);
formatter.setAllowsInvalid(false);
editor = new JFormattedTextField(formatter);
editor.setBorder(UIManager.getBorder("Tree.editorBorder"));
return editor.getValue();
代码示例来源:origin: stackoverflow.com
NumberFormat format = NumberFormat.getCurrencyInstance(Locale.US);
format.setMaximumFractionDigits(0);
NumberFormatter formatter = new NumberFormatter(format);
formatter.setMinimum(5.0);
formatter.setMaximum(10000000.0);
formatter.setAllowsInvalid(false);
formatter.setOverwriteMode(true);
JFormattedTextField field = new JFormattedTextField(formatter);
field.setValue(5.0);
代码示例来源:origin: stackoverflow.com
public static void main(String[] args) {
NumberFormat format = NumberFormat.getInstance();
NumberFormatter formatter = new NumberFormatter(format) {
@Override
public void install(JFormattedTextField pField) {
final JFormattedTextField oldField = getFormattedTextField();
final int oldLength = pField.getDocument().getLength();
final int oldPosition = pField.getCaretPosition();
super.install(pField);
if (oldField == pField && oldLength == pField.getDocument().getLength()) {
pField.setCaretPosition(oldPosition);
}
}
};
JFormattedTextField field = new JFormattedTextField(formatter);
field.setValue(1234567890);
JOptionPane.showMessageDialog(null, field);
}
代码示例来源:origin: stackoverflow.com
private JLabel buyLabel;
private JLabel sellLabel;
private static final NumberFormat NUMBER_FORMAT = NumberFormat.getInstance();
JPanel container;
container = new JPanel();
btnProfit = new JButton("Calculate");
buyPrice = new JFormattedTextField(getIntFormatter(NUMBER_FORMAT));
sellPrice = new JFormattedTextField(getIntFormatter(NUMBER_FORMAT));
resultField = new JTextField();
buyLabel = new JLabel("The price you intend to pay");
NumberFormatter formatter = new NumberFormatter(NUMBER_FORMAT);
formatter.setValueClass(Integer.class);
formatter.setMinimum(0);
内容来源于网络,如有侵权,请联系作者删除!