Win a copy of The Business Blockchain this week in the Cloud forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

how to add javascript in javaFx

 
drashti pandya
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i have tried by giving external java script source in javaFx css but it doesn't work
 
M.Aamir khan
Greenhorn
Posts: 11
Netbeans IDE Python VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
drashti pandya wrote:i have tried


What You have tried yet?
 
drashti pandya
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
#16{
-fx-background-image: url("exit6.png");
}
<script src="JavaScript1.js" type="Text/JavaScript">
</script>

In JavaScript1.js
alert("Hello");
 
M.Aamir khan
Greenhorn
Posts: 11
Netbeans IDE Python VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
drashti pandya wrote:#16{
-fx-background-image: url("exit6.png");
}
<script src="JavaScript1.js" type="Text/JavaScript">
</script>

In JavaScript1.js
alert("Hello");


The Code is Vague , i do not know What you are trying to do...

You might need to look at WebEngine

A Simple Example Could be look like this:


 
drashti pandya
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If i want to add js in controller part then?
 
M.Aamir khan
Greenhorn
Posts: 11
Netbeans IDE Python VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
drashti pandya wrote:If i want to add js in controller part then?


You are Still not Clear....

look at

Communicating between JavaScript and JavaFX with WebEngine,
Adding HTML Content to JavaFX Applications....

and if your mean to load Some Externals.js into your Code than yes , You can...
For Example you can load JQuery into the WebView ...


 
drashti pandya
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
but my application is desktop app
 
John Damien Smith
Ranch Hand
Posts: 296
14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
My advice is:
1. Write better questions that are more than one line and really explain what you are trying to do.
2. Don't mix Java and JavaScript code.
3. Don't use JavaScript to program JavaFX.

---

You put JavaScript code in a CSS file - that will never work, a CSS file is for the CSS language, not JavaScript.

---

You wrote an alert in your JavaScript Sample. Normally an alert triggers a browser to show an alert dialog, it won't do that for JavaScript using JavaFX as there is no browser context to show the alert. I don't know what the alert would do if you tried to use it for a JavaFX program (likely nothing or print to a console). Basically, if you do use JavaScript and JavaFX, then don't use alert.

---

If you really want to program your JavaFX application in JavaScript, you can use Nashorn:
https://docs.oracle.com/javase/8/docs/technotes/guides/scripting/nashorn/javafx.html
Again, I don't recommend that.

---

If you want to include JavaFX in your FXML files, you can do that using the fx:script element:

https://docs.oracle.com/javase/8/javafx/api/javafx/fxml/doc-files/introduction_to_fxml.html#scripting
http://stackoverflow.com/questions/19545727/how-to-load-fxml-dynamically-from-webserver (sample code in answer)

Again, I don't really recommend that.

 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic