How to remove layout from JPanel using NetBeans?

I use MainWindow (JFrame) with JPanel, using simple CardLayout, CardLayout is filled with some JPanels.

It works fine if I drag and drop JPanels from the palette into CardLayout and then place the contents in the panels. However, I want to place different JPanels in separate files, so I created some JPanel forms with NetBeans.

The only problem I have now is when I put my JPanel derived class in CardLayout (for example, using the Select Bean function from NetBeans). NetBeans always sets a new layout for the panel, so my initial layout from the JPanel class becomes overridden, and all I get is an empty JPanel.

So does anyone know if there is a way to just remove the layout from my JPanel classes? I mean, I cannot set the layout to No or something in NetBeans, if I set it to Null Layout, it still calls jPanel.setLayout (null); but I just don't want to call setLayout at all, is it possible?

And sorry if I'm just stupid to find a solution here. I am new to NetBeans, but there must be a way to manually change the code, I canโ€™t just use the "Customize code" option because it says "// Subcomponent code and layout - not shown here" ...

I hope someone understands that my problem is here. :)

+4
source share
4 answers

This is one of the main reasons not to use the GUI Builder. You should be able to get the code in Netbeans, however I suggest reloading the GUI manually. This will give you more flexibility and you can better understand what you are doing.

+1
source

Rather annoying, isn't it? You can code around it like this ...

After your derived JPanel class calls initComponent() in its constructor, it can disable further calls to setLayout() with, say, setAllowLayoutChange(false); .

Put the following code in a common base class derived 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); } 
+1
source

I had the same problem and had a lot of time. I could not see that JLabel was added to the extended class.

I think this is an incredible flaw in Netbeans designer due to the stupid generated and undeletable Panel1.setLayout (new ...

Solved it by adding to the previous answer. Basically the setLayout class lock 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); } 
0
source

Just set it to the default layout - this will prevent the code generator from executing. Note. Netbeans calls it Default / FlowLayout, but actually it just does nothing ...

0
source

Source: https://habr.com/ru/post/1333938/


All Articles