JFormattedTextField specific format

650 views Asked by At

I need to make a JFormattedTextField following the format "00:00" with some requirements:

  • The only changeable should be either of the "00". (So ":" should not be deletable)
  • Tab makes you change between the two sides of the ":". (So having the cursor on one side and tabbing marks both "00" on the other side)
  • Changing "00" to "2" should format it to "02".
  • It's character limit should be 5 including the ":". (4 changeable characters)
  • It should be initialized as "00:00", but it shouldn't be an acceptable input.
  • You shouldn't be able to type in anything else than numbers. (Letters, symbols, negative numbers etc.)

Is there a way to do this? I have looked at different formatters, validators and Document Filter to add to the JFormattedTextField, but I'm not sure which ones to go with. (Using a DefaultFormatter right now, but have looked at a NumberFormatter for the limit. Will I need to use a combination of formatters and validators?)

This is my JFormattedTextField right now: http://pastebin.com/jW2RSJXe[1] .

Anything from code that does it to examples/pointers on what to look will be appreciated!

1

There are 1 answers

2
Paul Samsotha On BEST ANSWER

Run this example. It uses a MaskFormatter. The : is there permanently, But I'm not sure how to format the other part of your question where if you only have 2, it'll show 02. You can play around with it

import java.awt.BorderLayout;
import javax.swing.JFormattedTextField;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.text.MaskFormatter;

public class MaskFormatterTest extends JPanel {

    private JFormattedTextField formatText;

    public MaskFormatterTest() {
        formatText = new JFormattedTextField(createFormatter("##:##"));
        formatText.setColumns(20);
        formatText.setText("00:00");

        setLayout(new BorderLayout());
        add(new JLabel("Enter only numbers"), BorderLayout.NORTH);
        add(formatText, BorderLayout.CENTER);
    }

    private MaskFormatter createFormatter(String s) {
        MaskFormatter formatter = null;
        try {
            formatter = new MaskFormatter(s);
        } catch (java.text.ParseException exc) {
            System.err.println("formatter is bad: " + exc.getMessage());
            System.exit(-1);
        }
        return formatter;
    }

    public static void main(String[] args) {
        javax.swing.SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                JFrame frame = new JFrame("MaskFormatter example");
                frame.add(new MaskFormatterTest());
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.setLocationByPlatform(true);
                frame.pack();
                frame.setVisible(true);
            }
        });
    }
}

The MaskFormatter sets the type value for each character at each position and the size allowed. In this case, I used ##:##. Allowing two digits, a colon, and two more digits.

Update: Added formatText.setText("00:00"); to code to initialize the text field.