Arvanem
Arvanem

Reputation: 1053

For glasspane purposes, why are input elements in Swing seemingly not considered part of a JPanel?

By input elements I mean things like JSpinners and JComboxBoxes. My glasspane is passed a JPanel containing JSpinners, JComboBoxes and for the most part, JLabels. The glasspane has a MouseListener attached. The surprising thing is that mouseEntered is called upon the mouse cursor leaving the input elements and hovering over the other parts or empty space of the JPanel! Is this normal behaviour? How can I get the input elements to be considered part of the JPanel for Glasspane purposes?

Here is a screenshot of my UI with its input elements and jLabels. Sample UI

Here is an example piece of Code:

import javax.swing.*;

public class DialogTest {
    public DialogTest() {
        JPanel dialogPanel = new JPanel();
        SpinnerModel edgeModel = new SpinnerNumberModel(1, 1, 9, 1);
        JSpinner edgeSpn = new JSpinner(edgeModel);
        dialogPanel.add(edgeSpn);

        JDialog initialDialog = new JDialog(new JFrame(), "Test", true);
        initialDialog.setContentPane(dialogPanel);
        initialDialog.pack();
        glass = new GlassComponent(dialogPanel);
        initialDialog.setGlassPane(glass);
        glass.setOpaque(false);
        glass.setVisible(true);
    initialDialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
    initialDialog.setVisible(true);
    }
}

public class GlassComponent implements MouseListener {
   JPanel c;
   public GlassComponent(JPanel c) {
       this.c = c;
       this.c.addMouseListener(this);
   }

   ...
   public mouseEntered(MouseEvent e) {
       System.out.println("Entered JPanel");
   }    
}

By way of explanation, my goal is to eventually use the GlassPane to block input for those elements marked with the prohibition sign. However, given that the mouseListener assigned to the dialogPanel is seemingly generating new events upon leaving the input elements, I may have some difficulties achieving this.

Upvotes: 2

Views: 581

Answers (3)

trashgod
trashgod

Reputation: 205865

You can forward mouse events to the underlying components, as shown in The Glass Pane demo's method, redispatchMouseEvent().

Upvotes: 4

mKorbel
mKorbel

Reputation: 109823

  • you can use GlassPane for overlay required Container or JComponent by @camickr, or my questions based on his code here or here,

  • another suggestion could be use JLayer (required Java7 for Java6 is there JXLayer)

Upvotes: 4

Hovercraft Full Of Eels
Hovercraft Full Of Eels

Reputation: 285405

You appear to be using glasspane in a way that I feel it shouldn't be used.

As far as I know, a glasspane typically shouldn't be holding components at all but rather cover over the top-level window and then can act as a gate-keeper for the components that are below it, all held by the top level window's contentPane.

Upvotes: 4

Related Questions