4

I have user draggable JToolBars added to the NORTH, EAST and WEST regions of a JPanel with a BorderLayout. When the app exits I would like to save the region that the user has docked the JToolBars to, in order to respect this preference on next app launch.

I've examined the property change listener approach, and tried obtaining the instance of BorderLayout on the JPanel and inquiring as to the Constraints each added component has, but am unable to find the right info. I've trawled the source code too, and most of the user dragging appears to be delegated to the UI in effect and am struggling to keep context as I read through it all.

Any Swing aficionados know the answer here please?

Paul Samsotha
  • 205,037
  • 37
  • 486
  • 720
Hamish258
  • 305
  • 2
  • 9

1 Answers1

4

Can't you just use ContainerListener

public void componentAdded(ContainerEvent e);

for your container to get the docking event?

StanislavL
  • 56,971
  • 9
  • 68
  • 98
  • 1
    Seem to work. Tested with `System.out.println(((BorderLayout)e.getContainer().getLayout()).getConstraints(toolBar));` :-D – Paul Samsotha Sep 29 '14 at 09:34
  • Me too, thanks both: in the ContainerListener.componentAdded method, by using the BorderLayout.getConstraints(e.getChild()) method it returns the region, i.e. North, West etc. – Hamish258 Sep 29 '14 at 09:40
  • 1
    @Hamish258 http://java-sl.com/tip_multiple_floatable_toolbars.html this also could be useful. You can do the same on LayoutManager level – StanislavL Sep 29 '14 at 10:56
  • @StanislavL btw is that a typo in the `prefferedLayoutSize` method, perhaps means `preferredLayoutSize`? Thanks – Hamish258 Sep 30 '14 at 12:56