I'm using a MainWindow (JFrame) with a JPanel using a simple CardLayout, the CardLayout is filled with some JPanels.
It's working fine if I drag & drop JPanels from the palette to the CardLayout and then place the contents in the panels. However, I want to place the different JPanels in separated files so I've created some JPanel Forms with NetBeans.
The only problem I have now, when I put my derived JPanel class onto the CardLayout (e.g. using the "Choose Bean" function from NetBeans) NetBeans always sets a new layout for the panel, so my original layout from the JPanel class gets overridden and all I get is a blank JPanel.
So does anyone know if there is a way to simply remove the layout from my JPanel classes? I mean I cannot set the Layout to "None" or something with NetBeans, if I set it to "Null Layout" it still invokes "jPanel.setLayout(null);" off course, but I just want no call to setLayout at all, isn't that possible somehow?
And sorry if I'm just to stupid to find the solution here. I'm quite new to NetBeans, but there must be a way to manually change the code, I can't just use the "Customize Code" option b开发者_JS百科ecause it says "// Code of sub-components and layout - not shown here"...
I hope anyone understands what my problem is here. :)
This is one of the major reasons not to use a GUI Builder. You should be able to get to the code someone in Netbeans, however, I suggest to restart your GUI by hand. It will give you more flexibility and you will have a better understanding of what you are doing.
Rather annoying, isn't it? You can code around it like this...
After your derived JPanel
class calls initComponent()
in its constructor, it could disable any further calls to setLayout()
with, say, setAllowLayoutChange(false);
.
Put the following code in a common base class that derives from JPanel
:
protected boolean mAllowLayoutChange;
/** Creates new form CommonPanel */
public CommonPanel()
{
super();
mAllowLayoutChange=true;
}
public void setAllowLayoutChange(boolean b)
{
mAllowLayoutChange=b;
}
@Override
public void setLayout(LayoutManager mgr)
{
if(mAllowLayoutChange) super.setLayout(mgr);
}
I had the same issue and lost a lot of time with it. I could not see any JLabel added to the extended class.
I think it is an unbelievable flaw in Netbeans designer all due to a silly generated and undeletable Panel1.setLayout(new ...
Solved it with using an addaptation to a previous answer. Basically locking the class setLayout after initComponents.
protected boolean mAllowLayoutChange=true;
/** Creates new form CommonPanel */
public CommonPanel()
{
initComponents();
//no more layout changes allowed
mAllowLayoutChange=false;
}
@Override
public void setLayout(LayoutManager mgr)
{
if(mAllowLayoutChange) super.setLayout(mgr);
}
Just set it to the default layout - that prevents the code generator from doing anything. Note, Netbeans calls this Default/FlowLayout but in fact it just does nothing ...
精彩评论