1

How can I create a method that automaticly adds a character when the user fills in a certain amount of characters.

Let's say I have to enter a code that is 12 characters long.
enter image description here

Each time I pass the fourth character a dash must be added. (4-5,8-9) in total 2 dashes.

enter image description here

I am a bit new to Java so I don't know where to start.

Thanks in advance.

Rafael
  • 788
  • 2
  • 17
  • 38
  • Create an event for OnChange/ keypress and in the code for keypress it would just be something like `if (Textbox.length % 4 == 0) {Textbox.Text += '-'; }` (Very much simplified) KeyPress event looks like it is here http://stackoverflow.com/questions/24018373/is-there-an-onchange-for-java – Austin T French Jan 08 '15 at 15:04

1 Answers1

4

Use a JFormattedTextField for which you can define an input mask

JFormattedTextField textField = new JFormattedTextField();
textField.setFormatterFactory(new DefaultFormatterFactory(new MaskFormatter("HHHH-HHHH-HHHH")));

edit add a working example

public class SimpleInputMask {

    private static void createAndShowGUI() {
        JFrame frame = new JFrame("MaskFormatteExample");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        JPanel panel = new JPanel();
        JLabel label = new JLabel("input: ");
        panel.add(label);

        // define the text field with an input mask
        JFormattedTextField textField = new JFormattedTextField();
        textField.setFont(new Font(Font.MONOSPACED, Font.PLAIN, 14));
        try {
            String inputMask = "HHHH-HHHH-HHHH";
            textField.setFormatterFactory(new DefaultFormatterFactory(new MaskFormatter(inputMask)));
        } catch (ParseException ex) {
            // will be raised if the inputMask cannot be parsed
            // add your own exception handling here
        }

        panel.add(textField);

        frame.add(panel);
        frame.pack();
        frame.setVisible(true);
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                createAndShowGUI();
            }
        });
    }
}
SubOptimal
  • 22,518
  • 3
  • 53
  • 69
  • Nice. I didn't even know such a thing exists. But OP needs 12 characters, so maybe `"####-####-####"` or `"AAAA-AAAA-AAAA"` – tobias_k Jan 08 '15 at 15:14
  • @tobias_k I amend the mask to 12 digits. – SubOptimal Jan 08 '15 at 15:22
  • I use swing components on a jpanel, do I put this code begin the txtfield or at the beginning of the class with a try-catch? – Rafael Jan 08 '15 at 15:50
  • @Mr.Goose you should put it at the place you currently define you JTextField. – SubOptimal Jan 09 '15 at 11:14
  • @Mr.Goose I add a small example to show how to use it. – SubOptimal Jan 09 '15 at 11:47
  • @SubOptimal That's pretty neat. Some letters are not working and some are, what is that actually? and if I store this in a database does the dashes also come with it? – Rafael Jan 09 '15 at 11:59
  • @Mr.Goose The input mask currently only accept "hex character (0-9, a-f or A-F)." Have a look here for all posible mask parameters https://docs.oracle.com/javase/7/docs/api/. If you call `textField.getText()` you will get the entered characters including the dashes. – SubOptimal Jan 09 '15 at 12:22
  • @SubOptimal I've build up my gui with swing controls in the design view, by dragging and dropping the controls. Obviously this is a different method than you use, like only coding it. How can I obtain this then? (reffering to your code) – Rafael Jan 09 '15 at 12:28
  • As this depends on your IDE you should rather show in the documentation of your IDE or ask another question. – SubOptimal Jan 12 '15 at 11:20