views:

245

answers:

1

I've always been a little fuzzy on the difference between the glass pane and a layered pane. Is the glass pane essentially just "the very top layer of the root pane," or does it behave differently? When would you use a layered pane instead of the glass pane?

+4  A: 

They are two different things:

  • the layered pane is the destination of all the contents that are added or shown inside a JFrame. For example, every normal component like JLabels, JTextFields, JTable etc.. in addition it implictly handles z-ordering of elements that are added to it so it can handles popup menus, or drag and drop effects: this because a popup menu is added to the layered pane with a z higher than the normal components, with the final effect to stay on top of other things. See here to understand the behaviour better.

  • the glass pane is an optional layer that is hidden by default and stays in any case on top of the layer pane. So basically everything you draw onto the glass pane will be always visible: you can think of it as a trasparent sheet that is applied on the top of a normal JFrame that you can choose to use you need special effects.

Both the layered pane and the glass pane are placed on the root pane that is the basis from which every frame is built up.

Jack