Advice welcomed on creating my own Swing component

Posted by Toto on Stack Overflow See other posts from Stack Overflow or by Toto
Published on 2010-03-24T20:47:01Z Indexed on 2010/03/24 21:03 UTC
Read the original article Hit count: 401

Filed under:
|
|
|
|

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 lengthscale 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.

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.

© Stack Overflow or respective owner

Related posts about java

Related posts about swing