File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes JavaFX and the fly likes How to apply my custom css rule to a JavaFX Web Browser? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Java » JavaFX
Bookmark "How to apply my custom css rule to a JavaFX Web Browser?" Watch "How to apply my custom css rule to a JavaFX Web Browser?" New topic
Author

How to apply my custom css rule to a JavaFX Web Browser?

Constantine Mosch
Greenhorn

Joined: Jan 20, 2013
Posts: 10
I have cross-posted my post to Oracle Community Forums
I asked the question before four days and I have no reply.
I have read the rules about cross-posting : Be Forthright when Cross Posting To Other Sites
Oracle forum is also informed about cross-posting.
I would appreciate any reply.

I want to apply my css rule to specific nodes of WebView.
I use Node.lookupAll(String cssSelector) method to find the node and Node.setStyle(String ccsStyle) to apply my rules.
As an example I use the following code from JavaFX Documentation










Karthik Shiraly
Ranch Hand

Joined: Apr 04, 2009
Posts: 475
    
    3
WebEngine.load() does not wait until loading has completed. That means if you try to access the web page DOM immediately after load() returns, the page is incompletely loaded.
The right way to do it is to access the DOM after the page has completed loading. The component notifies completion via a listener. It should look like this (from WebEngine documentation):


The other problem in your code is the line:

This code does not find "body" elements in the web page loaded in WebEngine. Instead, what it does is look for a "body" element in the scene's object graph.
When a WebEngine loads a web page, it does not convert the HTML elements in web page into JavaFX Nodes.

One correct way to do what you want is:

Constantine Mosch
Greenhorn

Joined: Jan 20, 2013
Posts: 10
@Karthik Shiraly
Wow!!!
I didn' t expect a so detailed response.Thank you for taking the time to write.
I still have a problem.
I use Jsoup library and I use CSS selectors to find DOM elements.
w3c.dom.Document doesn't support CSS selector and finds elements by id or by tagName

How do I find elements in a w3c.dom.Document using CSS rules (like Jsoup document )?
John Damien Smith
Ranch Hand

Joined: Jan 26, 2012
Posts: 101
    
    7
> I use Jsoup library and I use CSS selectors to find DOM elements.

Jsoup has it's own Document model class and does not provide a way to directly translate a w3c document model to it's document model. Also it's APIs only function on it's internal jsoup document model, not the w3c document model supported by WebView. So I do not believe that you would want to use Jsoup in conjunction with a WebView - they aren't really compatible.

> How do I find elements in a w3c.dom.Document using CSS rules (like Jsoup document )?

I would advise using jQuery and working directly against the dom model via the WebView's JavaScript engine rather than working in Java against the Java w3c document model. There is a sample for this in this gist.
Constantine Mosch
Greenhorn

Joined: Jan 20, 2013
Posts: 10
@John Damien Smith
Thanks for your reply.
executejQuery() method works fine.

I have another problem.
In the code below, I have disable javascript because JavaFX WebEngine crashes
when loads some web pages eg http://www.protothema.gr/
Before I call executejQuery(engine, script), I call engine.setJavaScript(true)

I must press JButton "Change" twice to change tag <a> font color.
How can I fix this problem?
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: How to apply my custom css rule to a JavaFX Web Browser?
 
Similar Threads
Calling Javafx webEngine method 'executeScript' in Java code
webview worked in standalone version but hangs when embedded
Directed Graph Framework Compatible With JavaFX 2.x
Using autofill/autocomplete in Javafx
Rounded corners on Modal Dialogs