1

I want to create a JToolBar without adding it into any JFrame window. If I have to add it, then how can I make it such that the toolbar is created as a floating toolbar not a docking toolbar?

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
user385261
  • 4,069
  • 8
  • 26
  • 24

2 Answers2

2

you are going to need to override BasicToolBarUI and set the toolbars parent to an instance of JDialog which is bound to the current frame, that way you can float a toolbar by default and keep it on top of the frame.

krystan honour
  • 6,523
  • 3
  • 36
  • 63
0

You can add it to some panel and then make it float immediately by calling setFloating on its BasicToolBarUI, no need to override classes:

JPanel someParentPanel = ...; // BorderLayout?
JToolBar toolBar = ...; // Your toolBar

// It is mandatory for the JToolBar to have a parent component before calling ui.setFloating
someParentPanel.add(toolBar, BorderLayout.WEST);

// Now, make it float
BasicToolBarUI ui = (BasicToolBarUI) toolBar.getUI();
ui.setFloating(true, new Point(0, 0)); // Pass any point where you want it to appear

You can also make it dock by using the same method, but passing false as first parameter.

Aldo Canepa
  • 1,791
  • 2
  • 16
  • 16