JFrame semi-transparent / transparent for events but still visible
Is it possible to create in Java JFrame
or another component completely transparent to all mouse events?
In my application, I only have one window, which is an instance JFrame
. I have many others JComponent
in this window , some of them have more JComponent
, etc. But most importantly, the main window has only one PopUpFrame
, which inherits JFrame
. This one is PopUpFrame
invisible until I move the cursor to a specific one JComponent
with a thumbnail, when this one JComponent
catches an event mouseEntered(MouseEvent event)
, it will make this one PopUpFrame
visible and load the full image into it. I did that PopUpFrame
move with the cursor, but the problem is that the cursor goes out of the sketch PopUpFrame
. For example, ifPopUpFrame
with full image is the right side of the cursor and I move the mouse to the left and / or up, everything works fine. When I move the mouse to the right and / or down, the cursor continues PopUpFrame
, so it is PopUpFrame
"frozen". Of course I can add one additional event to this PopUpFrame
and it will move with the cursor even when the cursor is on it, but this is not a solution. PopUpFrame
should be visible ONLY when the cursor is on the thumbnail. If PopUpFrame
can capture events and thumbnail covers, I cannot catch mouseExited(MouseEvent event)
in the correct thumbnail value.
So, somehow make the JFrame completely transparent to mouse events, but still make it visible on the screen (probably something like that setMouseTransparent
in JavaFX). And if that is not possible, then how to create a full image (and only image) popup that is visible only when the cursor is on the thumbnail and is moved with the cursor.
Here is my code.
import java.awt.Color;
import java.awt.Point;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionAdapter;
import javax.swing.ImageIcon;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
public class MainExample {
private static MainWindow window;
public static void main(String args[]) {
window=new MainWindow();
}
}
class MainWindow extends JFrame {
private static PopUpFrame popup=new PopUpFrame();
private static ThumbnailPanel mini=new ThumbnailPanel("panel");
public MainWindow() {
setVisible(true);
setSize(600, 400);
setLayout(null);
add(mini);
mini.setLocation(50, 75);
}
public static void previewPopUpShow(Point cursor) {
popup.refresh(cursor.x, cursor.y);
}
public static void previewPopUpHide() {
popup.setVisible(false);
}
public static void previewPopUpLoad(String url) {
popup.setImage(url);
}
}
class ThumbnailPanel extends JComponent {
private JPanel thumbnail=new JPanel();
public ThumbnailPanel(String name) {
setVisible(true);
setOpaque(false);
setSize(80, 45);
setBorder(null);
setLayout(null);
add(thumbnail);
thumbnail.setBackground(Color.RED);
thumbnail.setBorder(null);
thumbnail.setBounds(0, 0, 70, 40);
thumbnail.addMouseListener(new MouseAdapter() {
public void mouseEntered(MouseEvent entered) {
MainWindow.previewPopUpLoad("");
}
public void mouseExited(MouseEvent exited) {
MainWindow.previewPopUpHide();
}
});
thumbnail.addMouseMotionListener(new MouseMotionAdapter() {
public void mouseMoved(MouseEvent moved) {
MainWindow.previewPopUpShow(moved.getLocationOnScreen());
}
});
}
}
class PopUpFrame extends JFrame {
private ImageIcon full;
private JLabel fullLabel;
public PopUpFrame() {
setSize(440, 260);
setResizable(false);
setUndecorated(true);
setVisible(false);
setFocusable(false);
setAlwaysOnTop(true);
addMouseMotionListener(new MouseMotionAdapter() {
public void mouseMoved(MouseEvent moved) {
MainWindow.previewPopUpShow(moved.getLocationOnScreen());
}
});
}
void refresh(int x, int y) {
setLocation(x, y);
}
void setImage(String url) {
full=new ImageIcon("full_image.png");
fullLabel=new JLabel();
fullLabel.setOpaque(true);
fullLabel.setBackground(Color.WHITE);
this.getContentPane().add(fullLabel);
this.setVisible(true);
}
}
source to share