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 Problem in adding Jpanel to JFrame (Updated) Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Problem in adding Jpanel to JFrame (Updated)" Watch "Problem in adding Jpanel to JFrame (Updated)" New topic
Author

Problem in adding Jpanel to JFrame (Updated)

Swapnil Sonawane
Ranch Hand

Joined: Jan 02, 2008
Posts: 190
Here is my code:

ApplicationDriver.java



MainFrame.java




LoginPanel.java




But I do not get the JPanel inside the JFrame. I can only see the JFrame. I am confused a lot.


EDIT 1: LoginPanel.java and MainFrame.java updated and irrelavent code removed. I got the JPanel inside the JFrame. How can I position it to the center? What if I want to change the content pane? Would simply creating an instance of some other JPanel like LoginPanel and passing it to setContentPane do?

[ October 09, 2008: Message edited by: Swapnil Sonawane ]
[ October 09, 2008: Message edited by: Swapnil Sonawane ]

Swapnil S. Sonawane<br /> <br />B.Tech (Expected May 2009)<br />Computer Science, NIT Durgapur, India<br />SCJA 1.0
pete stein
Bartender

Joined: Feb 23, 2007
Posts: 1561
Originally posted by Swapnil Sonawane:
Here is my code:
and a lot of code it is. Please consider cutting this down to a reasonable size. You will have a greater chance that people will read it this way.

But I do not get the JPanel inside the JFrame. I can only see the JFrame. I am confused a lot.

What JPanel? When is it supposed to become visible? The more details you provide the easier it is for someone to know what you are trying to do, what is going wrong, what needs fixing.

One other thing: I see that you are coding Swing via NetBeans which I do not recommend at this stage in your Swing education as doing this will set this education back by at least 6 to 12 months. Sure use it after you know what you are doing, but not til then.
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19670
    
  18

You add the controls to the GroupLayout, but nowhere do you actually add anything to any of the JPanels themselves.

Also, contentPane.setLayout(null); in MainFrame is a bad idea, especially if you never set the bounds of its components manually. Now the LoginPanel is at location 0,0 with a size of 0x0.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19670
    
  18

Originally posted by pete stein:
and a lot of code it is. Please consider cutting this down to a reasonable size. You will have a greater chance that people will read it this way.

I agree, but only partly. In this case, only the menu creation could be removed. All the other code is actually necessary for finding out the problem. You can't test a user interface without building it, now can you?
Swapnil Sonawane
Ranch Hand

Joined: Jan 02, 2008
Posts: 190
I will clean up and post something that makes sense. I have to get the things clear.

1) Instead of having those labels and textfields in the loginpanel, I had created an instance of JPanel inside the LoginPanel. This happened because I made a mistake while using the GUI builder. I will rectify this now.
[ October 09, 2008: Message edited by: Swapnil Sonawane ]
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19670
    
  18

You won't have to (although it doesn't hurt ), but again: add the components!

You need to add the following:

The order doesn't really matter, and the "this." part can be omitted. You can also specify some constraints when adding, although I don't think that's necessary with GroupLayout. It certainly isn't for FlowLayout (JPanel's default).
Swapnil Sonawane
Ranch Hand

Joined: Jan 02, 2008
Posts: 190
I have rectified that part already. But I have some other doubt. Changing the location of LoginPane using setLocation or setBounds method doesn't change its location. Why does this happen?
pete stein
Bartender

Joined: Feb 23, 2007
Posts: 1561
You may want to post your new code. Having said this, I recommend that you read up on using the Layout managers in the Sun Swing tutorials. You can find it here: Lesson: Laying Out Components Within a Container

It will show you how to lay out your components and JPanels without using null layout, without using setBounds.
[ October 09, 2008: Message edited by: pete stein ]
Swapnil Sonawane
Ranch Hand

Joined: Jan 02, 2008
Posts: 190
I have edited my original code. But now I have switched to GridBagLayout (code not posted). After I include that in the JFrame, it is getting packed, but I don't want that and I haven't done the packing in the code.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Problem in adding Jpanel to JFrame (Updated)