?
Solved

Changing a value from a method

Posted on 2011-10-24
62
Medium Priority
?
219 Views
Last Modified: 2012-05-12
when I open my dialog I am checking to make sure that it is the only dialog open using a IF

if (flag == 0) {
	    showediDialog(theSession);
	}
	return null;
	}

Open in new window


In the dialog method I am changing the value of flag

private void showediDialog(TCSession theSession) {
	   flag = 1; 
	   EdiBaseDialog ar = new EdiBaseDialog(null, theSession);
		 ar.setLocationRelativeTo(null); 
		 ar.setModal(false);
		 ar.setVisible(true);
   }    

Open in new window


Then I have written a small method to change the value back to 0

public int dialogCheck() {
	   flag = 0;
	   return flag;
   }

Open in new window


In my Cancel button I have this

 cancelButton.addActionListener(new ActionListener() {
         public void actionPerformed(ActionEvent e) {
        	 edh = new EdiDialogHandler();
        	 edh.dialogCheck();
        	 dispose();
         }
      });

Open in new window


Everything works except the cancel button don't seem to change the value of flag - where is the logic gone wrong?
0
Comment
Question by:jkteater
  • 30
  • 24
  • 8
62 Comments
 
LVL 47

Expert Comment

by:for_yan
ID: 37018419
are you calling this dialogCheck ?
put System.out.println("chab=nging flag");
next to flag = 0;'


0
 
LVL 47

Expert Comment

by:for_yan
ID: 37018436
You don't need to have flag to make sure you have only one dialog

Make the dilaog hande itself, I guess "ar" in you code instance variable

at the top of the class:
 EdiBaseDialog   ar;

in method:
if(ar == null) ar =   new EdiBaseDialog(null, theSession);
0
 
LVL 47

Expert Comment

by:for_yan
ID: 37018456
meybe you'll need a method in this original class when you dispose of the dialog to set
ar to null. I think it wil even do it itself, but it is safer to have.
Still it is better than to have a flag

 
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.

 

Author Comment

by:jkteater
ID: 37018488
You are correct that does keep it to 1 dialog :)  but what call would I put in my cancel button to pretty much remove all the information in the dialog?  Right now if I close it and open it back it has the same information - which I am ok with that, but I want the cancel button to remove everything so when the it is opened again it will be blank
0
 
LVL 47

Expert Comment

by:for_yan
ID: 37018542
you just make er = null before you dispose of it - and then all information is effectively gone.
Next time you create it with constructor (new) it will be blank as the previous time.

You want it to be blank -as created new - correct ?


If you want to keep it and just make it invisible for seome time then you just make setVisible(false) and setVisdible(true)
 


0
 

Author Comment

by:jkteater
ID: 37018574
I will try to make er = null before you dispose of it
0
 
LVL 47

Expert Comment

by:for_yan
ID: 37018591
And you should not be conccenred where is all the stuff that was drawing it
after you set it to null and dispose of it - eventually garbage collection will deal with it - but you should not worry this is jvm's business
0
 

Author Comment

by:jkteater
ID: 37018622
I am not sure how I would make it NULL?  
0
 

Author Comment

by:jkteater
ID: 37018632
maybe I do give me a second
0
 

Author Comment

by:jkteater
ID: 37018641
I guess not
0
 

Author Comment

by:jkteater
ID: 37018650
Here is my dialog call

 
private void showediDialog(TCSession theSession) {
	   if(ar == null) ar =   new EdiBaseDialog(null, theSession); 
	   //EdiBaseDialog ar = new EdiBaseDialog(null, theSession);
		 ar.setLocationRelativeTo(null); 
		 ar.setModal(false);
		 ar.setVisible(true);
   }    

Open in new window


Here is the code for the cancel button

EdiDialogHandler ar;

Open in new window


cancelButton.addActionListener(new ActionListener() {
         public void actionPerformed(ActionEvent e) {
        	ar = null;
        	 dispose();
         }
      });

Open in new window


What am I missing
0
 

Author Comment

by:jkteater
ID: 37018699
do I need a method in the class that is calling the dialog that will change the ar value then call something like

ar.setNull(); in the cancel button?
0
 
LVL 47

Expert Comment

by:for_yan
ID: 37018739
yes

 setNull(){
ar = null;
}

should be called before disposing. before disposing

but itshould be invokled not on ar
but on instance of your class which contain all this code and method should be in this class  - in this same class wehere you create this dialog
0
 

Author Comment

by:jkteater
ID: 37018780
This is what I have

cancelButton.addActionListener(new ActionListener() {
         public void actionPerformed(ActionEvent e) {
        	 edh = new EdiDialogHandler();
        	 edh.setNull();
        	dispose();
         }
      });

Open in new window



This method is in  EdiDialogHandler

public void setNull() {
      ar = null; 
   }

Open in new window


This method is in  EdiDialogHandler

private void showediDialog(TCSession theSession) {
	   if(ar == null) ar = new EdiBaseDialog(null, theSession); 
	   ar.setLocationRelativeTo(null); 
	   ar.setModal(false);
	   ar.setVisible(true);
   }    

Open in new window


THis is the rest of EdiDialogHandler

public class EdiDialogHandler extends AbstractHandler {
	 EdiBaseDialog ar;
	
	TCSession theSession = null;
	
	/* The constructor */
	public EdiDialogHandler() {
	}
    
	@Override
   public Object execute(final ExecutionEvent event) throws ExecutionException {
	  
	try {
		theSession = EdiInitialization.Initialize();
	} catch (TCException e) {
		e.printStackTrace();
	}
	   showediDialog(theSession);
		return null;
	}

Open in new window


Everything seems to work no errors - but it still opens with the data in the dialog already
0
 
LVL 47

Expert Comment

by:for_yan
ID: 37018792
Impossible
0
 
LVL 47

Expert Comment

by:for_yan
ID: 37018797
Post what you are doing in construictor
0
 

Author Comment

by:jkteater
ID: 37018844
I put some print statements in

public void setNull() {
	   System.out.println("setNull : " + ar + "\n");
	   ar = null; 
   }

Open in new window


 private void showediDialog(TCSession theSession) {
	   System.out.println("show : " + ar + "\n");
	   if(ar == null) ar = new EdiBaseDialog(null, theSession); 
	   //EdiBaseDialog ar = new EdiBaseDialog(null, theSession);
		 ar.setLocationRelativeTo(null); 
		 ar.setModal(false);
		 ar.setVisible(true);
   }    

Open in new window


When I first open the dialog - prints "show null"
if I close the dialog without using cancel and open it up - print "show bunch of text"

So i know that ar is not null right now.

close the dialog using the cancel button - prints "setNull null"

So I would assume that ar is null

but when I open it again - prints  "show bunch of text"

It seems like the ar I am setting to null is not the same ar in the if check

0
 

Author Comment

by:jkteater
ID: 37018862
constructor for where the dialog is called

public EdiDialogHandler() {
	}

Open in new window

constructor for the cancel button class

 
public EdiBaseDialog(Frame parent, TCSession theSession){
	   super(parent, false);
	   session = theSession;
	   myModel = SingletonSelectTable.getInstance();
	   myModel.setEdb(this);
	   
	   createDialog();  
   } //end Constructor

Open in new window

0
 
LVL 86

Expert Comment

by:CEHJ
ID: 37018885
>>
         public void actionPerformed(ActionEvent e) {
               edh = new EdiDialogHandler();
               edh.dialogCheck();
               dispose();
         }

>>

Make sure the variable you're calling 'flag' is the variable you think it is
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 37018897
e.g. if you're creating arbitrary numbers of dialogs, the only way that variable is going to work is if it persists across new instances of the dialogs. Normally it would be an instance variable of the class that's creating them
0
 
LVL 47

Expert Comment

by:for_yan
ID: 37019002
The reason why you are getting the same is because your myModel = SingletonSelectTable.getInstance() - is alaways the same
- so you ppopulated it with asome list of data - and it is still there - so it credates
new dialog and populateds it with the same data.
So before you go waay from you previous user/dialog - just where you cll settuing null youir dalog -
you should alos re-create the ArrayList whixch contains your previous data
I don't rememeber but you shou,ld have aList = new ArrayList<...>() at the very same point.
Then it will not be populated with stuff when you create a new dialog.
That;'s waht I'm guessing basdd on previous recollections
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 37019044
The question is why the flag doesn't change:

>>Everything works except the cancel button don't seem to change the value of flag - where is the logic gone wrong?
0
 

Author Comment

by:jkteater
ID: 37019257
The data I am referring to is not the list data.  I am not even populating the list when I am testing it
0
 
LVL 47

Expert Comment

by:for_yan
ID: 37019283
So what can be left there?

You should understand that once you closed your dialog, said ar = null
and next time you are executing ar = new   EdiBaseDialog(null, theSession);
even though you have the same name "ar" in your code
the object you create has absolutely nothing to do with your prevuiius one
It just goes through all the code in your constructor and creates new ovbject
If it uses soem persistemt indformation - like underrlying
arrays or lists - and you have the singletomn instance of this model - which all
is still there - ththat would be reflected - but there is no remanants of your previous
object - whatever you see was createv anew with your constructor and even placed in different
placce in physical memor - does not matter that you have the saem variable naming it in your code.
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 37019318
If we can get back to the actual question, where is this variable 'flag' jkteater?
0
 
LVL 47

Expert Comment

by:for_yan
ID: 37019319
So you should forget about previous dialog and look at this code:

myModel = SingletonSelectTable.getInstance();
         myModel.setEdb(this);
        
