This week's book giveaway is in the Other Languages forum.
We're giving away four copies of Functional Reactive Programming and have Stephen Blackheath and Anthony Jones on-line!
See this thread for details.
Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

load Jpanels from seperate classes

 
Kyle Fernandes
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 441
IntelliJ IDE Scala Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Doesn't the code above work? What happens when you try it?
 
Rob Spoor
Sheriff
Pie
Posts: 20667
65
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why not use a CardLayout instead?
 
Kyle Fernandes
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 441
IntelliJ IDE Scala Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 441
IntelliJ IDE Scala Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


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
Pie
Posts: 20667
65
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You do if you add / remove components after the parent has already been made visible.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic