The process to change the
JTextField
's text to some other format like upper case for instance can be easily done by adding a DocumentFilter
to the text field component. The DocumentFilter
allow us to do a filter action for changes in the document such as insert, replace and remove.package org.best.example.swing; import javax.swing.text.DocumentFilter; import javax.swing.text.BadLocationException; import javax.swing.text.AttributeSet; import javax.swing.text.AbstractDocument; import javax.swing.*; import java.awt.*; public class DocumentFilterExample extends JFrame { public DocumentFilterExample() throws HeadlessException { initComponents(); } protected void initComponents() { setSize(200, 200); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); getContentPane().setLayout(new FlowLayout(FlowLayout.LEFT)); DocumentFilter filter = new UppercaseDocumentFilter(); JTextField firstName = new JTextField(); firstName.setPreferredSize(new Dimension(100, 20)); ((AbstractDocument) firstName.getDocument()).setDocumentFilter(filter); JTextField lastName = new JTextField(); lastName.setPreferredSize(new Dimension(100, 20)); ((AbstractDocument) lastName.getDocument()).setDocumentFilter(filter); getContentPane().add(firstName); getContentPane().add(lastName); } public static void main(String[] args) { SwingUtilities.invokeLater(new Runnable() { public void run() { new DocumentFilterExample().setVisible(true); } }); } class UppercaseDocumentFilter extends DocumentFilter { // // Override insertString method of DocumentFilter to make the text format // to uppercase. // public void insertString(DocumentFilter.FilterBypass fb, int offset, String text, AttributeSet attr) throws BadLocationException { fb.insertString(offset, text.toUpperCase(), attr); } // // Override replace method of DocumentFilter to make the text format // to uppercase. // public void replace(DocumentFilter.FilterBypass fb, int offset, int length, String text, AttributeSet attrs) throws BadLocationException { fb.replace(offset, length, text.toUpperCase(), attrs); } } }
No comments:
Post a Comment