• 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

Why is a method needed in my example?

 
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello,

I'm new to Java. I'm currently looking at a few examples on Youtube to learn how to code in Java. On Youtube I saw the following code:



I have 2 question about this code:

1) Why do we need to create the method gui()? Can't we just program all the code in the Frame1() contructor like this?



2) Why do we need to use f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE)? The frame closes just fine without this command.

Greetings,

Denasio
 
Saloon Keeper
Posts: 10687
85
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows ChatGPT
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The gui layout code gets to be rather extensive in some projects, it's a good idea to put it into its own method in order to keep the code manageable.

The method setVisible() should be the last method called after the gui layout has been completed.
 
Marshal
Posts: 79151
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Carey Brown wrote:. . . it's a good idea to put it into its own method in order to keep the code manageable. . . .

Even so, some books do show all the GUI setup code in a constructor.

And, Nasio denasio. welcome to the Ranch
 
Saloon Keeper
Posts: 15484
363
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Welcome to CodeRanch!

As Carey mentioned, the GUI layout code can become quite bulky, and may distract from more important things that are happening in the code, such as the initialization of fields. Usually this method is called initializeComponents(), instead of gui(). Remember that method names must be verbs that describe what the methods do.

I believe that by default, closing a JFrame hides a window without actually disposing it. That means the event dispatch thread will keep running and the application doesn't terminate.

Fields should be private and final, if possible. Don't make f public. Also, give it a more descriptive name, such as frame.

Never call setVisible() or pack() from a constructor, either directly or indirectly. They start up the event dispatch thread, and you must never start threads from constructors. Instead, put them in a separate method that is called by the same method that calls the constructor.
 
Carey Brown
Saloon Keeper
Posts: 10687
85
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows ChatGPT
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Stephan van Hulst wrote:...you must never start threads from constructors.

Could you clarify this please.
 
Stephan van Hulst
Saloon Keeper
Posts: 15484
363
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Threads tend to leak references to the uninitialized object that's being constructed. On top of that, the thread may see the constructor code being executed out of order, unless you perform synchronization inside the constructor, which is well beyond the responsibility of constructors. Finally, when I create an object, I don't expect it to fire up something else that works in the background. It violates the principle of least astonishment.
 
Bartender
Posts: 732
10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This is a bad example. You should always create the GUI and display it from the EventDispatchThread, not on the main thread.
 
Carey Brown
Saloon Keeper
Posts: 10687
85
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows ChatGPT
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This is the template I've been using. This inherits from JFrame which some people don't like, but it has worked fine for me.
 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
By creating the method named gui, When you collect all the similar statements at a separate place then its look organized and take a short time to add some more features to that. You need this f.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE) because when you click on the close button that frame was not closed, only disappears but still exists in the memory.
 
Campbell Ritchie
Marshal
Posts: 79151
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Nasio denasio wrote:. . .. . .

Sorry for coming to this thread late. If you click the close button on your frame where the default closing operation is EXIT, you will close the GUI and the JVM will terminate immediately afterwards. There are three other options, which you can find in the JFrame documentation. They are DISPOSE, HIDE (the default), and DO_NOTHING. Now, what if you have some data you want to record in a file? You can do that via additional code called when you close the window. But not if you specify EXIT. You can add code to the frame to listen for its closing, and that code can write whatever it is to the file. So it is not always a good idea to use EXIT; sometimes one of the other three options is better. There are all sorts of other things you might want to do when the window closes, which would again not be possible if you use EXIT.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic