I'm in the need of a translucent AWT component/container to be placed on top of other components to receive the mouse events.
The problem here is to create a translucent component. My investigation so far showed that Swing has the possiblity to do setOpaque(false) on e.g. a JPanel. Is there something similiar for AWT?
The idea is to make a ScrollPane scrollable by simply clicking somewhere and moving the mouse around. Thats why I need a transparent component to place it above all other components.
Long story short, I need a translucent component, no matter which one, to place on top of other elements to receive the mouse events.
Thanks in advance
AFAIK not possible correctly for prehistoric AWT Container and Components you can to try that with changing this code from Swing to the AWT, Translucency is supported for Swing JComponents only
The JNA project provides some utilities for making AWT components translucent. There's a WebStart demo of the functionality. The window transparency utilities are in platform.jar, which augments the base jna.jar.
However, if all you want to do is trap mouse events, you should look into the different layers that are already built into a JFrame. The component already has a concept of layers via JLayeredPane, and you can fairly easily use a JPanel as a glass pane which sits above all other components to filter events.
Related
I am learning AWT. I have add a panel inside frame. I have set bounds to the panel such that it stays at the bottom. But when I maximize or minimize, the panel is reset to the center top. Is this a normal behavior? Is there any events for minimize or maximize?
I am learning AWT.
Why? It was replaced by Swing a decade ago. Some people would even argue the Swing has been replaced by JavaFX.
In any case you were given a link in you last question (that you have now deleted) to the Swing tutorial. Why have you not read the tutorial?
The basic concepts of Swing and AWT are the same, since Swing is built on top of AWT, so by reading the Swing tutorial you will learn many of the AWT concepts. In most cases the difference it the component class name.
the panel is reset to the center top. Is this a normal behavior?
Yes, Swing (and AWT) work with layout managers. The layout manager is responsible for determining the size and location of a component. Don't try to manually set the size or location of a component.
Is there any events for minimize or maximize?
Yes, you can use a WindowListener.
Read the Swing tutorial. You will find sections on:
Using Layout Managers
How to Write a WindowListener
I have the following Swing UI structure.
When I scroll the mouse wheel within the blue JPanel, the JScrollPane doesn't respond to the mouse wheel event. Why?
I read from the java doc that:
JScrollPane provides a scrollable view of a lightweight component. ...
Note that JScrollPane does not support heavyweight components.
So is this because my structure is too heavy? Or any other reasons?
ADD 1
I accidentally switched my window with the mouse middle click. And after that, the mouse wheel suddenly worked for the JScrollPane.
This leads me to think maybe it's related to the focus. Then I found below line:
this.setFocusableWindowState(false);
After I changed it to below, mouse wheel works.
this.setFocusableWindowState(true);
Though javadoc says:
Setting a Window's focusability state to false is the standard
mechanism for an application to identify to the AWT a Window which
will be used as a floating palette or toolbar, and thus should be a
non-focusable Window.
At first, I guess it's because the JDialog is not in focusable window state, so it cannot receive events. But actually, mouse click always works. So I am still not sure about the root cause.
It seems a toolbar or a floating palette cannot be focused but still can receive mouse click event. So I guess maybe only certain events are filtered by setFocusableWindowState(false).
About components being heavy weight: AWT components are meant, that use the operating system widgets. You use swing JComponents, J*. Swing components are called light weight as they do all drawing and event handling emulated, in one large native window.
The JPanel should be larger than the JScrollPane, so a "view port" may be scrolled. Have the preferred sizes set correctly.
In general I would have thought that every JTextPane would be in its own JScrollPane.
Also JScrollPane functions a bit differently.
scrollPane = new JScrollPane(panel);
In a board game we are developping in Java we would like the gui to be overshadowed when the game is finished. We have a Jframe in which there is JPanel with the board on which there are some colored pawns and boxes (JButtons) and we would like that everything becomes a sort of black and white and grey. Is there an authomatic method in Java to do this in Java Components?
There are several different kinds of panes to look at that could achieve this, or something similar, if you are using Swing (which I assume from the tag, that you are).
You could use a Glass Pane. http://docs.oracle.com/javase/tutorial/uiswing/components/rootpane.html#glasspane
glass panes could be thought of as like a CSS overlay. They dis-allow interaction with components behind them.
Also, take a look at JXLayers and JLayers, they allow you to modify the way components are actually painted.
Good Luck!
Override paintComponent() (Or if you have some components added to the container it's better to override paintComponents() method).
Call
super.paintComponent(g);
Color semiColor=new Color(0,0,0,128);//the last param represents alpha
g.fillRect(semiColor);
Basically I plan to place some buttons, textfields, labels, etc. on a JFrame and I would like to make it possible that a user can move the different components around on that JFrame with the mouse.
I have seen various ways with MouseListeners, subclassed JComponent code, DropSource/DropTarget implementations and so on, but I'm unsure which is the "recommended" way (I don't need to support "drag and drop" between different Frames/Applications which is what most examples seem to do).
The Component Mover can do this for you.
Use the GlassPane:
http://download.oracle.com/javase/tutorial/uiswing/components/rootpane.html
It's an invisible panel that sits on top of all other components. You can attach mouse listeners to it and then use SwingUtilities.getDeepestComponentAt() to figure out which component was clicked on beneath the GlassPane. Then use a mouseDragged listener on the glasspane and set the component location based on the mouse dragged event.
You will need to set the layout of your container to "null" so the components' setLocation will work.
I've got a problem. In LayeredPane I have 2 components: heavyweight coponent and JComponent(or any other what I could draw text in) that is placed above the first one. I'd like JComponent to have transparent background, but it has black background. Heavyweight coponent is a component that renders video and JComponent is component what I draw text in. This text should be printed with transparent background.
I tried to use GlassPane but glass content of glassPane was displayed above lightweight components and below heavyweight component.
Is there possibility to draw text above heavyweight coponent (Canvas)?
ps. I can't overload paint method because I got object of class HeavyComponent that is subclass of Canvas.
A heavyweight component will generally paint over lightweight components.
JMF is quite old. Investigate some of the newer alternatives. See the section labeled "Alternatives" at http://en.wikipedia.org/wiki/Java_Media_Framework