[SOLVED] JLayeredPane not work on CefBrowser

Having problems with building or using the JCEF Java binding? Ask your questions here.

[SOLVED] JLayeredPane not work on CefBrowser

Postby micrikit » Wed Sep 20, 2023 12:02 am

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.
Last edited by micrikit on Fri Sep 29, 2023 12:22 am, edited 2 times in total.
micrikit
Techie
 
Posts: 14
Joined: Tue Jun 14, 2022 2:10 am

Re: JLayeredPane not work on CefBrowser

Postby Phylanx » Mon Sep 25, 2023 1:29 am

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.
Phylanx
Expert
 
Posts: 211
Joined: Thu Aug 11, 2016 8:17 am

Re: JLayeredPane not work on CefBrowser

Postby micrikit » Wed Sep 27, 2023 8:02 pm

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.
micrikit
Techie
 
Posts: 14
Joined: Tue Jun 14, 2022 2:10 am

Re: JLayeredPane not work on CefBrowser

Postby KatrinaS » Thu Sep 28, 2023 9:07 am

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 :)
KatrinaS
Mentor
 
Posts: 87
Joined: Tue Jul 04, 2023 6:30 pm

Re: JLayeredPane not work on CefBrowser

Postby micrikit » Fri Sep 29, 2023 12:21 am

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!
micrikit
Techie
 
Posts: 14
Joined: Tue Jun 14, 2022 2:10 am

Re: [SOLVED] JLayeredPane not work on CefBrowser

Postby Phylanx » Mon Oct 02, 2023 9:33 am

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.
Phylanx
Expert
 
Posts: 211
Joined: Thu Aug 11, 2016 8:17 am


Return to JCEF Forum

Who is online

Users browsing this forum: Google [Bot] and 9 guests