简体   繁体   中英

From JTextField to JFormattedTextField

In NetBeans I've created a project with many of JTextField 's. Now I understand that I should validate it. Is it possible to "cast" text fields to JFormattedTextField 's?

It is possible to "cast" text fields to JFormattedTextField 's?

No it's not. At least not directly. The other way around is possible though, given JFormattedTextField extends from JTextField .

So I must delete all JTexFields and add Formatted fields ?

Not necessarily but it's highly recommended. On the other hand you can let the class' variables declared as JTextField and initialize them as JFormattedTextField through custom code:

自定义代码

代码定制器

Then you can "safely" downcast them as JFormattedTextField later. However I'd like to emphasize one more time that it sounds like a dirty workaround. Also note you won't be able to change the setText(...) code to setValue(...) which is preferred for formatted text fields.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM