This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Swing / AWT / SWT and the fly likes Ugh...so confused. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Ugh...so confused." Watch "Ugh...so confused." New topic
Author

Ugh...so confused.

Shannon Sims
Ranch Hand

Joined: Jul 03, 2003
Posts: 197
Hello.

Would someone please explain the difference between extending a JFrame or JPanel? Some of the sample code I've seen, shows extending of both. Is it preferred to extend one over the other? If so, why? It seems the answer to this isn't addressed in my Swing book...could be that scanning the book doesn't help either.

In my code, I'm doing both but with no luck extending the JPanel. I have a class that extends the JFrame and a listener that invokes the class that extends the JPanel. In a nutshell, I'm trying to simulate a pop-up window when a user clicks on my table. So, here's what it looks like:

windowOne extends JFrame
{
listener executes
{
news windowTwo extends JPanel
{
1. create new layout
2. set Layout
3. new JFrame
4. create & add labels to windowTwo
5. set windowTwo on step #3 ( new JFrame( windowTwo) )
6. set size
}
set windowTwo as visible
}
}

Once I click on the table, nothing happens. My pop-up window (windowTwo) does not display.

Thanks to all who review and/or reply to my post. All feedback and help is extremely appreciate!
Ken Blair
Ranch Hand

Joined: Jul 15, 2003
Posts: 1078
The difference is one is a JFrame and the other is a JPanel. One is an actual frame displayed on the screen and the other is simply a container. You need to post some code before we can help you further. If you're actually doing what you say you're doing it'd work, the fact that it's not indicates your code isn't actually doing what you think it is. Please present it.
Ken Blair
Ranch Hand

Joined: Jul 15, 2003
Posts: 1078
Wait a tick...



Is that actually your code? There is no JFrame constructor that accepts a JPanel. I'm not even sure how that compiles if you're literally passing a JPanel in.
Shannon Sims
Ranch Hand

Joined: Jul 03, 2003
Posts: 197
I sense a bit of sarcasm, which I didn't think one would get when seeking for help. Therefore, I will withdrawn my question...
Craig Wood
Ranch Hand

Joined: Jan 14, 2004
Posts: 1535
It is a sensible question. I think one reason that it is not addressed is that there are so many ways to put things together. Where/how people learn java seems to have a lot to do with their preferences along these lines. In the tutorial it is common practice to extend JFrame for the main class, ie, the class with the main method that gets everything started.

To be more specific I'll talk about what you shared. An easy way to create a popup is to use the JOptionPane class. It has a lot of static convenience methods that launch a dialog with minimal code. Check out the "Creating and Showing Simple Dialogs" section on the How to Make Dialogs page in the swing tutorial.

It is a little more work to make up your own component for this. JDialog is useful as a top–level container for custom components. You can use it like you do a JFrame and have the option to make it modal. To use a JPanel you need to load it into a top–level container as you were trying to do with a new JFrame. Generally we try to use only one JFrame per app and lighter top–level containers like dialogs for the rest.

edit: forgot the values for 'colNames'
[ October 06, 2005: Message edited by: Craig Wood ]
Stephen Boston
Ranch Hand

Joined: Jul 14, 2005
Posts: 165
Originally posted by Shannon Sims:
I sense a bit of sarcasm, which I didn't think one would get when seeking for help. Therefore, I will withdrawn my question...


I don't believe that is what Ken intended. I think he was just asking for some clearification.
Believe me, this is a great place to get help. I've used it many times and have not only gotten my questions answered but I learned more of JAVA in the process. As a matter of fact, JavaRanch is the only forum I post on due to the fact that my sometimes stupid questions get answered without flaming or sarcasm.


Steve<br /> <br />No matter where you go, there you are.<br /> <br />"My evil self is at the door, and I have no power to stop it."
Ken Blair
Ranch Hand

Joined: Jul 15, 2003
Posts: 1078
Originally posted by Shannon Sims:
I sense a bit of sarcasm, which I didn't think one would get when seeking for help. Therefore, I will withdrawn my question...


If you replace "sarcasm" with "confusion" then yes. You didn't really specify what your question was other than the difference between extending two different classes. The difference is they are two different classes, the implications of which are tenfold. I asked for code to help you becase the steps you say you're taking should result in it working, and it's not, therefore you're either not taking those steps (and only think you are) or your code is performing other steps that are breaking it that you didn't mention. Unless of course that constructor is actually what you're using, in which case I'm not even sure how it would compile. For that reason, I'm left to assume you put it in hastily and it's not literally what you're using.

If you want sarcasm, try the Sun forums, it's scary.

EDIT: And if you want a bit of help then don't let a bit of sarcasm get in your way, especially when it's merely misinterpreted. There's enough real assholes around that you don't need to manufacture more, for the sake of your sanity just assume people aren't as bad as you think they are.
[ October 06, 2005: Message edited by: Ken Blair ]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Ugh...so confused.
 
Similar Threads
need help with JPanel, liseners, and referencing vectors
avoiding flickering
Need some help please.
OverStack?
How to copy from 4 JTextFields to 1 JTextArea