Win a copy of Murach's Python Programming this week in the Jython/Python forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Simplest Window Possible  RSS feed

 
George Willis
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Is it possible to display an AWT Window only(just 'cause feel like doing it)? Or, is the simplest window possible to display a JFrame?

Thanks
 
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
To start with AWT and Swing are not the same. Swing is in fact a light weight version of AWT. It is recommended since quite a few years to use Swing instead of AWT.
Almost all AWT components have a direct one to one replacement.
e.g.
Window (AWT) -> JWindow(Swing)
Frame -> JFrame
Notice the 'J' prefix for the Swing versions.

Coming back to your question, Window and JFrame are different components (though both "first class citizens")
From the API docs for JWindow
A JWindow is a container that can be displayed anywhere on the user's desktop. It does not have the title bar, window-management buttons, or other trimmings associated with a JFrame, but it is still a "first-class citizen" of the user's desktop, and can exist anywhere on it.


So can you display (only) a JWindow (or a Window(AWT) for that matter)? Yes you can. However, 'simplest' is a relative term. If you identify simplicity by the number of lines of code required, I would say the simplest would be
 
Randall Twede
Ranch Hand
Posts: 4585
6
Java Python Scala
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
a JWindow is what you are probably looking for. read more about the whole hierarchy though. i used a JWindow for an app recently. imagine a JFame with no title bar.
 
George Willis
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
By 'simplest' I was referring to 'the most elemental' visible Java GUI thing. Unless you guys know how to make a Container visible all on its own I guess Window/JWindow is it.
Swing? Surely. But here I was trying to find out what that basic element was. It seemed reasonable to look further up the heirarchy and probably further back in Java history to find it.
I was able to get a working JWindow. JWindow has a convenient Constructor with no parameters that allows me to "just make a JWindow". And what is returned is a wonderful gray rectangle that is otherwise useless. Exactly what I was looking for (unless the same can be done with a Container).:

JFrame should be a piece of cake and is not what I was looking for.
I never made a Frame before though. Is there a way to turn the X in the upper right ON so that it closes the Frame? There doesn't appear to be any type of setDefaultCloseOperation available. The only examples I found made a Button with a Listener and then a System.exit().

Window is harder. As the javadoc says, "A window must have either a frame, dialog, or another window defined as its owner when it's constructed." Creating a Frame just to place a Window inside it does not seem elemental. I don't have the foggiest how one would create a Window to parent another Window. If there is a relatively simple way to do this please let me know. But, if one has to erect a skyscraper just to see the foundation I would not be interested.

Thanks
 
Campbell Ritchie
Sheriff
Posts: 53769
127
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
George Willis wrote: . . . Unless you guys know how to make a Container visible all on its own I guess Window/JWindow is it. . . .
You cannot make containers visible. Only a “top‑level” container can appear against the “background” of the desktop. There are to all intents and purposes 4 “top‑level” containers: windows frames dialogues and applets, but each comes in a Swing version and an AWT version. Read all about it in the Java Tutorials!
 
Campbell Ritchie
Sheriff
Posts: 53769
127
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
George Willis wrote: . . . Is there a way to turn the X in the upper right ON so that it closes the Frame? There doesn't appear to be any type of setDefaultCloseOperation available. . . .
Yes, there is a default closing operation method in JFrame. You should use the Swing version, JFrame, not AWT = Frame. In AWT you have to add a WindowListener to the frame in order to terminate the app.Pretty boring: comes up in silver grey and you have to use ctrl‑c to terminate it. I think you have read the wrong constructor about needing an owner. Read about all the constructors.
 
Campbell Ritchie
Sheriff
Posts: 53769
127
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
As I said: use the Swing versions of components throughout, not AWT
 
George Willis
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you for your knowledge and patience. I have what I need.
 
Campbell Ritchie
Sheriff
Posts: 53769
127
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You're welcome
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!