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);
}
}

