Skip to main content

JXLayer glasspane question

1 reply [Last post]
uvoigt
Offline
Joined: 2006-01-26
Points: 0

Hi,

I try to display a close-button over a panel only if the mouse has entered the panels bounds. For this I use the RolloverEffectUI posted some time ago in this forum. It sets the glasspane (containing the button) visible or not.

Now I got two questions:
1. The button is shown only if the view of the layer does not use FlowLayout. Why does FlowLayout not work?
2. I am very unsure to which component the layer component must be added. In my demo application I have added the layer to its view. Is that correct?

Thanks for any information regarding this problem!
Ulrich

<br />
import java.awt.*;<br />
import java.awt.event.MouseEvent;<br />
import java.beans.PropertyChangeEvent;</p>
<p>import javax.swing.*;</p>
<p>import org.jdesktop.jxlayer.JXLayer;<br />
import org.jdesktop.jxlayer.plaf.AbstractLayerUI;</p>
<p>public class JXLayerFrame<br />
    extends JFrame<br />
{<br />
    class RolloverEffectUI<br />
        extends AbstractLayerUI<br />
    {</p>
<p>        private boolean isRollover;</p>
<p>        public boolean isRollover()<br />
        {<br />
            return this.isRollover;<br />
        }</p>
<p>        public void setRollover(boolean rollover)<br />
        {<br />
            boolean oldRollover = isRollover();<br />
            this.isRollover = rollover;<br />
            firePropertyChange("rollover", oldRollover, this.isRollover);<br />
        }</p>
<p>        @Override<br />
        public void handlePropertyChangeEvent(PropertyChangeEvent evt,<br />
            JXLayer<? extends JComponent> l)<br />
        {<br />
            super.handlePropertyChangeEvent(evt, l);<br />
            if (evt.getPropertyName().equals("rollover"))<br />
            {<br />
                boolean flag = evt.getNewValue() == Boolean.TRUE;<br />
                l.getGlassPane().setVisible(flag);<br />
            }<br />
        }</p>
<p>        @Override<br />
        protected void processMouseEvent(MouseEvent e, JXLayer<? extends JComponent> l)<br />
        {<br />
            if (e.getID() == MouseEvent.MOUSE_ENTERED)<br />
            {<br />
                setRollover(true);<br />
            }<br />
            else if (e.getID() == MouseEvent.MOUSE_EXITED && l.getMousePosition() == null)<br />
            {<br />
                setRollover(false);<br />
            }<br />
        }<br />
    }</p>
<p>    public JXLayerFrame()<br />
        throws HeadlessException<br />
    {<br />
        super();</p>
<p>        setLayout(new BorderLayout());</p>
<p>        add(new JLabel("Each cell should display an x-button on mouseOver"), BorderLayout.NORTH);</p>
<p>        JPanel mainPanel = new JPanel();<br />
        mainPanel.setLayout(new GridLayout(3, 3));</p>
<p>        for (int row = 0; row < 3; row++)<br />
        {<br />
            for (int col = 0; col < 3; col++)<br />
            {<br />
                JPanel cellPanel = new JPanel();</p>
<p>                // =====================================================================<br />
                /*<br />
                 * the button is not shown if the cell panel has FlowLayout. WHY?<br />
                 */<br />
                switch (row)<br />
                {<br />
                case 0:<br />
                    cellPanel.setLayout(new BorderLayout());<br />
                    break;<br />
                case 1:<br />
                    cellPanel.setLayout(new GridLayout(2, 2));<br />
                    break;<br />
                case 2:<br />
                    cellPanel.setLayout(new FlowLayout());<br />
                    break;<br />
                }<br />
                // =====================================================================</p>
<p>                cellPanel.setPreferredSize(new Dimension(100, 100));<br />
                cellPanel.setBorder(BorderFactory.createLineBorder(Color.red));</p>
<p>                JButton closeButton = new JButton("x");</p>
<p>                final JXLayer layer = new JXLayer(cellPanel);<br />
                layer.setUI(new RolloverEffectUI());<br />
                layer.getGlassPane().add(closeButton);<br />
                layer.getGlassPane().setVisible(false);</p>
<p>                mainPanel.add(cellPanel);</p>
<p>                // ================================================================<br />
                // is there a rule to which component the layer has to be added to?<br />
                cellPanel.add(layer);<br />
                // ================================================================<br />
            }<br />
        }<br />
        add(mainPanel, BorderLayout.CENTER);</p>
<p>    }</p>
<p>    public static void main(String[] args)<br />
    {<br />
        SwingUtilities.invokeLater(new Runnable()<br />
        {<br />
            public void run()<br />
            {<br />
                JFrame f = new JXLayerFrame();</p>
<p>                f.setVisible(true);<br />
                f.pack();<br />
            }<br />
        });<br />
    }<br />
}<br />

Reply viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
uvoigt
Offline
Joined: 2006-01-26
Points: 0

I have found out myself:

My fault was to add the component AND the layer. This is wrong. Only the layer must be added and then everything works as expected.