• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Passing Data From Parent Form to Subform

 
Greenhorn
Posts: 17
2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks to my previous post (https://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?
 
Bartender
Posts: 732
10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 732
10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 732
10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
reply
    Bookmark Topic Watch Topic
  • New Topic