2

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.

Hovercraft Full Of Eels
  • 283,665
  • 25
  • 256
  • 373
Arvanem
  • 1,043
  • 12
  • 22

3 Answers3

4

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.

Hovercraft Full Of Eels
  • 283,665
  • 25
  • 256
  • 373
  • Hi Hovercraft, thanks for your comment and I value and respect it. I guess I need to give further thought to how to implement a glass pane. My initial concern was that I would be unable to ascertain with a blank glass pane which component the mouse was over, but I suppose getDeepestComponentAt() is going to resolve that. +1 – Arvanem Mar 15 '12 at 19:38
4
  • 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)

Community
  • 1
  • 1
mKorbel
  • 109,525
  • 20
  • 134
  • 319
  • 1
    Hi mKorbel, thanks for your answer and its suggestions. I am still in the process of digesting the code you linked. :) +1 – Arvanem Mar 15 '12 at 19:39
4

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

trashgod
  • 203,806
  • 29
  • 246
  • 1,045
  • Thanks for your answer which seems likely to assist me to achieve my goal. I had seen redispatchMouseEvent() in the demo but until now had not realised its significance or usefulness. +1 – Arvanem Mar 15 '12 at 19:34
  • Am I correct to infer that this is a novel way to signal that a control is not applicable? Would `setEnabled()` or a dynamic layout do as well? – trashgod Mar 15 '12 at 19:44
  • Thanks for your comment and your inference is correct. Unfortunately setEnabled() grays out the relevant JComponents so that they are almost unreadable. The screenshot above doesn't do the unreadability justice. I will have to google dynamic layouts. – Arvanem Mar 15 '12 at 19:48