Tag: jformattedtextfield

JFormattedTextField格式化百分比数字?

我想使用JFormattedTextField将浮点数格式化为百分比值,允许输入从0到100%(转换为0.0f-1.0f),始终显示百分号并禁止任何无效字符。 现在我用NumberFormat.getPercentInstance()和NumberFormatter属性进行了一些实验,但没有成功。 有没有办法创建一个遵循标准类遵守这些规则的JFormattedTextField? 或者我必须实现自己的NumberFormatter? 这就是我到目前为止(无法输入100%,输入0完全打破它): public class MaskFormatterTest { public static void main(String[] args) throws Exception { JFrame frame = new JFrame(“Test”); frame.setLayout(new BorderLayout()); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); NumberFormat format = NumberFormat.getPercentInstance(); NumberFormatter formatter = new NumberFormatter(format); formatter.setMaximum(1.0f); formatter.setMinimum(0.0f); formatter.setAllowsInvalid(false); formatter.setOverwriteMode(true); JFormattedTextField tf = new JFormattedTextField(formatter); tf.setColumns(20); tf.setValue(0.56f); frame.add(tf); frame.pack(); frame.setVisible(true); } }

如何使JFormattedTextField接受不带小数点的整数(逗号)?

我以这种方式使用JFormattedTextField和NumberFormat : – 提供JFormattedTextField引用 JFormattedTextField integerField; – 创建一个NumberFormat引用 NumberFormat integerFieldFormatter; – 在构造函数中: integerFieldFormatter = NumberFormat.getIntegerInstance(); integerFieldFormatter.setMaximumFractionDigits(0); integerField = new JFormattedTextField(integerFieldFormatter ); integerField.setColumns(5); ………. 我的意思是只使用整数,但是当我键入1500这样的数字时,它会在失去焦点后转换为1,500,抛出exception这是它的第一行: 线程“AWT-EventQueue-0”中的exceptionjava.lang.NumberFormatException:对于输入字符串:“1,500” 当我使用JTextField而不是JFormattedTextField正常接受所有整数时,但我想要使用JFormattedTextField的原因是受益于其输入限制优势。

如何获得用户从JFormattedTextField看到的相同值?

我正在使用NumberFormatter和JFormattedTextField ,但.getValue()不会返回与用户看到的值相同的值。 我认为输入字符串是使用NumberFormats parse-method解析的,我从NumberFormat.getNumberInstance();得到Numberformat NumberFormat.getNumberInstance(); 与实际的区域设置。 所以我认为我不能轻易扩展它并编写自己的解析方法? 例如 ,如果用户键入1234.487则getValue()将返回: 1234.487但将显示用户1,234.49 另一个例子 ,使用NumberFormat.getCurrencyInstance(); 。 用户1234.487 , getValue()将返回1234.487但用户将显示$1,234.49 相反 ,如果Formatter无法在不进行舍入的情况下格式化值,则需要生成ParseException。 同样的事情,如果用户键入4.35b6 ,默认情况下格式化程序将显示4.35 ,值将为4.35 ,但我想要一个ParseException,因为用户键入了无效值。 这是我尝试过的代码: NumberFormat nf = NumberFormat.getNumberInstance(); nf.setMaximumFractionDigits(2); nf.setMinimumFractionDigits(2); final JFormattedTextField ftf = new JFormattedTextField(nf); ftf.setValue(new BigDecimal(“1234.50”)); // Print the value from ftf final JTextField txt = new JTextField(12); txt.addFocusListener(new FocusAdapter() { public void focusGained(FocusEvent […]

如何设置jFormattedTextField所以它只允许2个数字?

我是Java和NetBeans的初学者。 我正在尝试制作一个简单的程序,你引入2个数字,它们的总和除以2。 但是,我正在使用JFormattedTExtField ,我不知道如何自定义它们中允许的输入。 基本上我试图找出如何: 只允许在JFormmatedTextField输入JFormmatedTextField ; 只允许一定数量的数字;

ActionListeners,多个字段更新以及从文件重新加载用户GUI选择

