• Post Reply Bookmark Topic Watch Topic
  • New Topic

Passing Data From Parent Form to Subform  RSS feed

 
Svetlana Rosemond
Greenhorn
Posts: 17
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks to my previous post (http://www.coderanch.com/t/666115/GUI/java/Creating-Java-Forms-Subforms), I was able to switch my sub-form from a JFrame to JDialog. My problem now, if I need to pass data from my parent form to my sub-form. I think I figured it out, but I just want to make sure I'm going in the right direction.



In the above code I created an instance of my sub-form and then, pass the parent with the this reference and true to the modal variable, and also 10 as my data that I want to pass to my sub-form.



I modified the constructor of the sub-form. to accept the int and then set my JTextField with the value. When I modified the sub-form code the following also had to modified with -1 as the default.



It works, however, as I've learned over time, just because it works doesn't mean it's the proper/correct way of doing it. Is the above correct for passing data to a sub-form?

Secondly, How would I in my sub-form properly close it. Right now I use:



in my sub-form. When the user clicks okay/accept, how would I properly close it? Do I need to call dispose on my sub-form in my parent form after the sub-form is closed?
 
Fred Kleinschmidt
Bartender
Posts: 555
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why are you creating the subform in two different place: one called "test" in the JButton1ActionPerformed method, and another called "dialog" in the run method of a runnable invoked from somewhere you haven't shown us?

When you create a dialog and add a window listener with a windowClosing override, that method should call the same method that the dialog's "Cancel" button calls. The callback for the "OK" button should take whatever action is required, then also possibly call the cancel method. The cancel method would either just set the dialog's visibility to false, or dispose it, or do whatever you want when the user dismisses the dialog. If you just set it's visibility to false, you should make the dialog reference an instance variable, and only create a new dialog the first time you want to make it visible.
 
Svetlana Rosemond
Greenhorn
Posts: 17
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Fred Kleinschmidt wrote:Why are you creating the subform in two different place: one called "test" in the JButton1ActionPerformed method, and another called "dialog" in the run method of a runnable invoked from somewhere you haven't shown us?

When you create a dialog and add a window listener with a windowClosing override, that method should call the same method that the dialog's "Cancel" button calls. The callback for the "OK" button should take whatever action is required, then also possibly call the cancel method. The cancel method would either just set the dialog's visibility to false, or dispose it, or do whatever you want when the user dismisses the dialog. If you just set it's visibility to false, you should make the dialog reference an instance variable, and only create a new dialog the first time you want to make it visible.


The invokeLater code block was automatically generated in the sub-form main method, I didn't add that.
 
Fred Kleinschmidt
Bartender
Posts: 555
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Svetlana Rosemond wrote:The invokeLater code block was automatically generated in the sub-form main method, I didn't add that.

You did something to cause it to be generated. It is WRONG. It will cause your application to quit when the user dismisses the dialog.
 
Svetlana Rosemond
Greenhorn
Posts: 17
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Fred Kleinschmidt wrote:
Svetlana Rosemond wrote:The invokeLater code block was automatically generated in the sub-form main method, I didn't add that.

You did something to cause it to be generated. It is WRONG. It will cause your application to quit when the user dismisses the dialog.



Oh. I did experience that. I'll recreate it and see what happens. Back to my main question though, how do you pass the data from parent to sub-form?
 
Fred Kleinschmidt
Bartender
Posts: 555
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What data? You have already done some of that - you pass it via arguments of the constructor, or you create public methods in the subform that the parent form can call to set values in the subform.
 
Svetlana Rosemond
Greenhorn
Posts: 17
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Fred Kleinschmidt wrote:What data? You have already done some of that - you pass it via arguments of the constructor, or you create public methods in the subform that the parent form can call to set values in the subform.


I just wanted to make sure that what I was doing was correct. I thought it was wasn't cause I modified the constructor to take in a number? That''s fine right? I haven't violated any rules or best practices?

Side Note: I just created another sub-form and again, the invokeLater code was there automatically. I'm using NetBeans.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!