Page 1 of 1

Swing Component Overview: Glass Panes Rate Topic: -----

#1 Dogstopper  Icon User is offline

  • The Ninjaducky
  • member icon



Reputation: 2876
  • View blog
  • Posts: 11,050
  • Joined: 15-July 08

Posted 07 July 2010 - 03:14 PM

Java Swing Overview– Glass Panes
Glass Panes sit at the very top of root panes, which are primary components of JFrames and other Top-Level components (see that tutorial in my contributions along with this one). Basically, what these components do is to make an invisible layer on top of everything else that allows very precise control of events and painting. Often, it is more difficult to use a glass panel than it is to not use one, so I suggest using them only when necessary.
Note that this tutorial explains some of Sun’s code in depth, so you will need to look over their code first: http://java.sun.com/...ssPaneDemo.java
My tutorial will focus on explaining 3 of the key methods there that make up a GlassPane. Kudos to Sun!

Event Handling
Events with glass panes are sometimes difficult to figure out because they take priority over any other component because they are on top of all. Because of this, if a component beneath the glass pane has an event that needs handling, then the glass pane must explicitly dispatch the event to that component. However, it is complex so I will go over some of the methods that they use to have the functionality that their demo has. I am assuming that you are familiar with events, so if you are not, then you should probably look at those in depth before proceeding.

The SwingUtitlities Class
This class is actually one of the more important classes when it comes to Swing as it contains a great number of methods that are very powerful. If you want to take a look at the methods, check out the API. The two that are used in Sun’s example are convertPoint() and getDeepestComponentAt().

convertPoint() takes 3 to 4 arguments based on which one you use.
Arguments:
1st argument is the source component which our point is based on.
2nd argument is the Point where the mouse is located (in the source component). In the four-argument version, this is 2 ints – x and y.
3rd argument is the destination component to which we are converting the point.

Let me give you an example. If we get the Point of a click in a bigger component and need to get that point relative to a component inside of it, then one can utilize this method. Obviously, if it is in the big component but outside of the smaller component, it will have a negative value.

getDeepestComponentAt() takes 3 arguments.
Arguments:
1st argument is the parent component (The one that is directly beneath the glass pane usually).
2nd argument is the x location at which to look for the component.
3rd argument is the y location at which to look for the component.

So, the program demo that Sun gives uses these methods to tell if a click occurred over a specific component and if so, then it sends the event to that component. However, if it is not, then the Glass Pane takes care of the event.

The dispatchEvent() method
This method takes a single argument – the Event to give to the component. In Sun’s example, the glass pane sits over the menu AND contentPane, so to send an event to the contentPane, you must sent a converted point with the coordinate system of the contentPane, not the GlassPane, as it will be referencing different places. So, you must construct a new event.

This is a good time to look into the API for referencing. It says that the constructor of a MouseEvent looks like so:

Quote

MouseEvent(Component source, int id, long when, int modifiers, int x, int y, int clickCount, boolean popupTrigger)
Constructs a MouseEvent object with the specified source component, type, modifiers, coordinates, and click count.

So, we can use all of the other event’s attributes EXCEPT to x and y, where there will be the converted point. You follow? This allows the child component (in this case, the contentPane) to receive and handle the event itself.

Glass Pane
The Glass Pane is the method by which the root pane can interfere with events and can paint over top of any components, because it IS on top. By default, it is hidden, but when needed, it is on top of the contentPane and can do things like paint. I will definitely make a tutorial on this later.

Conclusion
Though this tutorial has been short, I hope that Sun’s code along with their comments and my in-depth explanations have helped you to see the advantages of using Glass Panes to intercept events if ever necessary. I must acknowledge Sun for use of their code snippet, so thanks guys! Though complex, these panes are extraordinary and I hope that you find them useful.

This post has been edited by Locke: 07 July 2010 - 07:04 PM
Reason for edit:: Fix [quote] and [url] Tag


Is This A Good Question/Topic? 2
  • +

Page 1 of 1