File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Swing / AWT / SWT and the fly likes JPanel vs. JFrame - Problem with labels Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "JPanel vs. JFrame - Problem with labels" Watch "JPanel vs. JFrame - Problem with labels" New topic
Author

JPanel vs. JFrame - Problem with labels

Nicol Green
Ranch Hand

Joined: Jan 30, 2010
Posts: 66
I am not exactly sure what is the difference between JFrame and JPanel.

I have a class which extends JFrame (for me that means everything will be place in JFrame):



Could You, please, help me, and indicate where the problem is ?
Rob Camick
Ranch Hand

Joined: Jun 13, 2009
Posts: 2182
    
    7
Here I would like to set labels to the JFrame,


Well, your code is different. For your text fields you used setBounds(...), but you didn't do this for the labels.

Having said that, I would recommend that you do NOT use a null layout. You should never use the setBounds() method.

Instead, read the section from the Swing tutorial on Using Layout Managers. In the long run you are better to use one (or more) layout managers instead of hard coding the size and location.
Nicol Green
Ranch Hand

Joined: Jan 30, 2010
Posts: 66
Thanks for quick reply!

I will try with group or card layout.

Still, it would be nice if someone could tell me the difference between JFrame and JPanel, and when to use one, when another ?
Rob Camick
Ranch Hand

Joined: Jun 13, 2009
Posts: 2182
    
    7
I will try with group or card layout.


I would start with the simpler layouts, like FlowLayout, BorderLayout, GridLayout, BoxLayout and understand them first.

it would be nice if someone could tell me the difference between JFrame and JPanel,


You don't add components to frames, you add components to a Container (typically a Jpanel). Behind the scenes the frame.add(...) method adds the component to the content pane, which is actually a JPanel.

Again start by reading the Swing tutorial, thats why I provide the link with my first answer. Start with the "Using Top-Level Containers" section for more info.
Nicol Green
Ranch Hand

Joined: Jan 30, 2010
Posts: 66
Ok, thanks once more for a quick reply!

Now it is more understandable. - The story about JFrame and JPanel.

Just one more short question.

I will check all layouts out, but I need quick solution for my problem. Maybe You ran the code ? I would like to put two group box (where I will have labels and 2 textFields and one combo box). So what, upon Your opinion, would be the best solution concerning Layouts ?
pete stein
Bartender

Joined: Feb 23, 2007
Posts: 1561
We're more about teaching people to fish here than handing out fish. So in line with your "quick solution" based on your code I can't give any since it uses null layout. I join the chorus in recommending you read the Swing tutorials including and especially the layout manager section. There you will find your "quick solution". Luck.
Nicol Green
Ranch Hand

Joined: Jan 30, 2010
Posts: 66
Thanks!
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: JPanel vs. JFrame - Problem with labels