Page 1 of 1

[SOLVED] JLayeredPane not work on CefBrowser

PostPosted: Wed Sep 20, 2023 12:02 am
by micrikit
I want some Swing component on jcef browser so I coded like below:

Code: Select all
browser = jcef.getClient().createBrowser(startPageUrl, false, false);
browserUI = browser.getUIComponent();

JPanel browserPanel= new JPanel();
browserPanel.setLayout(new BorderLayout());
browserPanel.setPreferredSize(new Dimension(width, height));
browserPanel.add(browserUI);

this.add(browserPanel, JLayeredPane.DEFAULT_LAYER);//browser's layer set to DEFAULT here

JLabel button= new JLabel();
button.setBounds(20, 20, 100, 100);
button.setOpaque(true);
button.setBackground(Color.red);

this.add(button, JLayeredPane.MODAL_LAYER);//button's layer set to MODAL



but 'button' component put on behind 'browserPanel' component.
how to put 'button' on front of 'browserPanel' ?
maybe jalayeredpane not work correctly on jcef browser ?
if anyone know something , please give me advice.
thank you for reading.

Re: JLayeredPane not work on CefBrowser

PostPosted: Mon Sep 25, 2023 1:29 am
by Phylanx
Hi!

I guess you have a problem of mixing AWT(heavyweight) and Swing (lightweight) components.
We had the same problem with using swings JMenu/JMenuItem objects.

You can either use AWT components (e.g. Button instead of JButton) or use these statements before initializing the GUI:

Code: Select all
JPopupMenu.setDefaultLightWeightPopupEnabled(false);
ToolTipManager.sharedInstance().setLightWeightPopupEnabled(false);


That fixed it for us but you have to test this for yourself, nothing guaranteed.

Re: JLayeredPane not work on CefBrowser

PostPosted: Wed Sep 27, 2023 8:02 pm
by micrikit
Phylanx wrote:Hi!

I guess you have a problem of mixing AWT(heavyweight) and Swing (lightweight) components.
We had the same problem with using swings JMenu/JMenuItem objects.

You can either use AWT components (e.g. Button instead of JButton) or use these statements before initializing the GUI:

Code: Select all
JPopupMenu.setDefaultLightWeightPopupEnabled(false);
ToolTipManager.sharedInstance().setLightWeightPopupEnabled(false);


That fixed it for us but you have to test this for yourself, nothing guaranteed.


Thank you for your advise.
Following you said, using AWT component instead of Swing one solve the problem! Thank you!
But is there really no way to put Swing component on Jcef's browser ?
Because I want put transparent mask on it but awt.Panel not work with.

Re: JLayeredPane not work on CefBrowser

PostPosted: Thu Sep 28, 2023 9:07 am
by KatrinaS
Lightweight and Heavyweight Components aren't really compatible, and the Canvas class used isn't a Container, but if you control the HTML / Document in the Browser you can overlay as much / many transparent DIV's (or whatever) as you like in that :)

Re: JLayeredPane not work on CefBrowser

PostPosted: Fri Sep 29, 2023 12:21 am
by micrikit
KatrinaS wrote:Lightweight and Heavyweight Components aren't really compatible, and the Canvas class used isn't a Container, but if you control the HTML / Document in the Browser you can overlay as much / many transparent DIV's (or whatever) as you like in that :)


Okay ,
This time I give up putting mask on browser though,
If I have to do it, I'll do with the way you said!
Thank you!

Re: [SOLVED] JLayeredPane not work on CefBrowser

PostPosted: Mon Oct 02, 2023 9:33 am
by Phylanx
Well, look it that way...

Heavyweight means:
All Components (Buttons, Checkboxes, Canvas, Labels,...) are the same Components on the Operating System side.

Lightweight means:
Java takes a heavyweight component (e.g. a window, canvas,...) and draws the swing components on them.
So if you create a java swing JButton and add it to a Window, it is only drawn as a Button Window, but it is no button on the operating system.
If you click on the JButton, the click goes on the operating systems Window, Java sees the click was on the range where the button is located and therefore changes the UI of the button (so it looks like as it is clicked on) and triggers the listeners that are registered on the JButton.

The native C Canvas that JCEF/Chromium is using to draw HTML on it is a heavyweight component and knows nothing about Javas JButton.
Also Java doesn't know what the native Chromium code draws on the Canvas.