最近我问哪个是绑定到 BigDecimal 的最佳 Swing 组件变量(具有一些特定的编辑属性)。事实证明,没有一个标准 Swing 组件完全适合我,我在那里找到的第三方 Swing 组件库也不适合。所以我决定创建我自己的 Swing 组件。
组件描述:
我想扩展 JTextField或 JFormattedTextField,所以我的新组件可以可以轻松绑定到 BigDecimal 变量。
该组件将具有可自定义的scale和length属性。
行为:
绘制组件时,它仅显示小数点和右侧 scale 位数字的空格。
当组件获得焦点时,插入符号应位于小数点左侧。当用户键入数字时(任何其他字符都会被忽略),它们会出现在插入符号的左侧,仅接受 length - scale 数字,键入的任何其他数字都将被忽略整数部分已满。每当用户键入小数点时,插入符号都会移动到小数点的右侧。输入的以下数字显示在小数部分中,只有 scale 数字被视为输入的任何其他数字都将被忽略,因为小数部分已满。此外,当用户键入小数点后的数字时,应显示千位分隔符。
我还希望能够将该组件用作 JTable< 中的“单元格编辑器” /a> (无需编码两次)。
在组件上调用 getValue() 方法应生成表示刚刚输入的数字的 BigDecimal。
我从未创建过自己的 Swing 组件;我几乎没用过标准的。因此,我将不胜感激有关创建所描述组件的任何好的教程/信息/提示。 这是我唯一的事情已经到此为止了。
提前致谢。
Recently I asked which was the best Swing component to bind to a BigDecimal variable (with some particular editing properties). It turns out that none of the standard Swing components suit me completely, nor did the third-party Swing component libraries I've found out there. So I’ve decided to create my own Swing component.
Component description:
I want to extend JTextField or JFormattedTextField, so my new component can be easily bound to a BigDecimal variable.
The component will have customizable scale and length properties.
Behavior:
When the component is drawn, it shows only the decimal point and space for scale digits to its right.
When the component receives focus the caret should be positioned left to the decimal point. As the user types numbers (any other character is ignored) they appear to the left of the caret, only length – scale numbers are accepted, any other number typed is ignored as the integer portion is full. Any time the user types the decimal point the caret moves to the right side of the decimal point. The following numbers typed are shown in the decimal part, only scale numbers are considered any other number typed is ignored as the decimal portion is full. Additionally, thousand separators should appear as the user types numbers left to the decimal point.
I also want to be able to use the component as a Cell Editor in a JTable (without having to code it twice).
Invoking a getValue() method on the component should yield the BigDecimal representing the number just entered.
I’ve never created my own Swing component; I’ve barely used the standard ones. So I would appreciate any good tutorial/info/tip on creating the component described. This is the only thing I've got so far.
Thanks in advance.
发布评论
评论(2)
我喜欢您的 Grouchnikov 文章引用,但我不确定您是否想要更改 UI 委托。由于这将是一个不可变对象的视图,因此我更喜欢组合而不是继承。我倾向于认为您描述的组件是 渲染器,如示例。您可以添加
InputVerifier
或DocumwntListener< /code>
以获得您想要的验证。
附录:这是一个使用
JFormattedTextField
和MaskFormatter
。您需要调整格式掩码以匹配您的比例和长度。I like the Grouchnikov article you cited, but I'm not sure you would want to change the UI delegate. As this will be a view of an immutable object, I'd favor composition over inheritance. I tend to think of the component you describe in terms of being a renderer, as seen in this example. You can add an
InputVerifier
orDocumwntListener
to obtain the validation you want.Addendum: Here's an example that uses
JFormattedTextField
and aMaskFormatter
. You'll need to adjust the format mask to match your scale and length.使用您喜欢的任何组件并注册一个 KeyListener 来拒绝匹配您的行为的字符。 setValue 可以轻松获取/设置 BiDecimal 和其他一些方法,并且所有绘画都将由任何 JTextComponent 提供。
Use whatever component you like and register a KeyListener to reject characters to match your behaviour.Add a getValue() & setValue to get/set easily a BiDecimal and some other methods and all the painting will be provided by any JTextComponent.