| 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: 19216
|
|
|
Why not use a CardLayout instead?
|
SCJP 1.4 - SCJP 6 - SCWCD 5
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: 19216
|
|
|
You do if you add / remove components after the parent has already been made visible.
|
 |
 |
|
|
subject: load Jpanels from seperate classes
|
|
|