Percent JFormattedField

Discussion in 'Java' started by Dmitri, Nov 22, 2004.

  1. Dmitri

    Dmitri Guest

    Hello, guys

    I'm trying to create a text field where I could enter any valid
    digits for percents (let's say -900 .. 900) and have it displayed
    with '%' sign, when the field looses focus or ENTER is pressed.
    I am using NumberFormat.getPercentInstance() to get formatter and
    it displays the initial value fine (with '%' sign). I limit my
    input to digits with PercentInputFilter. The problem is, when I
    delete the current contents of the field, enter my new value
    (let's say 50) and click the button the value is not accepted
    and reverted to the previous one. As far as I understand, with
    getPercentInstance() it requires '%' char to be present. But
    what I'm trying to achieve is to enter digits and have '%' to
    be automatically displayed (if it's not there yet).

    I am browsing doc on JFormattedText field, but could come up
    with a good solution to meed my above mentioned needs.

    Any suggestions, help, sample code are appreciated.
    See sample code below. You can actually compile and run it
    to see the problem.

    Thanks
    -Dmitri

    <sample_code>

    import javax.swing.JFormattedTextField;
    import java.text.NumberFormat;
    import javax.swing.text.DocumentFilter;
    import javax.swing.text.BadLocationException;
    import javax.swing.text.AttributeSet;
    import javax.swing.text.DefaultFormatterFactory;
    import javax.swing.text.NumberFormatter;

    public class SpeedField extends JFormattedTextField {

    public static final int DEFAULT_COLUMNS = 11;
    public static final double DEFAULT_VALUE = 1.00; // 100%

    public SpeedField() {
    //super(NumberFormat.getPercentInstance());
    super();
    setColumns(DEFAULT_COLUMNS);
    setHorizontalAlignment(javax.swing.JTextField.RIGHT);

    NumberFormatter formatter =
    new NumberFormatter (NumberFormat.getPercentInstance()) {
    protected DocumentFilter getDocumentFilter() {
    return filter;
    }
    private DocumentFilter filter = new PercentInputFilter();
    };

    DefaultFormatterFactory dff = new DefaultFormatterFactory();
    dff.setDefaultFormatter(formatter);
    dff.setDisplayFormatter(formatter);
    dff.setEditFormatter(formatter);
    setFormatterFactory(dff);

    setValue(new Double(DEFAULT_VALUE));
    }

    protected int getColumnWidth() {
    return getFontMetrics(getFont()).charWidth('0');
    }

    private class PercentInputFilter extends DocumentFilter {

    public void insertString(FilterBypass fb,
    int offset,
    String string,
    AttributeSet attr) throws BadLocationException {

    StringBuffer buffer = new StringBuffer(string);
    for (int i = buffer.length() - 1; i >= 0; i--) {
    char ch = buffer.charAt(i);
    if (!Character.isDigit(ch) && ch != '-')
    buffer.deleteCharAt(i);
    }
    super.insertString(fb, offset, buffer.toStri343ng(), attr);
    }

    public void replace(FilterBypass fb,
    int offset,
    int length,
    String string,
    AttributeSet attr) throws BadLocationException {

    if (string != null) {
    StringBuffer buffer = new StringBuffer(string);
    for (int i = buffer.length() - 1; i >= 0; i--) {
    char ch = buffer.charAt(i);
    if (!Character.isDigit(ch) && ch != '-')
    buffer.deleteCharAt(i);
    }
    string = buffer.toString();
    }
    super.replace(fb, offset, length, string, attr);
    }

    } // class PercentInputField

    /** -----------------------------------------------------------------------
    * TEST UNIT
    */
    public static void main(String[] argv) {

    javax.swing.JFrame frame = new javax.swing.JFrame("SpeedField Test");
    frame.setDefaultCloseOperation(javax.swing.JFrame.EXIT_ON_CLOSE);

    final SpeedField speed = new SpeedField();
    javax.swing.JPanel p = new javax.swing.JPanel();
    p.add(speed);

    javax.swing.JButton btValue = new javax.swing.JButton("Value");
    btValue.addActionListener(new java.awt.event.ActionListener() {
    public void actionPerformed(java.awt.event.ActionEvent e) {
    System.err.println("value = " + speed.getValue());
    }
    });

    p.add(btValue);
    frame.getContentPane().add(p, java.awt.BorderLayout.NORTH);

    frame.pack();
    frame.setSize(new java.awt.Dimension(200, 80));
    frame.setLocation(100,100);
    frame.setVisible(true);

    }

    } // class SpeedField

    </sample_code>
     
    Dmitri, Nov 22, 2004
    #1
    1. Advertising

  2. On 22 Nov 2004 11:19:27 -0800, Dmitri wrote:

    I'm not so experienced with JFormattedTextField to solve your
    problem, though I played with your code out of interest.

    > See sample code below. You can actually compile and run it
    > to see the problem.


    Good example! Except for one (late I assume) typo.

    > <sample_code>

    .....
    > for (int i = buffer.length() - 1; i >= 0; i--) {
    > char ch = buffer.charAt(i);
    > if (!Character.isDigit(ch) && ch != '-')
    > buffer.deleteCharAt(i);
    > }
    > super.insertString(fb, offset, buffer.toStri343ng(), attr);

    .....

    Where did the '343' come from?

    BTW - Please do not x-post so widely, I recommend you set the
    Follow-Ups to c.l.j.gui *only* (as I have done).
    The people that can answer this are on c.l.j.gui, if anywhere.

    --
    Andrew Thompson
    http://www.PhySci.org/codes/ Web & IT Help
    http://www.PhySci.org/ Open-source software suite
    http://www.1point1C.org/ Science & Technology
    http://www.LensEscapes.com/ Images that escape the mundane
     
    Andrew Thompson, Nov 22, 2004
    #2
    1. Advertising

  3. Dmitri

    Dmitri Guest

    Andrew Thompson <> wrote in message news:<n1rat8a6vmcr$>...
    > On 22 Nov 2004 11:19:27 -0800, Dmitri wrote:
    >
    > I'm not so experienced with JFormattedTextField to solve your
    > problem, though I played with your code out of interest.
    >
    > > See sample code below. You can actually compile and run it
    > > to see the problem.

    >
    > Good example! Except for one (late I assume) typo.
    >
    > > <sample_code>

    > ....
    > > for (int i = buffer.length() - 1; i >= 0; i--) {
    > > char ch = buffer.charAt(i);
    > > if (!Character.isDigit(ch) && ch != '-')
    > > buffer.deleteCharAt(i);
    > > }
    > > super.insertString(fb, offset, buffer.toStri343ng(), attr);

    > ....
    >
    > Where did the '343' come from?


    Sure, it's a typo... probably my emacs editing window had focus
    when I was typing line number for debugging...
    Thanks for reply anyway!
     
    Dmitri, Nov 23, 2004
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. =?Utf-8?B?cGhpbGxpcA==?=

    Regexp and percent signs

    =?Utf-8?B?cGhpbGxpcA==?=, Sep 24, 2004, in forum: ASP .Net
    Replies:
    0
    Views:
    642
    =?Utf-8?B?cGhpbGxpcA==?=
    Sep 24, 2004
  2. =?Utf-8?B?Y2hhcmxpZXdlc3Q=?=

    How to: Convert DOUBLE to PERCENT?

    =?Utf-8?B?Y2hhcmxpZXdlc3Q=?=, Jul 12, 2005, in forum: ASP .Net
    Replies:
    4
    Views:
    33,233
    =?Utf-8?B?Y2hhcmxpZXdlc3Q=?=
    Jul 12, 2005
  3. Mickey Segal
    Replies:
    76
    Views:
    1,732
    decent_john
    Feb 3, 2006
  4. Asterbing
    Replies:
    0
    Views:
    2,145
    Asterbing
    Feb 17, 2006
  5. Tony Eva
    Replies:
    1
    Views:
    494
    Jeff Epler
    Nov 9, 2003
Loading...

Share This Page