|
|
JFormattedTextField is a very useful Swing component, that let’s your users see data in a format suitable for them, while letting you read or write from it in a way that that suitable for your code. Let’s make sure that you clearly understand everything related to it. What you see is not what’s inside: Consider the TextField component. It has only one value associated with its model. The “text”. This is the same value that the user sees, and the value that you set or get using your code. This is OK for simple text fields, but what about cases where it’s convenient for you to manipulate the data in different way than what you need to show to the user. Not sure when such a case would arise? Consider this. Say you have a percentageTextField, in which the user can type his value in percentage like “30%” , “40%” etc. It’s very intuitive and readable for the user to see the string with “%” appeneded in the end (like “30%” instead of “30″), but wouldn’t you find it more easier to manipulate this value as integers 30 and 40, instead of Strings “30%” and “40%”? Formatter: The one who converts between the “text” and “value” So, as far, you have understood that the JFormattedTextField has both “text” and “value” associated with it. But there’s one small catch left yet. When the “text” is updated so should the “value” and vice verca. For eg., when the user types “30%” the value should have an integer value of 30. and then when the user changes the text to “40%” the value should get automatically updated to integer 40. Right? And viceverca is true too, when through code you set the value to “50″ the user should see “50%” in the UI. When does this conversion happen? Before that let’s see who converts these.
When does Synchronization between text and value happen?When the user types some text it should get converted to the appropriate value object and when you modify the value object through code, it should somehow get converted back to the “text” string that the user sees. When does this conversion happen? When modifying the value object the conversion to text happens immediately, but when ther user types text, it usually happens in one of the following ways
Note, however this is not always true. The Formatter may decide to do it in other times too, like for example , for each character press by the user. You can also programatically call the commitEdit() method in the JFormattedTextField to convert the text to value at once. An Example: Let’s write the percentage text field that we were discussing above. As discussed above it displays values like “30%”, “40%” etc., while the in the code we use integers like 30, 40 etc. First Let’s write an AbstractFormatter subclass Step 1: Create the PertentageFormatter classclass PercentageFormatter extends AbstractFormatter{ //Convert the string to Integer @Override public Integer stringToValue(String text) throws ParseException { if(text == null){ return Integer.valueOf(0); } //If the text ends with "%" remove it if(text.endsWith("%")){ text = text.substring(0, text.length()-1); } //If the text is empty, assume it's 0 if("".equals(text)){ return Integer.valueOf(0); } else{ try { //Try to parse the remaining string, if there's an exception use 0 return Integer.valueOf(text); } catch (NumberFormatException e) { //Leave it. If there's any exception return 0 below } } return Integer.valueOf(0); } @Override public String valueToString(Object value) throws ParseException { //If the value is an integer, append "%" to it // and return the value to display. if(value == null || (! (value instanceof Integer))) { return "0%"; } Integer val = (Integer)value; return val.intValue() + "%"; } } Step2: Create the JFormattedTextField using the “PercentageFormatter” formatterThis is really simple. You pass an instance of the “PercentageFormatter” sub class to the JFormattedTextField. JFormattedTextField field = new JFormattedTextField(new PercentageFormatter()); tha’s it. Now add this field to your form, type some value like “40%” and use the getValue() to get the value as an Integer Object 40. 4 Responses to 'Demystifying JFormattedTextField: A Step by Step tutorial'Leave a Reply |
Popular Articles
Blog Categories
Monthly Archives
Resources
|
crisp and clear. Would have been better if you have explained the MaskFormatter too.
[…] continues its series of tutorials on Swing text components, and this time they write an introduction to working with JFormattedTextField […]
I feel compelled to point out that the easy way to get a JFormattedTextField for percents is to do:
new JFormattedTextField(java.text.NumberFormat.getPercentInstance())
This way there’s no need to write your own formatter, but note that if you type 33% into the field and call getValue() it will return 0.33, not 33.0 (that’s how I generally prefer to deal with my percentage values anyway).
[…] Demystifying JFormattedTextField: A Step by Step tutorial » Java Swing JFormattedTextField is a very useful Swing component, that let’s your users see data in a format suitable for them, while letting you read or write from it in a way that that suitable for your code. Let’s make sure that you clearly understand everything related to it. […]