• 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

What is best Practice for Using JFrame class in GUI designing

 
Ranch Hand
Posts: 674
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi there

I have one question that suppose I am designing a Swing applications and I am using JFrame as Top level container.

So what is best
1)first Extends that class like


public class A Extends JFrame
{
//Do all stuff with Top Level container with gui
}




2)Or design like this from this source
http://docs.oracle.com/javase/tutorial/uiswing/examples/components/TopLevelDemoProject/src/components/TopLevelDemo.java





Thanks
 
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
My rule of thumb would be: Will the app have more than one window? If so, I'd create a subclass of JFrame, and keep the code separate from the class containing the "main" method. Otherwise I might set up the GUI in the main class.

As to whether the main class should extend JFrame, or construct it, I think that's personal preference to some degree. I think the latter is a somewhat cleaner approach.
 
Marshal
Posts: 79180
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Why should you have a class which extends JFrame? I prefer not to extend it myself. You can probably use most Swing components without subclassing them, but when you need to override paintComponent you will need a subclass (probably of JPanel). Example contrasting JFrame and its subclass in this thread.
 
"I know this defies the law of gravity... but I never studied law." -B. Bunny Defiant tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic