This week's book giveaway is in the Web Services forum.
We're giving away four copies of Microservices in Action and have Morgan Bruce & Paulo A. Pereira on-line!
See this thread for details.
Win a copy of Microservices in Action this week in the Web Services forum!
  • 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Bear Bibeault
  • Devaka Cooray
  • Liutauras Vilda
  • Jeanne Boyarsky
Sheriffs:
  • Knute Snortum
  • Junilu Lacar
  • paul wheaton
Saloon Keepers:
  • Ganesh Patekar
  • Frits Walraven
  • Tim Moores
  • Ron McLeod
  • Carey Brown
Bartenders:
  • Stephan van Hulst
  • salvin francis
  • Tim Holloway

Using a button open frame from one class from another  RSS feed

 
Ranch Hand
Posts: 63
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello Group!

As the title says I am trying to open a frame using a JMenuItem from one class and opening a frame from another class. Now the below snippet of code works as far as disposing of the current JFrame. How ever if I try and open a JFrame from another class the frame will open up however it will be blank. Thanks in advanced as I've tried several examples with no good outcomes.

 
Master Rancher
Posts: 3010
105
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How does the contentpane of frame1 look? Have you overridden the paintComponent of that panel? Or of other panels that are part of this frame?
 
Marshal
Posts: 61820
193
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There is something wrong with having two frames; consider using a dialogue window instead of the second frame. More details in the Java™ Tutorials.
Also your class looks incorrect. What you are doing is creating an action which causes a second frame to appear. But you are not adding that listener to anything. What is more, you appear to have a statement outside any methods, so I don't think that code will compile in the first place. Also you appear to have non‑private fields if you are using the dot after pojava in line 6.
You should add the listener directly to the button (a menu item is a kind of button):-Now put all the logic in that method.
 
Piet Souris
Master Rancher
Posts: 3010
105
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
We only see a very small part of the code. Indeed, it is a little strange, for instance:an AbstractAction already implements ActionListener, and inside an anonymous ActionListener is created.
To keep the button or menu item in sync with the visibility of the frame that is being controlled this way, you need one or more WindowListeners (or a PropertyChangeListener). Unfortunately, my SSCCE takes at least 60 lines of code.

But OP states his code is running, only that the frame comes up blank.

By the way: what problems could there be when having two (or more) frames?
 
Campbell Ritchie
Marshal
Posts: 61820
193
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why would you want several heavyweight components? Other components can be implemented with dialogues, internal frames, popups, etc. Or even card layout.
 
Piet Souris
Master Rancher
Posts: 3010
105
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Okay, but are there any problems with it that I am not aware of? The only thing that I can think of is what to do when one of the frames is being closed, but a decent WindowListener will handle that.
 
Campbell Ritchie
Marshal
Posts: 61820
193
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Performance?
Yes, you would need a window listener; using EXIT_ON_CLOSE would be disastrous.
 
Scott Eric Catalano
Ranch Hand
Posts: 63
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the replies and explanations below:
1.) pojava - is another class. I am calling the frame from that class using a JMenuItem. When called it brings up a blank frame with no components. I have gathered from reading other posts and code that using: should work. It does it just brings up a blank frame as if it was new and not bringing the components along with it.
2.) I am using a button from yet another class to call the same frame and it works just fine.
With that being said I am confused. Thanks for the help.
 
Scott Eric Catalano
Ranch Hand
Posts: 63
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok I managed to solve my initial "blank frame problem" by using this piece of code: this code works in opening and populating the frame however now I need to dispose of the open frame. Thanks for the help!
 
Piet Souris
Master Rancher
Posts: 3010
105
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Okay, Scott,

if you need any help let us know.

By the way: you create your button using an Action as argument. That is fine, but you also add a dedicated ActionListener to your button. Sounds like being double work, although that should not cause frame1 to show up blank.
So, if you like: show us how you created frame1.
 
Scott Eric Catalano
Ranch Hand
Posts: 63
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Peit Souris:

1.) Frame1 no longer is blank solved by using this code:
2.) Now I have 2 frames open: Frame and Frame1. I would like Frame to go away when Frame1 is opened. I've tried several ways and none are working. Usually when I issue my JMenuItem commands I sually add an action listener and it works. However I am using this instead: So where do I put my actionListener command? Thanks!
 
Scott Eric Catalano
Ranch Hand
Posts: 63
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello All!

Problem has been solved with the following:


Things come together when you step back a little and re examine the code and taking advice from others. Thanks for making me think!
 
Campbell Ritchie
Marshal
Posts: 61820
193
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No it isn't necessarily solved. Why are you passing that Action to the constructor and then adding an action listener in line 3? Particularly if that Action adds action listeners: how many listeners will you have registered to that menu item if it is clicked repeatedly? You also have access problems; it should be impossible to access fields like that because those fields should have private access.
By the way: your bad indentation will make your code impossible for you to understand.
 
Scott Eric Catalano
Ranch Hand
Posts: 63
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
@Campbell Ritchie: The JMenuItem does two things: 1.) Opens a JFrame and 2.) closes the open JFrame. I could not figure how to close Frame1 from the main2Action section of code so I did it the way I did it with other frames in the program. There's no chance for the user to repeatedly click the MenuItem as the whole Frame is disposed of completely. I've tested it over and over...one frame opens, another closes over and over again and then clicking the master Exit Program button everything closes out. It works and throws no errors or exceptions and compiles just fine as far as I can tell.
 
Campbell Ritchie
Marshal
Posts: 61820
193
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Print out the action listeners associated with that menu item using the getActionListeners() method, and see what happens when the button is clicked repeatedly. Unless you mean that the button disappears because you are disposing the current frame. That sounds just as peculiar to me.
 
Piet Souris
Master Rancher
Posts: 3010
105
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
For a start: look at the code in your opening post:

You do not implement an ActionListener at all. In fact, code is executed (lines 2-8) in a place where that should not be allowed. See Campbells remark about this.

But normally an Action is of the form:

and perhaps a little later:

I wrote a small program with two frames and a button in one of them, and that button controlls the visibility of the second frame. It is not exactly what your program does, but it shows that the second frame is not blank, and how to keep the visibility and the button in sync. Using two frames and controlling when one of them is closed, or iconified et cetera is certainly not so easy.

My demo, in case you are interested, can be found on my GitHub account, GitHub , in the src map, ScottEricCatalano.java
 
Scott Eric Catalano
Ranch Hand
Posts: 63
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
@Piet Souris: Thanks for the demo code I downloaded and looked over it and will use it for reference :-)
 
Piet Souris
Master Rancher
Posts: 3010
105
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You're welcome! Since this all is a bit unusual, please let us know about your experiences, whether there are other problems that were unforeseen.
 
Don't get me started about those stupid light bulbs.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!