Reputation: 15
Why does the toolbar not show up? I want to put it under the File/help menu...Im recreating the paint application and I want to put the buttons on the toolbar. The menu works fine, I believe that the problem is that the canvas where the user draws is covering it but im not sure. Please help.
Upvotes: 0
Views: 482
Reputation: 324108
contentPane = new JPanel();
setContentPane(contentPane);
CustomCanvas panel = new CustomCanvas();
panel.setBounds(0, 0, this.getWidth(), this.getHeight());
int xx, yy;
contentPane.add(panel);
contentPane.setLayout(null);
JToolBar toolBar = new JToolBar("This is the toolbar");
toolBar.setBounds(0, 0, 800, 50);
toolBar.setVisible(true);
The above code is a bit of a mess because you:
The end result is that the CustomCanvas
is painting over the toolbar.
Don't do any of the above.
Instead let the layout manager of the content pane to all the work. The default layout for a JFrame is a BorderLayout. So typically you would simply use:
//contentPane.add(toolBar);
add(toolBar, BorderLayout.PAGE_START);
Read the section from the Swing tutorial on How to Use ToolBars for a working example to show you a better program structure.
Upvotes: 3