Document doc = new MaxLengthDocument(5); // set maximum length to 5
JTextField field = new JTextField(doc, "", 8);
JPanel flowPanel = new JPanel();
flowPanel.add(field);
JFrame frame = new JFrame("MaxLengthDocument demo");
frame.setContentPane(flowPanel);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(160, 80);
frame.setVisible(true);