Results 1 to 7 of 7
  1. #1
    kbro3 is offline Member
    Join Date
    Feb 2011
    Posts
    24
    Rep Power
    0

    Default Multiple JTextField updates

    Having an issue where I want three JTextFields to be updated dependant on each other.

    Basically, I want my three fields to update like so:

    field A updates field B and C
    field B updates field A and C
    field C updates field A and B

    I've set up DocumentListeners and the code goes into insertUpdate(), however if I only update two fields, it works fine. If I try to update all, I get an Attempt To mutate in notification exception.

    Any ideas?

    Thanks

  2. #2
    kbro3 is offline Member
    Join Date
    Feb 2011
    Posts
    24
    Rep Power
    0

    Default

    I thought i'd post my code to make it clearer what I mean.
    I also read that I should wrap my documentlistener code in SwingUtilities.invokeLater(), but if I do that, I get a never ending thread:

    Java Code:
    
    import javax.swing.*;
    import javax.swing.text.Document;
    import javax.swing.event.*;
    
    /**
     *
     * @author 
     */
    public class MW extends JFrame{
    
        private JLabel lblA;
        private JLabel lblB;
        private JLabel lblC;
        private JTextField fieldA;
        private JTextField fieldB;
        private JTextField fieldC;
    
    
        public MW(){
    
            lblA = new JLabel();
            lblB = new JLabel();
            lblC = new JLabel();
    
            fieldA = new JTextField();
            fieldB = new JTextField();
            fieldC = new JTextField();
    
            lblA.setText("A");
            lblB.setText("B");
            lblC.setText("C");
    
    
    
    class AUpdate implements Runnable{
    
    
        public void run(){
    
            fieldB.setText(fieldA.getText() + 3);
            fieldC.setText(fieldA.getText() + 4);
            
        }
    
    }
    
            fieldA.getDocument().addDocumentListener(new DocumentListener(){
    
                public void changedUpdate(DocumentEvent de){
    
                    System.out.println("A changed");
                }
    
                public void removeUpdate(DocumentEvent de){
    
                    System.out.println("A remove");
    
                }
    
                public void insertUpdate(DocumentEvent de){
    
                    System.out.println("A insert");
                    SwingUtilities.invokeLater(new AUpdate());
                   // fieldB.setText(fieldA.getText() + 5);
                   // fieldC.setText(fieldA.getText() + 6);
                    
                }
    
    
    
            });
    
            
    class FieldUpdate implements Runnable{
    
        public void run(){
    
    
            fieldB.setText(fieldC.getText() + 9);
            fieldA.setText(fieldC.getText() + 8);
        }
    
    
    
    }
    
            fieldB.getDocument().addDocumentListener(new DocumentListener(){
    
                public void changedUpdate(DocumentEvent de){
    
                    System.out.println("B changed");
                }
    
                public void removeUpdate(DocumentEvent de){
    
                    System.out.println("B remove");
    
                }
    
                public void insertUpdate(DocumentEvent de){
    
                    System.out.println("B insert");
                    //fieldA.setText(fieldB.getText() + 10);
                    //fieldC.setText(fieldB.getText() + 20);
    
                }
    
    
    
            });
    
            fieldC.getDocument().addDocumentListener(new DocumentListener(){
    
                public void changedUpdate(DocumentEvent de){
    
                    System.out.println("C changed");
                }
    
                public void removeUpdate(DocumentEvent de){
    
                    System.out.println("C remove");
    
                }
    
                public void insertUpdate(DocumentEvent de){
    
                    SwingUtilities.invokeLater(new FieldUpdate());
                   // fieldA.setText(fieldC.getText() + 35);
    
                }
    
    
    
            });
    
    
    
    
    
            javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
            getContentPane().setLayout(layout);
            layout.setHorizontalGroup(
                layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(layout.createSequentialGroup()
                    .addContainerGap()
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addComponent(fieldA, javax.swing.GroupLayout.PREFERRED_SIZE, 79, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addComponent(lblA))
                    .addGap(32, 32, 32)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addComponent(fieldB, javax.swing.GroupLayout.PREFERRED_SIZE, 89, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addComponent(lblB))
                    .addGap(18, 18, 18)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addComponent(lblC)
                        .addComponent(fieldC, javax.swing.GroupLayout.PREFERRED_SIZE, 81, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addContainerGap(158, Short.MAX_VALUE))
            );
            layout.setVerticalGroup(
                layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(layout.createSequentialGroup()
                    .addGap(70, 70, 70)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(lblA)
                        .addComponent(lblB)
                        .addComponent(lblC))
                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(fieldA, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addComponent(fieldB, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addComponent(fieldC, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addContainerGap(239, Short.MAX_VALUE))
            );
    
            
    
    
    
    
            this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            this.setVisible(true);
            pack();
    
            
        }
    
    }
    Thanks

  3. #3
    camickr is offline Senior Member
    Join Date
    Jul 2009
    Posts
    1,234
    Rep Power
    7

    Default

    You get an infinite loop because you continually update the text fields.

    If you can a you update b and c. So then when you update b, it tries to update a and c.

    You need to remove the Document listener from all the text fields before you attempt to change the dependent fields. After the update is done you can add the listeners back to each text field.

  4. #4
    kbro3 is offline Member
    Join Date
    Feb 2011
    Posts
    24
    Rep Power
    0

    Default

    Ah thanks, that makes sense, so A starts a thread, which updates B and C and C updates A which starts the thread again, etc etc.

    Now, I'm not sure how to remove the document listener without its' name:


    The code below requires the documentListener name, which I don't have:
    Java Code:
    class FieldUpdate implements Runnable{
    
        public void run(){
    
            fieldA.getDocument().removeDocumentListener();
            fieldB.setText(fieldC.getText() + 9);
            fieldA.setText(fieldC.getText() + 8);
        }
    }
    Thanks!

  5. #5
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,789
    Rep Power
    7

    Default

    You can get the listeners attached to a Component.

  6. #6
    camickr is offline Senior Member
    Join Date
    Jul 2009
    Posts
    1,234
    Rep Power
    7

    Default

    You defined your text fields as class variables. You can always do the same thing for the DocumentListeners.

  7. #7
    kbro3 is offline Member
    Join Date
    Feb 2011
    Posts
    24
    Rep Power
    0

    Default

    Thanks guys,

    Did as you said and redefined the documentListeners, then removed and re-added them after processing code. Works as expected.

    Thanks for such quick and helpful replies! :)

Similar Threads

  1. Automatic Updates...How to stop them?
    By Otto in forum New To Java
    Replies: 5
    Last Post: 05-26-2010, 08:18 AM
  2. Java JDK updates on Mac?
    By QualityControl in forum New To Java
    Replies: 4
    Last Post: 03-24-2010, 04:57 PM
  3. Multiple Queries using batch Updates
    By swati.jyoti in forum JDBC
    Replies: 3
    Last Post: 05-12-2009, 10:40 AM
  4. Replies: 1
    Last Post: 01-30-2009, 06:44 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
  •