Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

JDesktopPane - reference JInternalFrame  RSS feed

 
Bill Hayes
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have been slowly learning swing over the past year or so. I am at the point of creating a multi-frame application. I have a JDesktopPane with two JInternalFrames. I have set up buttons on both internal frames that will hide itself (setVisible(false)) and show the other. On the form that becomes visible I want to reference components on the other form (mainly get text from JTextFields). Based on what I've learned so far I'm thinking I need to set up some sort of listener. I'm not sure what kind of listener or where it goes. Am I on the right track? Can someone show me a snippet of starter code? Or possibly point me to a post that has already answered this question? I did not see one in my search.
Thanks, Bill
 
Craig Wood
Ranch Hand
Posts: 1535
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There are a lot of ways you could put something like this together.
Here's an exploration of one possibility.
 
Bill Hayes
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you for the response. I understand the example and was able to get it to run.
Trying to fit some of it into my code is proving to be a challenge for me. Perhaps I'm trying to take a short cut.
I have the JDesktopPane and JInternalFrames set up. I have a JButton on my second JInternalFrame where I'm placing my code to reference the first JInternalFrame.
Where I have frmReference = frames[i] in my code seems to be setting the reference to my other JInternalFrame (it at least returns the name i was expecting). But when I get the components for frmReference I only get 2, a JRootPanel and a MetalInternalFrameTitlePanel. I have 5 JButtons, 2 JTextFields and a JTable on the internal frame.


 
Craig Wood
Ranch Hand
Posts: 1535
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The rootPane will contain children. You have to traverse the component hierarchy to find your components. This suggests some form of recursion.
Try using something like the getTextField method shown above.
 
Ted Smyth
Ranch Hand
Posts: 73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
My first thought would be to keep references to a model backing each internal frame, with some sort of unique identifier. This way you could get the data from each frame without worrying about traversing component hierarchies.



Granted this requires setting up a bit of a relationship between the model and view to ensure they are in synch. The trigger to synchronize the model and view could be that button you are using to hide the internal frame, for example.
 
Bill Hayes
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well, I went back and created the separate methods as Craig Wood originally proposed. I was able to use getIFrame, getNextFrame and getTextField. I modified getTextField to getTable as I'm pulling the users selection from a JTable. This works great and returns the users selection.
The code for my button is clean and the separate methods keep things neat and organized.
Thank you.

 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!