tags:

views:

136

answers:

4

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.

+2  A: 

Without specifying the layout for the top panel, it might be assuming the incorrect one.

Adding two toolbar panels to it might just be replacing the first with the second, or ignoring the second.

Just for testing set the topPanel's layout to FlowLayout and try again.

Allain Lalonde
Actually I had added the topPanel's layout as BoxLayout. I forgot to write in the above description. It is not running with that layout too.
Yatendra Goel
A: 

I think you are trying to do too much before testing. the way I would approach this is to start with something very simple, for example one panel, one static label. When that appears as you expect add a toolbar with a menu item. Does that work. Then incrmentally add the pieces.

Quite likely you'll have problems with a simple case and can then figure it out, or you'll have a simple case to post here.

Alterntively, as starting point pinck some working example from the net. Cut it down and then build up to your case.

djna
+3  A: 

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:

topPanel.validate();
topPanel.invalidate();
topPanel.revalidate();
topPanel.layout();

(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.

Carl Smotricz
Hurray! It's working.A very-very thanks to you. I have been trying this thing since morning. You have given answers in all 3 posts of mine. A lot of thanks for that.
Yatendra Goel
Excellent! Glad to hear I was able to help. Which of the validate family did you end up using?
Carl Smotricz
I used revalidate()
Yatendra Goel
A: 

Does it help ? Is it what you want to achieve ?

import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JToolBar;
import javax.swing.SwingUtilities;

public class AddingToolbars {
    public static void main(String[] args) {

        SwingUtilities.invokeLater(new Runnable(){

            public void run() {
                AddingToolbars me = new AddingToolbars();
                me.initGui();

            }

        });

    }

    private JPanel topPanel;
    private JPanel mainPanel;
    private JFrame frame;

    private void initGui() {
        frame = new JFrame();

        mainPanel = new JPanel(new BorderLayout());
        frame.setContentPane(mainPanel);

        topPanel = new JPanel();
        BoxLayout bLayout = new BoxLayout(topPanel,BoxLayout.Y_AXIS);
        topPanel.setLayout(bLayout);
        mainPanel.add(topPanel,BorderLayout.NORTH);

        JButton addButton = new JButton("Add toolbar");
        mainPanel.add(addButton,BorderLayout.CENTER);

        addButton.addActionListener(new ActionListener(){

            public void actionPerformed(ActionEvent e) {
                addNewToolBar();
            }

        });

        frame.setSize(500,500);
        frame.setLocationRelativeTo(null);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setVisible(true);

    }

    protected void addNewToolBar() {
        JToolBar tb = new JToolBar();
        tb.add(new JButton("b1"));
        tb.add(new JButton("b2"));
        tb.add(new JButton("b3"));

        topPanel.add(tb);
        mainPanel.validate();
    }
}
Laurent K