         createDialog();

and try to understadn waht variables are you using here which still conain the old values
and populate your brand new dialog with the old  data - and you should re-create those
variables at the moment when you realize that you finshed working with the previous dialog/user ot whatever that old object is.
0
 
LVL 47

Expert Comment

by:for_yan
ID: 37019337
Varaible flag would not change anything.
If you are recreating the same dialog it hppens because when you creating
it you are relying on some old data.
Variable lag was only used for preventing to create new dialog if the old one exists - you can do it through flag or through
checking null on the dialog itself, but it re-creates the old dialog viewe beacuse underlying data it uses in cosntrcution of the dialog
were not cleared
0
 
LVL 47

Expert Comment

by:for_yan
ID: 37019387
So if in your createDialog() method you are using this myModel instance - rememeber that your
myModel is singleton - so with that
myModel = SingletonSelectTable.getInstance();
you are getting al the time the same instance - so whatever instance varibales your myModel had pervious time
class they will all be the same unless you reinitialized them.
So you need to re-initialize them at the moment when you want to start a new cycle/user/case.
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 37019410
jkteater, there's a lot of noise in this question. Can you please restate the problem? If you think it's changed for some reason, please say so
0
 

Author Comment

by:jkteater
ID: 37019476
Basically when the user clicked the menu action to open the dialog, they could click it 3 times or more and 3 or more dialogs would will open up.  

I only want one dialog to be open, once the user is done with the dialog  I want them to click the cancel button and then when they click the menu again a new dialog opens with no data.

I had put a if statement in where the dialog is being called and that would stop having multiple dialogs, then i had a small method that changed the flag so when the if statement ran agiain after the user clicked the menu it would create a new dialog.  The method ran from the cancel button click.  But even though the prints out where saying that the method was changing the value, the if statement never seen the new value.  That is the issue pretty much in a nut shell
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 37019496
>>the if statement never seen the new value.  

So what happened when you investigated the nature/scope of 'flag' as i suggested?
0
 
LVL 47

Expert Comment

by:for_yan
ID: 37019498
But now you acheived that it does not allow you to open second wibndow while previous omne is opopene - correct?
So the issue is that you open second time the dialog - and it looks the same as the one you closed - is it corect status as of now?
0
 
LVL 47

Expert Comment

by:for_yan
ID: 37019518
show the place where calling method dialogCheck()
even though it really is not important anow abiut this flag, becuase checking for null on the
dialog itself is definitely no worse than introducing this flag
0
 

Author Comment

by:jkteater
ID: 37019537
CEHJ - It still did not seem to change the value before the IF ran again. Unless I had it coded wrong which is possible

for_yan - It does not open a second dialog if the first one is opened, the status of the issue is when I click cancel and then click to open a new dialog, I need it to be empty, like the first time the dialog is created. RIght now data is still in it.
0
 

Author Comment

by:jkteater
ID: 37019550
private JPanel OKCancelButtons() {
	  
	  
      JButton cancelButton = new JButton("Cancel");
      cancelButton.setEnabled(true);
      cancelButton.addActionListener(new ActionListener() {
         public void actionPerformed(ActionEvent e) {
        	 edh = new EdiDialogHandler();
        	 edh.setNull();
        	dispose();
         }
      });
            
      JPanel p = new JPanel();
      //p.setBorder(BorderFactory.createLineBorder(Color.red));
      p.setLayout(new BoxLayout(p, BoxLayout.LINE_AXIS));
      p.add(myModel.getButton());
      p.add(Box.createHorizontalGlue());
      p.add(myModel.getOKButton());
      p.add(cancelButton);

      return p;
   }// end OKCancelButtons()

Open in new window


CAlling this method
 public void setNull() {
	   System.out.println("setNull : " + ar + "\n");
	   ar = null; 
   }

Open in new window

0
 
LVL 47

Expert Comment

by:for_yan
ID: 37019554

I explained to you what may be the reason that you see the same dialog

Post your createDialog() method

and forget abot the flag - it is no longer relevant - we achieved the
result wfor which it was created with a better way
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 37019556
>>CEHJ - It still did not seem to change the value before the IF ran again. Unless I had it coded wrong which is possible

As i mentioned, the likelihood, if it's not changing the value, is that it's not the value you think it is. It needs to be a single GLOBAL variable for it to work
0
 

Author Comment

by:jkteater
ID: 37019557
This is the whole class that creates the dialog when the user clicks a menu action


public class EdiDialogHandler extends AbstractHandler {
	 EdiBaseDialog ar;
	
