• 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

extends JFrame or new JFrame()

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

I' am studying the java language and have found several ways to create a frame, which way is the best and why?:

1. In Class method
//Create and set up the window.
JFrame frame = new JFrame("SwingApplication");

2. With the Class
public class AL extends Frame

Thank you

kind regards

Lars
 
Ranch Hand
Posts: 15304
6
Mac OS X IntelliJ IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Inheritance because you can is no reason to use inheritance. 99.999999% of the time, you gain nothing by extending JFrame. The other .000001% is for those that need something a bit more *special* than whatever JFrame currently provides. Quite rare.
 
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
but wont you save code? i atleast think its easier to just extend JFrame instead of JFrame pointer = new JFrame(); because you dont all the the time you use the methods write pointer.method(), you can just write method()?

or is this just being lazy?
 
Gregg Bolinger
Ranch Hand
Posts: 15304
6
Mac OS X IntelliJ IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

jerry washington wrote:or is this just being lazy?




 
Gregg Bolinger
Ranch Hand
Posts: 15304
6
Mac OS X IntelliJ IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You'll be surprised at how very little you'll need to do on the JFrame instance itself. It's really just creating it, setting a title (those are 1 line combined), setting an initial size, and then managing the listeners (close, resize, etc), which are handled the exact same way whether you use inheritance or not.
 
author and iconoclast
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It helps to point out that long, long ago, before the Earth cooled, and dinosaurs roamed the land, lo! There was no Swing! There was only AWT. And in fact, there was The Old AWT, before the Java 1.1 Event Model changed everything. Gather around, children, and heed these words.

In this mystical long-ago land, extending a component was the only way to respond to events! Verily, it was so. Thou wert required to extend Frame (or Panel, or Canvas) or the buttons could not beseech thee to action. And to make marks upon the fertile field of the grey Panel, lo! Again, inheritance was the one true way.

Perchance and anon, thou might comest across a scroll from these ancient days, or one from an elder author who remembers that time. And it will be not good, for it shall beseech thee to follow this long-eroded path, which leads only to madness and Bad Code. Come towards the light and be saved! Don't extend components unless you're actually changing the behavior of a component!
 
Bartender
Posts: 11497
19
Android Google Web Toolkit Mac Eclipse IDE Ubuntu Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Interesting discussion , and more interesting information by EFH here! Thanks for the insight on a little bit of history.

I usually extend JFrame when I want to maintain it as a "common reference point" for all child components. Lots of common code is also abstracted to it. This way, I end up with an extended JFrame instance, typically with a static getter method, and lots of pass through methods. Agreed this is not why one should be using inheritance, but it adds to my convenience. Of course this is a personal opinion and YMMV your mileage may vary.

 
Sheriff
Posts: 22781
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Maneesh Godbole wrote:YMMV


Please UseRealWords. I had to use Google to find out it means "your method may vary".
 
Maneesh Godbole
Bartender
Posts: 11497
19
Android Google Web Toolkit Mac Eclipse IDE Ubuntu Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Oops!
Actually I meant it as your mileage may vary. But I stand corrected and have edited my post.
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If you use the NetBeans IDE and the GUI builder that NetBeans has, and you use the wizard to create a new JFrame form, it will generate a class for you that extends JFrame (and probably the GUI builder even expects it to be that way).

Since your class that extends JFrame really represents the window you're creating, I don't see any problem in this case with using inheritance. If you'd use inheritance only because you need functionality from the superclass (and there is no clear "is a" relationship between the superclass and the subclass) then I'd object ( ) to it.
 
Lars Dold
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you all for your post's.

Now for me it's clear, when I need only the functionality of the Jframe I will use a method to use it. But now I have another confusion, how do I at the actionlistner?
Do I at it to the class where the object new JFrame() is created or can I add it directly to the object of new JFrame()?

Another Question I have: for what reason do I extend from JPanel?

Thank you very much

kind regards

Lars

 
Gregg Bolinger
Ranch Hand
Posts: 15304
6
Mac OS X IntelliJ IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Extending JFrame is, in most cases, not good design, regardless of what Netbeans does (which is probably a huge reason I don't use Netbeans).
 
Rob Spoor
Sheriff
Posts: 22781
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Although I too often extend JFrame for my user interfaces, it's indeed not good design. A sub class should add new functionality, and the contents of a user interface is not new functionality. That's the same as extending ArrayList to add some elements in the constructor. The controls can be removed after the JFrame has been created, breaking the entire user interface.

Why I still do it then? Habit I guess. Laziness as well probably. When I create a proper JComponent subclass I make sure I override the add / remove / layout methods to make sure my user interface stays intact. Something Sun hasn't done with most Swing components by the way - try adding a JButton to a JTable using the add method.
 
Marshal
Posts: 79151
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Jesper de Jong,
Your post was moved to a new topic.
 
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
I have split off the discussion about the NullPointerException which started today, but made a mistake, so it's now two threads; start here, and continue from the link I posted there.
 
reply
    Bookmark Topic Watch Topic
  • New Topic