About ToolBar

I added two ToolBars in my program see below. It works fine until i move the toolbar. An exception was thrown:

Exception in thread"AWT-EventQueue-0" java.lang.IllegalArgumentException: cannot add to layout: constraints must be a GridBagConstraint

at java.awt.GridBagLayout.addLayoutComponent(Unknown Source)

at java.awt.Container.addImpl(Unknown Source)

at java.awt.Container.add(Unknown Source)

at javax.swing.plaf.basic.BasicToolBarUI.setFloating(Unknown Source)

at javax.swing.plaf.basic.BasicToolBarUI.floatAt(Unknown Source)

at javax.swing.plaf.basic.BasicToolBarUI$Handler.mouseReleased(Unknown Source)

at javax.swing.plaf.basic.BasicToolBarUI$DockingListener.mouseReleased(Unknown Source)

at java.awt.Component.processMouseEvent(Unknown Source)

at javax.swing.JComponent.processMouseEvent(Unknown Source)

at java.awt.Component.processEvent(Unknown Source)

at java.awt.Container.processEvent(Unknown Source)

at java.awt.Component.dispatchEventImpl(Unknown Source)

at java.awt.Container.dispatchEventImpl(Unknown Source)

at java.awt.Component.dispatchEvent(Unknown Source)

at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source)

at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source)

at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source)

at java.awt.Container.dispatchEventImpl(Unknown Source)

at java.awt.Window.dispatchEventImpl(Unknown Source)

at java.awt.Component.dispatchEvent(Unknown Source)

at java.awt.EventQueue.dispatchEvent(Unknown Source)

at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)

at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)

at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)

at java.awt.EventDispatchThread.pumpEvents(Unknown Source)

at java.awt.EventDispatchThread.pumpEvents(Unknown Source)

at java.awt.EventDispatchThread.run(Unknown Source)

import java.awt.BorderLayout;

import java.awt.GridBagLayout;

import javax.swing.JButton;

import javax.swing.JFrame;

import javax.swing.JPanel;

import javax.swing.JToolBar;

publicclass Testextends JFrame{

public Test(){

JToolBar toolBar1=new JToolBar();

toolBar1.add(new JButton("button1"));

JToolBar toolBar2 =new JToolBar();

toolBar2.add(new JButton("button2"));

JPanel panel =new JPanel();

panel.setLayout(new GridBagLayout());

panel.add(toolBar1);

panel.add(toolBar2);

this.setLayout(new BorderLayout());

this.add(panel,BorderLayout.NORTH);

}

/**

* @param args

*/

publicstaticvoid main(String[] args){

// TODO Auto-generated method stub

Test test =new Test();

test.setSize(200,200);

test.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

test.setVisible(true);

}

}

[4024 byte] By [youhaodiyia] at [2007-11-27 10:34:35]
# 1

> this.setLayout(new BorderLayout());

The default layout for a JFrame is a BorderLayout, so this line is unnecessary.

> panel.setLayout(new GridBagLayout());

The default layout for a JPanel is a FlowLayout. When I comment out the above line your code works fine for me.

When you close the toolbar is gets added back to the panel and the GridBagLayout expects you to specify the GridBagConstraints. The FlowLayout doesn't need any constraints so the toolbar can just be added back to the panel.

camickra at 2007-7-28 18:29:57 > top of Java-index,Desktop,Core GUI APIs...