[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Recognising changed data on a form

Posted on 2004-08-31
14
Medium Priority
?
215 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
12 Comments
 
LVL 35

Accepted Solution

by:
TimYates earned 100 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
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
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 100 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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

In this post we will learn different types of Android Layout and some basics of an Android App.
In this post we will learn how to make Android Gesture Tutorial and give different functionality whenever a user Touch or Scroll android screen.
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:
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.
Suggested Courses
Course of the Month19 days, 14 hours left to enroll

872 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