Solved

Recognising changed data on a form

Posted on 2004-08-31
14
172 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
 
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
Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

 
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

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
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…
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:

760 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

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now