This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Swing / AWT / SWT and the fly likes load Jpanels from seperate classes Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "load Jpanels from seperate classes" Watch "load Jpanels from seperate classes" New topic
Author

load Jpanels from seperate classes

Kyle Fernandes
Greenhorn

Joined: Apr 20, 2011
Posts: 8
Hello, Im trying to create an interface where I cant change the components of the without have to change frames. For example the main interface would have a empty jPanel in which I would add or remove other jPanels to. The other jPanels say for example one for login would be called from another class and all its components would load on the jpanel on the main interface. after the login button is clicked the login jpanel should be removed from the main and a new one placed in.

Here is some example code..

Login.java




Main.java



any help would be much appreciated thank you..
Luigi Plinge
Ranch Hand

Joined: Jan 06, 2011
Posts: 441

Doesn't the code above work? What happens when you try it?
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19656
    
  18

Why not use a CardLayout instead?


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Kyle Fernandes
Greenhorn

Joined: Apr 20, 2011
Posts: 8
It doesnt seem to work..I just get a blank area where the panel should be but everything else in the frame seems fine..Im not sure if I should setVisible() somewhere..
Im not familiar with card layout..:/Im using netbeans..drag and drop...
Kyle Fernandes
Greenhorn

Joined: Apr 20, 2011
Posts: 8
I tried to change the add(component) part to jPanel1.add(crum, BorderLayout.CENTER);
now Im getting this new error, Im not sure if its related..

Exception "java.lang.ClassNotFoundException: org.openide.loaders.DataObject"while constructing DataFlavor for: application/x-java-openide-dataobjectdnd; mask=1; class=org.openide.loaders.DataObject

Luigi Plinge
Ranch Hand

Joined: Jan 06, 2011
Posts: 441

I don't know about the exception - sometimes class loading errors might be because you've spelt the class name wrong.

What layout manager are you using with your panel? JPanels have a FlowLayout by default, so trying to add something with BorderLayout parameters might cause problems. Also your code looks like it's from NetBeans, which uses something called a GroupLayout unless you tell it to use something else.
Luigi Plinge
Ranch Hand

Joined: Jan 06, 2011
Posts: 441



I did an experiment and it seems you need to both validate() and repaint() the container when you add a new panel to it.

Ignore all (generated) code up to line 53
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19656
    
  18

You do if you add / remove components after the parent has already been made visible.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: load Jpanels from seperate classes
 
Similar Threads
JDBC Java Application not working
simple login - help asp
Pressing a button to open a new window
eroor in the pravite class declaration
how can i display output text in a textArea of a jpanel