This week's book giveaway is in the HTML/CSS/JavaScript forum.
We're giving away four copies of Practical SVG and have Chris Coyier on-line!
See this thread for details.
Win a copy of Practical SVG this week in the HTML/CSS/JavaScript forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Different ways to create a window?

 
John Corkrend
Ranch Hand
Posts: 51
Chrome Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So I've recently been playing around with windows(JFrame) in Java, and have found that there are a few different ways to create a window, as there are a few different ways to do everything in java. What I'm wondering is how many ways are there? And which would you recommend I use when creating a program.

For example...


Provides the same output as something like this.


Basically what I'm wondering is what I should use, what would you recommend I stick with.
 
Rob Camick
Ranch Hand
Posts: 2703
10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I recommend that you create the frame on the EDT.

Take a look at the section from the Swing tutorial on How to Manke Frames. The tutorial has other examples to look at as well.
 
Ramesh Pramuditha Rathnayake
Ranch Hand
Posts: 178
2
Java MySQL Database Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You have to choose the best way to create and show a JFrame according to your program.
 
Maneesh Godbole
Bartender
Posts: 11445
18
Android Eclipse IDE Google Web Toolkit Java Mac Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ramesh Pramuditha Rathnayake wrote:You have to choose the best way to create and show a JFrame according to your program.

No he has not.
Like Rob said GUI operations should happen on the EDT. Also the call to setVisible should be the last one (after setting the title, icon size etc)
 
Ramesh Pramuditha Rathnayake
Ranch Hand
Posts: 178
2
Java MySQL Database Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Maneesh Godbole wrote:
No he has not.
Like Rob said GUI operations should happen on the EDT. Also the call to setVisible should be the last one (after setting the title, icon size etc)


Then, it is the best way..

Think of a JFrame where its icon is changed dynamically as user given. Then setVisible should be called before.. There is no rule of calling setVisible() method. Programmer has to decide the best place to call the setVisible().

Therefore the best way is depend from program to program...
 
Maneesh Godbole
Bartender
Posts: 11445
18
Android Eclipse IDE Google Web Toolkit Java Mac Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ramesh Pramuditha Rathnayake wrote: Then setVisible should be called before.. There is no rule of calling setVisible() method. Programmer has to decide the best place to call the setVisible().
Therefore the best way is depend from program to program...

Please search the forums to find out the adverse effects of not calling setVisible last
 
Ramesh Pramuditha Rathnayake
Ranch Hand
Posts: 178
2
Java MySQL Database Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes.. I know that..
I don't tell that GUI operations should not happen on the EDT. I said that, there is no rule..

As you said, adverse effect must have been taken. But programmer should have to avoid them anyway, if programmer choose to call setVesible() firstly..
 
Rob Camick
Ranch Hand
Posts: 2703
10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Of course there is no absolute guideline in programming. There are always hundreds of ways to do things. This question is about the best/normal way to do something.

Don't call setVisible first. Add the components then call setVisible(). This is a good safe guideline to follow.

Think of a JFrame where its icon is changed dynamically as user given. Then setVisible should be called before..


This is a terrible example. Of course if there is user interaction you need to be able to update the GUI after it is visible. The advice given here is for initially creating and displaying the GUI.
 
Ramesh Pramuditha Rathnayake
Ranch Hand
Posts: 178
2
Java MySQL Database Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rob Camick wrote:Of course if there is user interaction you need to be able to update the GUI after it is visible. The advice given here is for initially creating and displaying the GUI.


Yes.. I also agree that advice.. But I'm telling is "That's not a rule". Programmer have to decide the best way..
 
Rob Camick
Ranch Hand
Posts: 2703
10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There is never any good reason NOT to do it this way! There is no decision for the programmer to make, just follow the rule and it will work as expected. If you don't follow the rule it may or may not work. So basically yes it is a rule.

Just like following Java naming conventions is a rule. Of course the code will work if you don't. But if you want people to understand your code, answer your questions on the forums you follow the rules.

Forums have rules, like using code tags when you post code. Of course you don't have to follow the rules but then people may ignore you questions.

Rules/guidelines are there for a reason.
 
Darryl Burke
Bartender
Posts: 5155
11
Java Netbeans IDE Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ramesh Pramuditha Rathnayake wrote:I don't tell that GUI operations should not happen on the EDT. I said that, there is no rule..

There is a rule. Please go through Concurrency in Swing. Then search the net for Swing single threaded rule and go through a few of the search results.
 
Ramesh Pramuditha Rathnayake
Ranch Hand
Posts: 178
2
Java MySQL Database Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
As swing is not thread safe, single thread have to be used..! As Darryl Burke mentioned, I can also can accept that EDT is a rule. Early, I said that EDT should be used and that's not a rule..

As Rob says, rules and guidelines are for a reason. They are for guide us. Not to control us. We have the choice to use them or not. It's good if we can follow them always. But if there is no way, what should we do..?

In my previous sentence, I quote something from Rob. Ranch has supplied special quote block to use. But I didn't use that, because the way I wrote is the best way that fits here(As I think).

I said here to use codes in the best way that fits to the program. Not to the programmer..!
 
Paul Clapham
Sheriff
Posts: 21892
36
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ramesh Pramuditha Rathnayake wrote:I said here to use codes in the best way that fits to the program. Not to the programmer..!


Yup, I agree with that. So all of your previous posts where you said that the programmer should decide, that wasn't quite what you meant to say, right?

In this case, the best way to fit to the program is to initialize the GUI and then call its setVisible method. Sure, the programmer has to decide to do that and not to do something which is different and inferior, but the decision process isn't a difficult one. "Call setVisible at the end of initialization -- check".
 
Ramesh Pramuditha Rathnayake
Ranch Hand
Posts: 178
2
Java MySQL Database Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ramesh Pramuditha Rathnayake wrote:You have to choose the best way to create and show a JFrame according to your program.


This is first post. From this and other posts, I said that the best way that fits to the program should be recognize by the programmer..

Paul Clapham wrote:In this case, the best way to fit to the program is to initialize the GUI and then call its setVisible method. Sure, the programmer has to decide to do that and not to do something which is different and inferior, but the decision process isn't a difficult one. "Call setVisible at the end of initialization -- check".




In this program setSize() is called after the setVisible() method.. But I think no one can do the vice versa..
 
Paul Clapham
Sheriff
Posts: 21892
36
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ramesh Pramuditha Rathnayake wrote:In this program setSize() is called after the setVisible() method.. But I think no one can do the vice versa..


Sure it is. But it isn't part of initializing the GUI. However this has already been pointed out -- I don't know why you bring it up again.
 
Ramesh Pramuditha Rathnayake
Ranch Hand
Posts: 178
2
Java MySQL Database Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think I have gone on a wrong path. I forgot that you are talking about initialization...

But I wanted to say that, methods like setSize(), setTitle(), setIcon(), etc can be called after calling the setVisible() method...!
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!