	TCSession theSession = null;
	
	/* The constructor */
	public EdiDialogHandler() {
		
	}
    
	@Override
   public Object execute(final ExecutionEvent event) throws ExecutionException {
	  
	try {
		theSession = EdiInitialization.Initialize();
	} catch (TCException e) {
		e.printStackTrace();
	}
	   showediDialog(theSession);
		return null;
	}
   
   
   private void showediDialog(TCSession theSession) {
	   System.out.println("show : " + ar + "\n");
	   if(ar == null) ar = new EdiBaseDialog(null, theSession); 
	   //EdiBaseDialog ar = new EdiBaseDialog(null, theSession);
		 ar.setLocationRelativeTo(null); 
		 ar.setModal(false);
		 ar.setVisible(true);
   }    

   public void setNull() {
	   System.out.println("setNull : " + ar + "\n");
	   ar = null; 
   }

}
  

Open in new window

0
 

Author Comment

by:jkteater
ID: 37019570
public void createDialog() {
	   
	  appReg = Registry.getRegistry(this);
	  setTitle(appReg.getString("edi.TITLE"));
	  	  
	  Component selectionsPanel = selectedTable();
	  Component currentPanel = currentItems();
	  Component buttonPanel = OKCancelButtons();
	  centerPanel = new JPanel();
      centerPanel.setLayout(new BoxLayout(centerPanel, BoxLayout.PAGE_AXIS));
      centerPanel.setPreferredSize(new Dimension(750, 350));
      centerPanel.add(Box.createRigidArea(new Dimension(5, 0)));
      centerPanel.add(currentPanel);
      centerPanel.add(selectionsPanel);
      centerPanel.add(Box.createHorizontalGlue());
      centerPanel.add(Box.createRigidArea(new Dimension(0, 2)));
      centerPanel.add(buttonPanel);
      
      getContentPane().add(centerPanel);
      
      addComponentListener(new ComponentAdapter() {
        public void componentResized(ComponentEvent event) {
           Component c = event.getComponent();
           c.setSize(Math.max((int)centerPanel.getPreferredSize().getWidth()/2, c.getWidth()),
                     Math.max((int)centerPanel.getPreferredSize().getHeight()/2, c.getHeight()));
        }
      });
       	  	
      this.pack();
      setLocation(getParent().getLocation(null));        
      centerPanel.setVisible(true);
    }// end createDialog()
   

Open in new window

0
 
LVL 47

Expert Comment

by:for_yan
ID: 37019575

what is EdiDialogHandler class ?