我有多个地方可以在一个坐标系和另一个坐标系之间进行转换。 在每种情况下都有一个余弦/正弦计算,我们称之为x,y和x’,y’。 这些都是JFormattedTextFields。 如果用户在4中的任何一个中输入值,则调用ActionListener。 让我们调用字段fieldx,fieldy,fieldx1和fieldy1。 如果用户在fieldx或fieldy中输入任何内容,我将使用fieldx和fieldy中的当前值更新fieldx1和fieldy1的键盘和焦点侦听器(所有四个字段都是相同的)。 如果对侦听器的调用来自fieldx1或fieldy1,则会计算fieldx和fieldy。 然后我决定在配置文件中保存所选字段(包括一堆复选框开/关和一些微调器的值)(开发后的新要求)。 我认为通过设置值和状态一切都会好的但是某些事情没有发生(在幕后)。 我认为部分原因是各种检查和输入等触发方法没有发生,因为当它们由一段代码设置时,字段没有触发键盘和焦点监听器。 在线阅读之后,我将所有的KeyboardAdapter更改为ActionListener,在我设置了值之后,我在上面提到的字段上调用了postActionEvent(),现在所有的后台内容都发生了(尽管顺序是一个问题,我将保存有关的额外信息)声明要正确更新。 我做了这个改变,因为我认为解雇这么多假键盘事件会更加困难吗? 可能有更聪明/聪明的方法来做到这一点但是现在我试图不要触及过多的代码。 有没有人建议保存面板的状态并刷新它(当前对象,面板)? 如果我继续这样做,有人可以建议一种方法来最小化ActionListener触发的时间吗? 它似乎经常发射它是荒谬的! 我应该改变这里建议的事情吗? 您的类应该实现ActionListener还是使用匿名ActionListener类的对象 这似乎涉及更多的编码,但更精确的触发时… 如果这种问题/讨论在这里不合适,请告诉我:)。 只需输入此页面,我就会想到更多要阅读和尝试的内容。

为什么JFormattedTextField是邪恶的?

在这个问题中有没有办法只接受JTextField中的数值? 其中一个答案表明JFormattedTextField存在问题。 我还没有使用它,但有人可以扩展(或不同意)这个课程的问题吗?

用于Double的JFormattedTextField仍然需要字符

我有一个JFormattedTextField用户输入价格,我有这个,但如果我输入一个字符,它还是会让我。 我需要这个文本字段只读取数字或键盘,并忽略它是否为char。 我应该如何更改才能使其正常工作? JFormattedTextField formattedTextField = new JFormattedTextField(); formattedTextField.setBounds(25, 330, 56, 20); contentPanel.add(formattedTextField); formattedTextField.setValue(new Double(10.0));

jFormattedTextField的Formatter.setCommitsOnValidEdit(true)在第一次焦点时不起作用

我有一个jFormattedTextField ,我将setCommitsOnValidEdit设置为true然后我在“value”属性上添加了一个事件监听器“属性更改”。 在jFormattedTextField第一个焦点处,它在键入时不会调用事件侦听器方法。 但是在“focusLost”上它会调用事件监听器,之后当它再次获得焦点时,它会在键入时调用事件监听器。 我希望在jFormattedTextField中的任何时间发生任何变化之后调用事件监听jFormattedTextField (即使在第一个焦点中)。 有什么问题? 我该如何解决?

FocusEvent没有得到JFormattedTextField的最后一个值,我怎么能得到它?

我的JFrame对象上有两个JFormattedTextField对象。 我希望通过这些JFormattedTextField对象的值进行基本的Math(加法)。 当焦点丢失第一个或第二个文本字段时,我希望它发生。 但是当“ focusLost() ”,事件没有得到最后一个值时,它会得到之前的值。 例如; tf1为0, tf2为0。 我将2写入tf1 ,当focusLost() ,结果( tf1+tf2 )变为静止0.当我更改其中任何一个时,结果变为2(前一个值) 如何获取focusLost上的最后一个值? 这是我的代码: JFormattedTextField tf1,tf2; NumberFormat format=NumberFormat.getNumberInstance(); tf1=new JFormattedTextField(format); tf1.addFocusListener(this); tf2=new JFormattedTextField(format); tf2.addFocusListener(this); 和focusLost() : public void focusLost(FocusEvent e) { if(tf1.getValue() == null) tf1.setValue(0); if(tf2.getValue() == null) tf2.setValue(0); //because if I dont set, it throws nullPointerException for tf.getValue() BigDecimal no1 = new […]

使JSpinner只读取数字,但也检测退格

我正在尝试创建一个只接受数字的JSpinner,但我也想让它读取/响应退格。 public class test { JFrame frame; JPanel panel; JSpinner spinner; public test() { frame = new JFrame(“test”); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setPreferredSize(new Dimension(200,200)); panel = new JPanel(); SpinnerNumberModel numSpin = new SpinnerNumberModel(10, 0,1000,1); spinner = new JSpinner(numSpin); JFormattedTextField txt = ((JSpinner.NumberEditor) spinner.getEditor()).getTextField(); ((NumberFormatter) txt.getFormatter()).setAllowsInvalid(false); panel.add(spinner); frame.setContentPane(panel); frame.pack(); frame.setLocationByPlatform(true); frame.setVisible(true); } public static void main(String[] args) { test […]