Solved

Recognising changed data on a form

Posted on 2004-08-31
14
202 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 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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering 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

Suggested Solutions

Title # Comments Views Activity
arguments to jar 5 40
Detect Closed Loops (circles, figure-8s, etc) in PNG Images 6 77
eclipse console opening separately 2 38
ejb entity bean example 2 38
Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.
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.

726 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