8 Replies Latest reply on Dec 13, 2007 6:34 PM by 843806

    Why glass pane requires setLightWeightPopupEnabled(false)?

    800351
      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();
          frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      
          JPanel gp = new JPanel(new BorderLayout());
          frame.setGlassPane(gp);
      
          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);
          frame.setVisible(true);
          gp.setVisible(true);
        }
      }
        • 1. Re: Why glass pane requires setLightWeightPopupEnabled(false)?
          800351
          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.
          • 2. Re: Why glass pane requires setLightWeightPopupEnabled(false)?
            794342
            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
            //cb.setLightWeightPopupEnabled(false);
            and add
            gp.setOpaque(false);
            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.
            • 3. Re: Why glass pane requires setLightWeightPopupEnabled(false)?
              843806
              Hello,

              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,
              Marcel
              • 4. Re: Why glass pane requires setLightWeightPopupEnabled(false)?
                800351
                Thanks Michael and Marcel. Still a mystery, though. :)
                • 6. Re: Why glass pane requires setLightWeightPopupEnabled(false)?
                  camickr
                  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:

                  http://java.sun.com/docs/books/tutorial/uiswing/components/rootpane.html
                  • 7. Re: Why glass pane requires setLightWeightPopupEnabled(false)?
                    kirillcool
                    http://www.pushing-pixels.org/?p=95
                    • 8. Re: Why glass pane requires setLightWeightPopupEnabled(false)?
                      843806
                      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,
                      Marcel