Solved

Recognising changed data on a form

Posted on 2004-08-31
14
190 Views
Last Modified: 2010-03-31
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?
0
Comment
Question by:howesd
  • 5
  • 4
  • 2
  • +1
14 Comments
 
LVL 35

Accepted Solution

by:
TimYates earned 25 total points
ID: 11942050
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
 
LVL 18

Expert Comment

by:armoghan
ID: 11942111
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
 
LVL 18

Expert Comment

by:armoghan
ID: 11942132
you can make a child class of JPanel and add the listener code into it

0
Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

 
LVL 1

Author Comment

by:howesd
ID: 11942226
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
 
LVL 86

Assisted Solution

by:CEHJ
CEHJ earned 25 total points
ID: 11945652
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
 
LVL 35

Expert Comment

by:TimYates
ID: 11949967
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
 
LVL 86

Expert Comment

by:CEHJ
ID: 11950550
That's probably unlikely, but true. There's probably a simple solution to that, but can't think of it at the moment
0
 
LVL 35

Expert Comment

by:TimYates
ID: 11950558
Just keeping you on your toes ;-)
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 11950600
LOL
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 11950711
This'll fix it:

textHashcode = 31 * textHashcode + ((JTextComponent) currentComponent).getText().hashCode();
0
 
LVL 35

Expert Comment

by:TimYates
ID: 11950722
why 31?  Just asking...
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 11950913
Prime - they use it in String.hashCode
0

Featured Post

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.

770 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question