I am developing a Java Desktop Application. In the GUI, I want that user can add as many toolbars dynamically as he wants. To implement this, the following are the things that I have done already:
- Have taken a mainPanel and set its layout as BorderLayout
- Then taken a topPanel and added it to the mainPanel’s BorderLayout.NORTH
- set the topPanel’s layout as BoxLayout
- Then taken 5 panels named toolbar1Panel, toolbar2Panel, ….
- Afterthat, have added one toolbar to each of the toolbarPanel created in the previous steps.
- Have added only one toolbarPanel i.e toolbar1Panel on the topPanel
Now there is a button named “Add” on the first toolbar which is added on the “toolbar1Panel” which in turn is added to the topPanel.
Now I have implemented the “actionPerformed()” method of the above “Add” button as follows:
// to add second toolbar Panel to the topPanel dynamically
topPanel.add(toolbar2Panel);
But the problem is that it is not working. Means there is no toolbar added to the topPanel.
Is there anything which I am missing.
The code is Netbeans Generated so I think it would only add mess for others, that’s why I haven’t pasted any code here.
After adding another toolbar to the BoxLayout, you may need to (re|in)?validate the panel.
I’ve done this repeatedly but I can’t understand the logic behind the 3 or so method calls; so I just try them until I hit on the one that works:
(at least) one of those should force your GUI to re-calculate its layout, making the north panel larger and thus showing the 2nd (and successive) toolbar(s) you’ve added.