Forum Stats

  • 3,728,688 Users
  • 2,245,675 Discussions


Why glass pane requires setLightWeightPopupEnabled(false)?

800351 Member Posts: 3,898
edited December 2007 in Swing
In the code below, the glass pane is a JPanel which everybody knows is a Swing lightweight component. Then why JComboBox requires setLightWeightPopupEnabled(false) for its proper functioning? What on earth does the method, in the first place? Why glass pane? What oddity in the hell does the glass pane have that other Swing component doesn't, never?
import javax.swing.*;
import java.awt.*;

public class GlassPaneOddity{

  public static void main(String[] args){

    JFrame frame = new JFrame();

    JPanel gp = new JPanel(new BorderLayout());

    JComboBox cb 
      = new JComboBox(new String[]{"alpha", "beta", "gamma", "delta"});
    cb.setLightWeightPopupEnabled(false); // why this line is so critical?
    gp.add(cb, BorderLayout.NORTH);

    frame.setSize(300, 300);


  • 800351
    800351 Member Posts: 3,898
    Why this question continues to be of 'No replies'?
    Is it because my English is so bad?
    If it is I will rewrite the question and repost it.
  • 794342
    794342 Member Posts: 6,422
    Why this question continues to be of 'No replies'?
    Is it because my English is so bad?
    I doubt that would ever be the case, your English is better than most.

    This forum has been having huge problems of late, posts not showing,
    then appearing some time later (often a significant delay). I never saw the
    original post - perhaps there was a delay, and when finally showing,
    showed up on page 2 (but who knows, with what's going on here lately).

    Anyway, the behaviour looks a bit strange. If you comment out
    and add
    you see the popup (underneath the glasspane) - works as normal, unless
    you add a mouseListener to the glasspane

    if you add a JPopupMenu to the glasspane, and do not include setLightWeight....
    the popup shows above the glasspane fine.

    So its not consistent - definitely strange to me. Hopefully someone else
    has a bit of insight into it.
  • 843806
    843806 Member Posts: 49,998

    For me a JPopupMenu is not shown, unless the popup boundaries go outside the boundaries of the JFrame. Which is normal because every popup (also for combo boxes) is shown as heavy weight popup (a new window) if it has to be shown outside of the boundaries of the top level ancestor (which is a lot heavier). The popup mechanism will automatically decide this. Unless you specify with the method setLightWeightPopupEnabled(false) that it should always show the popup in a separate window. That is why the method is so crucial. But I don't know which layer or panel the popups are painted in exactly when mechanism decides to simulate it as light weight popup.

    Hope it helps,
  • 800351
    800351 Member Posts: 3,898
    Thanks Michael and Marcel. Still a mystery, though. :)
  • 843806
    843806 Member Posts: 49,998
    What is the mystery?
  • camickr
    camickr Member Posts: 24,931
    edited December 2007
    What is the mystery?
    According to Michael's observation the combobox popup is painted "below" the glass pane but a regular popup is painted on top of the glass pane.

    Whether this behaviour makes sense or not it is documented in the Swing tutorial on "Using Root Panes". The first diagram shows the relationship between the layered pane and the glass pane. The layered pane section shows the default layers in a layered pane:
  • kirillcool
    kirillcool Member Posts: 641
  • 843806
    843806 Member Posts: 49,998
    This is a quite interesting subject:

    The JRootPane has basically 2 components (although its layout is managing a few more):
    - the glasspane
    - the layered pane

    The z-order of components inside containers (so also for JRootPane) is defined to be:
    - index 0 is highest
    - index 1 is lower but higher than 2 etc...

    The glasspane is added always on index 0 (meaning highest)
    The layered pane is added after (meaning lower)

    Inside the layered pane are added:
    - the content pane
    - the menubar

    And the layout of the rootpane (RootLayout) takes care of the positioning of the glasspane, layered pane, content pane and the menubar.

    The layered pane has a layer for content (containing contentpane and menubar) but also for popups. The popups are shown on top of the content pane, but always under the glasspane.

    As far as I can see this seems to be in line with the explanation under the rootpane tutorial. And in line with Michael's observation.

    Hope it helps,
This discussion has been closed.