• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Tim Cooke
  • Devaka Cooray
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Rob Spoor
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Piet Souris
  • Mikalai Zaikin
Bartenders:
  • Carey Brown
  • Roland Mueller

Problem in adding Jpanel to JFrame (Updated)

 
Ranch Hand
Posts: 190
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
Bartender
Posts: 1561
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
Sheriff
Posts: 22806
132
Eclipse IDE Spring Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Rob Spoor
Sheriff
Posts: 22806
132
Eclipse IDE Spring Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 190
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 22806
132
Eclipse IDE Spring Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 190
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 1561
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 190
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
This is awkward. I've grown a second evil head. I'm going to need a machete and a tiny ad ...
We need your help - Coderanch server fundraiser
https://coderanch.com/wiki/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic