• Post Reply Bookmark Topic Watch Topic
  • New Topic

Creating JFrame  RSS feed

 
Ted Schrey
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am a beginner just starting to try to learn GUI. Looking at some tutorials online and have found right off the bat that there are two approaches to creating the basic JFrame and will go on from there. I want to chose the most common approach first, and ultimately learn both of course. The first way seems to create the Jframe Object in the typical way: JFrame frame = new JFrame("Title");, and the second is to have the class extend JFrame, then instantiating the JFrame Object by calling the class itself.

eg:



....and so on.. Can anyone advise which is the most common, recommend which path I should take first?
 
Ted Schrey
Ranch Hand
Posts: 57
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
after I posted and similar posts were shown, I went, and my question was answered...no need to repeat. (answer to go in direction that does not depend on inheritance --- don't use extends unless really need to). Thanks!
 
Darryl Burke
Bartender
Posts: 5167
11
Java Netbeans IDE Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I would add: before you have the chance to form any bad habits, learn about Concurrency in Swing, starting with invoking all Swing constructors and methods on the EDT, primarily via SwingUtilities#invokeLater(...).
 
Campbell Ritchie
Sheriff
Posts: 53779
128
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Also avoid subclassing Swing components as much as possible. It might be a good discipline to make a policy that the only subclassing permitted is to override the JPanel#JComponent#paintComponent() method and its overloadings.How to override paintComponent in an anonymous subclass of JPanelHow to override paintComponent in an ordinary subclass of JPanelAll other components are added as local variables in the myGUI constructor. All Listeners are added in the usual fashion or as λs; they do not count as components.

[edit]Spelling corrections and add that last line.[/edit]
[another edit]Somebody pointed out I was mistaken to say that paintComponent is overloaded. I must have looked in the wrong part of the API. Sorry[/edit]
 
Darryl Burke
Bartender
Posts: 5167
11
Java Netbeans IDE Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:It might be a good discipline to make a policy that the only subclassing permitted is to override the JPanel#JComponent#paintComponent() method and its overloadings.

I would add JTable, to override getColumnClass(...) / prepareRenderer(...) / prepareEditor(...). And sometimes when you want a whole lot of customized labels or buttons, it makes more sense to create a custom class than to set properties on every instance.

Of course, the latter can also be accomplished via a static utility method.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!