        edh = new EdiDialogHandler();
               edh.setNull();
              dispose();

you should not create it and then use it to setNull()
you should get to the same instance of your class from which you creted this dialog.
when you say
 edh = new EdiDialogHandler();
you create new uinsatnce and you change absolutely
different parameter
0
 

Author Comment

by:jkteater
ID: 37019583
EdiDialogHandler is the class above that is creating the dialogs
0
 
LVL 47

Expert Comment

by:for_yan
ID: 37019588
We went with you already rthrough that many times

Let's start from the beginning - enumearte what classes you have once again -
and last time wwe atrted form that and I thought you understood that - lets start again

Enumaerate what classes you have an where you create your dialog and where you use it.
0
 
LVL 47

Expert Comment

by:for_yan
ID: 37019596
You should understand what is instance of the class and waht are insrtance variables and you should refer to the same instance not just arbirary create new ones
and execute methods on the new instances.
0
 

Author Comment

by:jkteater
ID: 37019600
EdiDialogHandler - creates dialog
EdiBaseDialog - where the cancel button is

I thought I followed the steps pretty - but apparently not
0
 
LVL 47

Expert Comment

by:for_yan
ID: 37019753

This is a  total mess
You can;t possibly live with this stuff, for example with this:

  p.add(myModel.getButton());
      p.add(Box.createHorizontalGlue());
      p.add(myModel.getOKButton());

what is this? You are creating GUI interface - so how can you take buttons from some other class and add them
to panell in this class - this is not even possible to comprehend, what will be happneing with them and with their events, etc.

I think you should stop doing it - go to
very simple exmaple of how to create winodws, make interactions with the classes - understadn that
and then asatrt all your stuff from the very beginning

If you want, I can help you to get understanding - but we should do it with
the simple code to which we both have access - I posted you soem small application with
three woidnows - that would be one of the options.
 Once i see that you undnerstnd - then you'll be able to re-write your stuff in normal way.

Otherwise it goes nowhere. Just waste of the time.

So neither your flag, nor setNull() can possible work because you
are invoking these methods on a completely new instance which you just created.
and if you don;t undrstand  that, it means you still don't undesrtand
the very basic stuff.


And the issue is not following the steps - they will be always different in each situation - the issue
is to understand what you are doing.

You need to learn this. If I were you, I would follow my suggestion above.






0
 

Author Comment

by:jkteater
ID: 37019870
p.add(myModel.getButton());<-- My buttons are created in a seperate class and if they are enabled or not is decided in that class.  Should I be doing

JButton ok = myModel.getButton();
p.add(ok);

I would love nothing more to be able to start over and recreate the code correctly, but the project I was given is way behind schedule and I am starting to catch much grief.  I can work on getting the code correct on the side, but I still have to hit deadlines with this code.  Once I get the above working, then this code is about 90% done.

Here is what I thought about instances

public A {
  public A {

  }
 
  public testA() {
  print "Hello";
  }

}


public B {
A t;

  public B {
  t = new a();
  }

print(t.testA());

}

I thought that was the way it worked?
0
 

Author Comment

by:jkteater
ID: 37019884
I am guessing that the new is creating a new instance of A and that is why it is not working.

0
 
LVL 47

Expert Comment

by:for_yan
ID: 37019937
yes,  all GUI's should be created like that:

JButton ok = myModel.getButton();

p.add(ok);

and that should be mostly done in constructors
of the visual elemnts
You should never crete button in one class and pass it to anothe class -
maybe you can iimagine the situation when it would be justified
but the should be some mothsyears from now for you.

What you wrote about instances above does not sppeak at all about
interaction between classes and passingh handlers from one class to another of which we spoke a lot
and which is most importnat in your case (and almost in all java programs which has more than one class).

I still think it makes sense that you stop doing that ate least for couple odf days - sit down get a book or a tutorial -
and have 100% understandning what is instance of the class, what is instance variable, what is local variable,
what is static variable. How instance variabnles belong to instances of theier class.
Take a couple of simple code examples - understad how this interaction happens fully - so that
you'll not create a new instance invoke method in this new insatnce and then hope that it
would oaffect your original instance in which you creted theis dialog before
These kind of thing I believ eyou can undersatdn within a couple of days.

Then you should have minum understanding how to cretae visual elements
attach events to buttons/textfileds, etc
and how people normally split the creation of elements from
handiling the vents - look at some normals codes with this viwe.

When you get this understand maybe you will be able to deconvolute the most atrocious
coding which you see here and at least would stop wasting your time
asking questions like this one.

Otherwise - with some great luck you can suddenly make it working - but
then there will be next task, and next - and it will be pain all over if you do not internalize the very basics



 



0
 
LVL 47

Expert Comment

by:for_yan
ID: 37020024

>You should never crete button in one class and pass it to anothe class -
>maybe you can iimagine the situation when it would be justified

You can sometime pass a reference to button from one class to another
say in order to change the color of background of that original
button in the class that button was created from another class which nows when to change it,
even that is usually not vvery good practice
you should keep all these methods which change element appearances within the same class
where elements live and pass referebnce to this
class to your iothe classes so that you can acess these methods changing appearnces from events in
 other classes therough one handle of the class which has all these elements.

But what I meant above - is to grab "physical" button form another class
and  add it to panel in some othee  class - that is something really beyond normal
practice - and should never be done.

Actually you should stick to all elements creations to be done in the constructters of the class
and  methods and event hanldlers can modify their properties, but sually should not
create them.

0
 

Author Comment

by:jkteater
ID: 37020051
Once I get this one working - I will take some time and create a simple project and and try to understand and learn how they relate and pass instances.  I just have to get this working first.  I will go ahead and change the buttons though
0
 
LVL 47

Expert Comment

by:for_yan
ID: 37020084
change the bttons
and make sure that you pass to that dialog which you create the refernce to the instance of the class wher it is
being created, say have in thad dilog
instabce varaible poointing to the instance variable of the type of the creator class
and pass it through the constructore.
then use this handle to setNull or to set flag uin the parent instnce
not in the one newly created.
write if you understodd that.

0
 

Author Comment

by:jkteater
ID: 37020253
Here is the button location code

public EdiBaseDialog(Frame parent, TCSession theSession){
	   super(parent, false);
	   session = theSession;
	   myModel = SingletonSelectTable.getInstance();
	   myModel.setEdb(this);
	   submitButton = myModel.getOKButton();
	   removeButton = myModel.getButton();
	    createDialog();  
   } //end Constructo

Open in new window


Then I am doing p.add(removeButton);

I am going to walk through how I think this works - please bare with me


1.  
in EdiDialogHandler class upon execution I am calling this method

private void showediDialog(TCSession theSession) {
	   System.out.println("show : " + ar + "\n");
	   if(ar == null) ar = new EdiBaseDialog(null, theSession); 
	   //EdiBaseDialog ar = new EdiBaseDialog(null, theSession);
		 ar.setLocationRelativeTo(null); 
		 ar.setModal(false);
		 ar.setVisible(true);
   }    

Open in new window


because this is the first run ar is null, so ar is given a value by calling this assignemnt ar = new EdiBaseDialog(null, theSession);  - so now ar is the instance of the dialog and the EdiDialogHandler class.

2. I want to get the instance of ar to my EdiBaseClass

EdiDialogHandler edh;  <-- this is creating a variable edh of type EdiDialogHandler, but I still have not assigned any value to it.

3.

in the constructor I want to assign edh the value of ar.  I have tried many ways to do this.  
But I always go back to

edh = new EdiDialogHandler();

but apparently new is creating a new instance ar and we are not able to change the value I am looking to change.


4.  

In the cancel action I am calling a method in the creation class to change the ar instance value

public void actionPerformed(ActionEvent e) {
      	 
        	edh.setNull();
        	dispose();
         }

Open in new window



so I am thinking that step 3 is main problem?
0
 
LVL 47

Accepted Solution

by:
for_yan earned 200 total points
ID: 37020264

public class EdiDialogHandler {
 
EdiBaseDialog ebd; // instance variable pointing to the furttor dialog - initially when EdiDialogHandler class is loaded this is null

public EdiDialogHandler() {


}
// don't now either in method or in constructor at some point you create the dialog, say in method
public void  method() {
if(ebd == null) ebd = new EdiBaseDialog(this, ...,...);  // ebd is instance variable this instancwe of EdiDialogHandler will now hacve access form any method in this class
                                                            // to the dialog we created; we pass "this" in the constructor so that newly created EdiBaseDialog insatnce should
                                                          // maintain connection to the instance (particular instance, nott just class in ge,eral, but this very instance of this class!)
                                                           // of class-creator
                                          // if ebd was created already before by this class and was not null'ed it will not create dialog,
                                        //beacuse we want to keep only one dialog

}

public void nullEbd(){
ebd = null;  // we prepared method to be used from ourt child dialog when we
                  //want to close it to signal that chiled no longerexist - you can crete a new one if requested

}

}


Ok, now chile dialog class:

public class EdiBaseDialog{

EdiDialogHandler edh; //again - we prepared instance variable for pointing at creator

public EdiBase Dialog (EdiDialogHandler edh, ....) {
this.edh = edh; // here we assign that real creatoer who created us and sent us his handler (rememeber "this" parameter that was used in the constructor -
                        // this is the handle ; from now on child knows his parent everywhere in this class as edh, and parent know chald as you rememeber
                         // in itsown class as ebd - so this is connection between the instances - these particular instances which are now livingin your application
                      // not any templates, and not newly created instances, but peerticular live two instances which know each others addreses - thsi
                         // is the most important thing!

.//...OK, we do soem constrction - make all buttons etc



}


// now here we have evnts and event handlers and we have method to handle cancel button

//usually like that:

public void actionPerfoermed(ActionEvwent ae) {
if(ae.getSource().equals(Cancel)){
edh.nullEbd(); // and this points to exactly instance which created me, and in this way I disconnect
                       // from parent and from this pioint parent has no child and is allowed to create a new child
this.dispose(); // and this should just close this child dialog window


}



}





 

0
 
LVL 47

Expert Comment

by:for_yan
ID: 37020289
OK, right now fiorst of all read my previous post and write me if you undersatdn
100% everyrthing there and if you can now answer to yourself the question
why your flag was not changing before - we don't have flag here but the idology is the
same. . If you cannot understand it then  point to particular place or point which you don not
 understand.

this only addresses the interactions between these two classes

Why you get the new dialog populated with the old stuff - this is a separate issue, as i mentioned before.
0
 
LVL 47

Expert Comment

by:for_yan
ID: 37020315
>edh.nullEbd(); // and this points to exactly instance which created me, and in this way I disconnect
 >                      // from parent and from this pioint parent has no child and is allowed to create a new child

here I'm not invoking this method on some newly created dialog, I'm pointing to excatly that
instance that creted this very child; it is absolutwely critical that I'm using this very pointer that
was passed to me form parent in constructior as this; it points to particualar instance of my parent
not to some abstrct new instance of teh same type which I just created
0
 
LVL 47

Expert Comment

by:for_yan
ID: 37020338
OK, read attentively  my last three posts - acknowledge that you understand - then revise your prvious post - it is still not undrstandbale
0
 

Author Comment

by:jkteater
ID: 37020457
The reason the flag was not working was because I was not getting the instance of the dialog when creating it - I was creating a new separate instance and two instance did not talk to each other.  That makes sense now.  When my dialog was being created, we now passed the instance value as it was being created.  Then we all we have to do was assigned the passed instance to a value in the constructor.

knowing this now, I can see how bad this question was.  It was a very simple thing and I made it so big.  

Do you still have that link to the sample project that you told me about a few questions ago.  I do need to work on this and I did say I would take a couple of days to go over it.  If not I will try and look through all the post and find it.

Thanks
0
 
LVL 47

Expert Comment

by:for_yan
ID: 37020537


It is in this post:
http://#36995063
0
 
LVL 47

Expert Comment

by:for_yan
ID: 37020548
No it does not work across questions - sorry it in post 36995063 but in question:

http://www.experts-exchange.com/Programming/Languages/Java/Q_27405003.html
0
 
LVL 47

Expert Comment

by:for_yan
ID: 37020555
I reposted it here:

I would suggest that in a spare moment you look at this ismple example of how
classes intreact with each other - you can compile and see
how parent window intercats with childeren and chikldern with each other and look
at the ccode - in this cvase they represent framses - but this intercation throough
the handles between the classes in java application can work the same way between any classes,
not necessarily corresponding to visual elements
and it shows how in another class you can reach the same instance of some class
 which you creted in the third  class.

These things need to be understood clearly, then you'll be able to update
the table you created somewhere form another location in your application.
If you don't uinderstand this stuff, ask questions, it is really important.

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class MyMainWindow extends JFrame implements ActionListener {

      JTextField txt;
      JButton button;
      MyChildWindow child1;
      MyChildWindow child2;


    public MyMainWindow(){
        super("main");
     txt = new JTextField(10);
      button = new JButton("press");
        button.addActionListener(this);
            Container c = this.getContentPane();
        c.setLayout(new FlowLayout());
        c.add(txt);
        c.add(button);
          this.setSize(200,200);
        this.setLocation(100, 100);

      child1 = new MyChildWindow(this, 1);


           child2 = new MyChildWindow(this,2);


        child1.setVisible(true);
        child2.setVisible(true);
        this.setVisible(true);



    }
       public void setText(String s){
        txt.setText(s);
    }


    public void actionPerformed(ActionEvent e) {
            child1.setText(txt.getText());
          child2.setText(txt.getText());


    }

    public MyChildWindow getChild(int i){
        if(i==1)return child1;
        else return child2;
    }


    public static void main(String[] args) {
        new MyMainWindow();
    }


}

class MyChildWindow extends JFrame implements ActionListener {
    MyMainWindow parent;
       JTextField txt;
      JButton button;
    int num;

    public MyChildWindow(MyMainWindow parent, int num){
        super("child " + num );
        this.parent = parent;
        this.num = num;
           txt = new JTextField(10);
      button = new JButton("press");
           button.addActionListener(this);
        Container c = this.getContentPane();
          c.setLayout(new FlowLayout());
        c.add(txt);
        c.add(button);
        this.setSize(200,200);
        this.setLocation(num*200, num*200);

    }

    public void setText(String s){
        txt.setText(s);
    }

       public void actionPerformed(ActionEvent e) {
           parent.setText(txt.getText());
              MyChildWindow childNotMe = null;
           if(num == 1){
              childNotMe = parent.getChild(2);


           } else    childNotMe = parent.getChild(1);
           childNotMe.setText(txt.getText());



    }


}

Open in new window

0
 

Author Comment

by:jkteater
ID: 37020563
As always thanks, I will start on this code in the morning
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 37020987
>>
The reason the flag was not working was because I was not getting the instance of the dialog when creating it - I was creating a new separate instance and two instance did not talk to each other.  That makes sense now.  
>>

(which is what i was telling you in the comments i posted. The 'flag' variable was not what you thought it was)
0

Featured Post

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

Question has a verified solution.

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

An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
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…
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
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 Month15 days, 2 hours left to enroll

839 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