Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Recognising changed data on a form

Posted on 2004-08-31
14
Medium Priority
?
213 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 4
  • 2
  • +1
14 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: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying 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

This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
Suggested Courses

610 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