• 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

How to open new JFrame?  RSS feed

 
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello guys.
I have  problem.
In my desktop program i have a authorization window. And this window have a button.This button have action listener which will close authorization window and open a main window, but i can`t do this.
Look at my code, please. createWindow it`s method where i create the main window. And his object is static. When i running this program, and click on a button, authorization window was close, but main window don`t open.
 
Marshal
Posts: 61691
193
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why have you marked everything static? I tthink the keyword static always needs a good explanation, otherwise it is a mistake.
Why do you want two frames? Why not use a dialogue to take the authorisation, and clicking OK on the dialogue will close it. Read about dialogues in the Java™ Tutorials.
 
Bartender
Posts: 634
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell's post is the right approach.

But for the purpose of answering your question, the reason your code does not open a second frame is that your run() method calls logWindow() which creates and tries to display the log window, and then returns immediately before you have had a chance to interact with that window, with authorization still true. The if-test in run() then fails (because authorization is still true), and so nothing else happens.

The contents of the log window might not show up, since you set it visible before you add any content to it.
The call to setVisible(true) should be done AFTER you finish building the contents.  
 
Neizvestnost Odin
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Fred Kleinschmidt wrote:Campbell's post is the right approach.

But for the purpose of answering your question, the reason your code does not open a second frame is that your run() method calls logWindow() which creates and tries to display the log window, and then returns immediately before you have had a chance to interact with that window, with authorization still true. The if-test in run() then fails (because authorization is still true), and so nothing else happens.

The contents of the log window might not show up, since you set it visible before you add any content to it.
The call to setVisible(true) should be done AFTER you finish building the contents.  



Fred, please, can you look at my code and say what i`m doing incorrect?

This is my MAIN frame



This is a Authorization dialog window, and when i press "login" i want to open my main frame.




This is a thread where i running this two methods
 
Neizvestnost Odin
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:Why have you marked everything static? I tthink the keyword static always needs a good explanation, otherwise it is a mistake.
Why do you want two frames? Why not use a dialogue to take the authorisation, and clicking OK on the dialogue will close it. Read about dialogues in the Java™ Tutorials.


Campbell, check my code above, what i doing incorrect?
 
Fred Kleinschmidt
Bartender
Posts: 634
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Do not call window.pack() until after you have finished creating the entire GUI.
Calling pack causes the JFrame to ask its children what size they prefer to be so it can determine how lay everything out properly.
You are calling pack() immediately after creating the JFrame, before you have added any children to it. Since id has no children yet,  it will decide that its best size is zero by zero.

And if you call pack() just after adding the jPanel to it, the same thing will occur, since you have not yet added anything to the jPanel.

Also, you are calling window.setSize() after calling pack(). so the effects of the pack() statement will be ignored.
You should let the layout manager figure out the proper size rather than calling setSize on the frame.


You should call pack() just before calling setVisible(), and that should be the last statement in the method that creates the entire GUI.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!