Page 2 of 2 FirstFirst 12
Results 21 to 24 of 24
  1. #21
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    An example of a DocumentListener together with a DocumentFilter. The listener takes the data from the document, multiplies it by two and shows the result in the next JTextField. The DocumentFilter allows only int input:

    Java Code:
    import javax.swing.*;
    import javax.swing.event.*;
    import javax.swing.text.*;
    
    @SuppressWarnings("serial")
    public class GloboExample extends JPanel {
        private JTextField entryField = new JTextField(10);
        private JTextField resultField = new JTextField(10);
        
        public GloboExample() {
            PlainDocument doc = (PlainDocument) entryField.getDocument();
            doc.setDocumentFilter(new NumberDocFilter());
            doc.addDocumentListener(new MyDocListener());
            
            resultField.setEditable(false);
            resultField.setFocusable(false);
            
            add(entryField);
            add(new JLabel(" x 2 = "));
            add(resultField);
        }
        
        private class MyDocListener implements DocumentListener {
            private void doCalc() {
                if (entryField.getText().isEmpty()) {
                    resultField.setText("");
                    return;
                }
                int value = Integer.parseInt(entryField.getText());
                String result = String.valueOf(2 * value);
                resultField.setText(result);
            }
    
            public void removeUpdate(DocumentEvent arg0) {
                doCalc();
            }
            
            public void insertUpdate(DocumentEvent arg0) {
                doCalc();
            }
            
            public void changedUpdate(DocumentEvent arg0) {
                doCalc();
            }
        }
    
    
        public static void main(String[] args) {
            SwingUtilities.invokeLater(new Runnable() {
                public void run() {
                    createGui();
                }
            });
        }
    
        private static void createGui() {
            JFrame frame = new JFrame("Globo Example");
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            frame.getContentPane().add(new GloboExample());
            frame.pack();
            frame.setLocationRelativeTo(null);
            frame.setVisible(true);
        }
    
    }
    
    class NumberDocFilter extends DocumentFilter {
    
        private boolean testString(String text) {
            if (text.isEmpty()) {
                return true;
            }
            try {
                Integer.parseInt(text);
            } catch (NumberFormatException e) {
                return false;
            }
            return true;
        }
    
        @Override
        public void insertString(FilterBypass fb, int offset, String insertString,
                AttributeSet attr) throws BadLocationException {
            Document doc = fb.getDocument();
            String text = doc.getText(0, doc.getLength());
            StringBuilder sb = new StringBuilder(text);
            sb.insert(offset, insertString);
            if (testString(sb.toString())) {
                super.insertString(fb, offset, insertString, attr);
            } else {
                java.awt.Toolkit.getDefaultToolkit().beep();
            }
        }
    
        @Override
        public void replace(FilterBypass fb, int offset, int length,
                String replaceText, AttributeSet attrs) throws BadLocationException {
            Document doc = fb.getDocument();
            String text = doc.getText(0, doc.getLength());
            StringBuilder sb = new StringBuilder(text);
            sb.replace(offset, offset + length, replaceText);
    
            if (testString(sb.toString())) {
                super.replace(fb, offset, length, replaceText, attrs);
            } else {
                java.awt.Toolkit.getDefaultToolkit().beep();
            }
        }
    
        @Override
        public void remove(FilterBypass fb, int offset, int length)
                throws BadLocationException {
            Document doc = fb.getDocument();
            String text = doc.getText(0, doc.getLength());
            StringBuilder sb = new StringBuilder(text);
    
            sb.delete(offset, offset + length);
            if (testString(sb.toString())) {
                super.remove(fb, offset, length);
            } else {
                java.awt.Toolkit.getDefaultToolkit().beep();
            }
        }
    }
    Enjoy

  2. #22
    globo is offline Member
    Join Date
    Sep 2010
    Posts
    61
    Rep Power
    0

    Default

    Interesting thanks a lot. I will apply the same codes to my project.

  3. #23
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Of course it is over-simplified and has a bug. Enter 1555..., and keep entering 5's until the DocumentFilter stops you because you're running into the Integer.MAX_VALUE and you'll see what I mean.

  4. #24
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,352
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by Fubarable View Post
    Of course it is over-simplified and has a bug. Enter 1555..., and keep entering 5's until the DocumentFilter stops you because you're running into the Integer.MAX_VALUE and you'll see what I mean.
    That MAX_VALUE is 0x7fffffff and the auto repeat rate of my keyboard is set to approximately 30ms so it'll take me more than 745 days to overflow that thing; thank you very much Sir ;-)

    kind regards,

    Jos

Page 2 of 2 FirstFirst 12

Similar Threads

  1. Finding a number in array close to another number
    By SteroidalPsycho in forum New To Java
    Replies: 2
    Last Post: 02-15-2010, 12:37 AM
  2. Printing the Number of Times a Number in a Range Shows up
    By space4rent00 in forum New To Java
    Replies: 1
    Last Post: 02-05-2010, 10:42 PM
  3. Replies: 1
    Last Post: 08-07-2009, 12:14 PM
  4. checking for an event during an event
    By infinity in forum AWT / Swing
    Replies: 22
    Last Post: 04-09-2009, 01:08 AM
  5. Replies: 1
    Last Post: 11-03-2008, 02:38 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
  •