Recognising changed data on a form

I've got a JPanel with a number of different controls on it. The user can change data that is presented by these controls - is there a "normal" way of checking to see if any of the controls have changed on the forms rather than writing a monster IF statement?
LVL 1
howesdAsked:
Who is Participating?
 
TimYatesConnect With a Mentor Commented:
you can add listeners to the controls (after you set the default data into them), and if any of the listenerrs fire,  set a boolean flag to show that data (may) have changed....
0
 
armoghanCommented:
add change listeners to all teh components like

addTextListener(new
                            TextListener() {
                            public void textValueChanged(TextEvent e) {
                                guiChangeEventHandler();
                            }
                        });
or some components will have this
itemStateChanged(ItemEvent e) {
                                guiChangeEventHandler();
                            }
                        });

in guiChangeEventHandler();
make a flag dirty to show that some change has occured
0
 
armoghanCommented:
you can make a child class of JPanel and add the listener code into it

0
Cloud Class® Course: MCSA MCSE Windows Server 2012

This course teaches how to install and configure Windows Server 2012 R2.  It is the first step on your path to becoming a Microsoft Certified Solutions Expert (MCSE).

 
howesdAuthor Commented:
That's what I came up with ... is there a way to do that via a collection, or do I have to do it all manually?
0
 
CEHJConnect With a Mentor Commented:
You can use the following class. Call

int initialValue = TextWatcher().rememberText(yourFrame);

when you want to 'start remembering' and once again when you want to see if changes have been made. Compare the two values:

class TextWatcher {


      private int textHashcode;


      /**
       *  Returns hash code of all text components
       *
       * @param  currentComponent  Description of the Parameter
       * @return                   Description of the Return Value
       */
      public int rememberText(Component currentComponent) {
            if (currentComponent instanceof Container) {
                  Component[] children = ((Container)currentComponent).getComponents();
                  for (int i = 0; i < children.length; i++) {
                        Component c = children[i];
                        rememberText(c);
                  }
            }
            if (currentComponent instanceof javax.swing.text.JTextComponent) {
                  textHashcode += ((JTextComponent) currentComponent).getText().hashCode();
            }
            return textHashcode;
      }

}
0
 
TimYatesCommented:
Will that work CEHJ?

what happens if I just swap 2 text values over?  ie:  if I have put the address in the name field, and vice-versa, and I just swap them over...

Won't that method show that as no change?
0
 
CEHJCommented:
That's probably unlikely, but true. There's probably a simple solution to that, but can't think of it at the moment
0
 
TimYatesCommented:
Just keeping you on your toes ;-)
0
 
CEHJCommented:
LOL
0
 
CEHJCommented:
This'll fix it:

textHashcode = 31 * textHashcode + ((JTextComponent) currentComponent).getText().hashCode();
0
 
TimYatesCommented:
why 31?  Just asking...
0
 
CEHJCommented:
Prime - they use it in String.hashCode
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.