Results 1 to 2 of 2
  1. #1
    Ralphw is offline Member
    Join Date
    Feb 2009
    Posts
    9
    Rep Power
    0

    Default JFormattedTextField / Formatter / OverWriteMode

    Hi *

    i am struggeling with a behavior of the JFormattedTextField

    See example. If i type in 4 digits number values it works.
    The cursor position is after the 4. digit.

    If i type more numbers they will be overwritten from right to left.
    I do not want this behavior.
    I thought setting OverWriteMode to false should stop this, but it does not work.
    Any ideas ?.

    Many Thanx
    Ralph
    XML Code:
    public class JFormattedTF extends JFrame {
    
      public JFormattedTF() {
        JPanel panel = new JPanel();
        JLabel label = new JLabel("Number Format :");
           
        NumberFormat nf = NumberFormat.getInstance();
        nf.setMaximumIntegerDigits(4);
        nf.setGroupingUsed(false);
       
        NumberFormatter formatter =  new NumberFormatter(nf); 
        formatter.setAllowsInvalid(false);
        formatter.setOverwriteMode(false);
        
        JFormattedTextField tf = new JFormattedTextField(
            formatter);
        tf.setColumns(10);
        tf.setHorizontalAlignment(JTextField.LEFT);
        panel.add(label);
        panel.add(tf);
    
        getContentPane().add(panel, BorderLayout.SOUTH);
        pack();
      }
    
      public static void main(String[] args) {
        JFormattedTF tfe = new JFormattedTF();
        tfe.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        tfe.setVisible(true);
      }
     
    }

  2. #2
    RamyaSivakanth's Avatar
    RamyaSivakanth is offline Senior Member
    Join Date
    Apr 2009
    Location
    Chennai
    Posts
    832
    Rep Power
    6

    Default

    Hi,
    No clue regarding your problem.I think some issue in Java level itself.

    But,you can use MaskFormatter instead of NumberFormatter
    like this below.

    MaskFormatter mf = new MaskFormatter("####");

    JFormattedTextField tf = new JFormattedTextField(mf);

    This will allow only 4 number values.Just try it out and let me know in case of any issues.

    -Regards
    Ramya
    Ramya:cool:

Similar Threads

  1. JFormattedTextField + SimpleDateFormat
    By Ralphw in forum AWT / Swing
    Replies: 1
    Last Post: 07-07-2009, 10:53 AM
  2. Null Pointer Exception for formatter object
    By andre1011 in forum Networking
    Replies: 0
    Last Post: 03-28-2009, 10:51 PM
  3. Problems with JFormattedTextField
    By Gatts79 in forum New To Java
    Replies: 0
    Last Post: 10-03-2008, 04:59 PM
  4. JFormattedTextField Issue
    By teracomp in forum AWT / Swing
    Replies: 1
    Last Post: 01-21-2008, 06:55 PM
  5. Using java.util.Formatter
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 11-16-2007, 02:29 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •