import javafx.application.*;
import javafx.scene.*;
import javafx.stage.*;
import javafx.scene.layout.*;
public class LoanCalculator extends Application
{
public void start(Stage myStage)
{
myStage.setTitle("Loan Calculator");
FlowPlane rootNode = new FlowPane();
Scene myScene = new Scene( rootNode, 300, 200 );
myStage.setScene( myScene);
myStage.show();
}
public static void main( String [] args)
{
launch(args);
}
}
This code pops up with multiply errors when I try running it with Eclipse. I apologize if it is an obvious error. I am new to coding, I found this piece of code in my book and I wanted to test it out.
Thanks Guys
Maybe is ir just a typo? In your code it reads: FlowPlane instead of FlowPane.
Java FX is supported by JDK starting from JDK 7 update 6.
Updated my JDK 7 update 5 to JDK 8, executed your code, it worked like a magic.
Also correct the FlowPlane to FlowPane as suggested below by Kornel.
Related
I'm trying to build a small user interface using JavaFX, But I'm getting an error like this:
Error: Could not find or load main class myApp Caused by:
java.lang.NoClassDefFoundError: javafx/application/Application
This is my code:
and im using jdk 12.0.2
import javafx.application.Application;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.paint.Color;
import javafx.stage.Stage;
public class myApp extends Application{
public static void main(String[] args) {
Phonebook mybook = new Phonebook();
launch(args);
}
#Override
public void start(Stage primaryStage) throws Exception {
Group group = new Group();
Scene scene = new Scene(group, 600, 300);
scene.setFill(Color.GRAY);
primaryStage.setTitle("Phone Book");
primaryStage.setScene(scene);
primaryStage.show();
}
This is the Libraries and jdk I'm using:
Image1
I think JavaFX is not a part of the JDK > 9 any more. (Your version is 12.x.x)
Probably this could help you:
https://openjfx.io/openjfx-docs/#install-javafx
(Assuming you are using Maven) If this does not help, try to clean and build your Application.
Sometimes Maven does not recognize newly added dependencies.
I am starting an assignment and just installed javafx from the Eclipse marketplace, after I installed and created a javafx project with fxml however in main the imports are showing errors saying "the import javafx cannot be resolved". I am new to Java and Eclipse and have tried to search for similar questions but they all seem to be different cases.
I would appreciate any help thank you.
Below is the code, and all the imports are showing errors:
package application;
import javafx.application.Application;
import javafx.stage.Stage;
import javafx.scene.Scene;
import javafx.scene.layout.BorderPane;
import javafx.fxml.FXMLLoader;
public class Main extends Application {
#Override
public void start(Stage primaryStage) {
try {
BorderPane root = (BorderPane)FXMLLoader.load(getClass().getResource("Student.fxml"));
Scene scene = new Scene(root,400,400);
scene.getStylesheets().add(getClass().getResource("application.css").toExternalForm());
primaryStage.setScene(scene);
primaryStage.show();
} catch(Exception e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
launch(args);
}
}
I have figured that it is the library JavaFx SDK which is not able to be added to the project, is anyone good with Eclipse can help solve this problem? I tried configure build path and removing the JavaFx SDK and readding, still does not work.
This may have been asked before but i was not able to find an answer. Im working on a JavaFX app that contains a lot of scenes and a lot of animation. Currently I'm having different Animationtimers and different Scenes all defined inside the start() function, inside the main class that extends Application. However the code gets very messy and long.
Is there a way in which you can define all of these things in a separate Java class, and then simply do something like primaryStage.setScene(MyScene.getScene) - MyScene being the java class that has all your scene code.
Something like this:
public class TestScene {
private Group root = new Group();
Scene test = new Scene(root);
Button button = new Button("test");
root.getChildren.add(button);
}
And actually having that code be a scene that you can just import and set on primaryStage.
Edit: I have no idea why this was so difficult for my mind, as Bertijn said I obviously just need to use a constructer. For whatever reason I forgot that, and so I obviously couldent perform a root.getChildren.add(button), outside a function of some sort.
If anybody else struggles with this here is the super simple solution:
Class containing our scene:
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.paint.Color;
public class OurScene {
public Scene getScene() {
Group root = new Group();
Scene scene = new Scene(root, Color.GREEN);
Button button = new Button("Hello world!");
root.getChildren().add(button);
return scene;
}
}
And then to add it to primaryStage:
import javafx.application.Application;
import javafx.stage.Stage;
public class Main extends Application {
public static void main(String[] args) {
launch(args);
}
#Override
public void start(Stage primaryStage) throws Exception {
OurScene ourScene = new OurScene();
primaryStage.setScene(ourScene.getScene());
primaryStage.show();
}
}
Try making a class called Scenes. In the constructor, created all your scenes, you can give them an id if you want. In your main class, just create an instance of this class Scenes scenes = new Scenes();. The scenes get created. Then you can access them by creating a getScene(String id) method.
Hope I understand your question correctly, and if this doesn't answer it, feel free to get back to me!
As stated in the answer to this question, one can setup a Timeline to check whether there is a change in the system clipboard:
Set and use variables outside timeline in javafx 8
But is there a better way? For example, an event listener? I have searched JavaFx 8 doc and didn't find anything obviously helpful.
Solutions using JavaFx is preferred, but all answers are welcome.
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.input.Clipboard;
import javafx.scene.layout.StackPane;
import javafx.stage.Stage;
public class Main extends Application {
#Override
public void start(Stage primaryStage) throws Exception {
final Clipboard systemClipboard = Clipboard.getSystemClipboard();
new com.sun.glass.ui.ClipboardAssistance(com.sun.glass.ui.Clipboard.SYSTEM) {
#Override
public void contentChanged() {
System.out.print("System clipboard content changed: ");
if ( systemClipboard.hasImage() ) {
System.out.println("image");
} else if ( systemClipboard.hasString() ) {
System.out.println("string");
} else if ( systemClipboard.hasFiles() ) {
System.out.println("files");
}
}
};
primaryStage.setScene(new Scene(new StackPane()));
primaryStage.show();
}
}
Test:
Press key Print Screen
Ctrl+C for selected string
Ctrl+c for selected files
No, there isn't a better way. There are no events sent round when the clipboard contents change (especially if it's from outside of the Java application) and so only a polling approach is appropriate.
With a JavaFX applet :
The javascript object I get with document.getElementById("APPLET_ID") has no Packages attribute under Windows.
I run my tests on Windows XP with IE8, FF and Chrome up to date, but it's the same problem under windows 7.
Under Ubuntu with JRE 7u7 x64, no such problem.
Here is my test code :
package test;
import netscape.javascript.JSObject;
import javafx.application.Application;
import javafx.application.Platform;
import javafx.scene.Scene;
import javafx.scene.control.TextField;
import javafx.scene.layout.HBox;
import javafx.stage.Stage;
public class Main extends Application {
private JSObject js;
TextField tf;
#Override
public void start(Stage primaryStage) {
js = this.getHostServices().getWebContext();
HBox hb = new HBox();
Scene s = new Scene(hb, 400, 400);
tf = new TextField("MAIN");
primaryStage.setScene(s);
hb.getChildren().add(tf);
primaryStage.show();
runTest();
}
public static void main(String[] args) {
launch(args);
}
void runTest(){
new Thread(new Runnable() {
#Override
public void run() {
try {
Thread.sleep(5000);
} catch (InterruptedException e) {
e.printStackTrace();
}
Platform.runLater(
new Runnable() {
#Override
public void run() {
tf.setText("" + js.eval("document.getElementById('applet_id').Packages == null"));
}
}
);
}
}).start();
}
}
Displays "false" under Ubuntu JRE7u7x64 and "true" under Windows and Ubuntu JRE7u21 with all browsers.
As the JavaFX2 deployment doc page tells I'm doing it the right way, it looks like a JRE bug. What do you think about it ?
http://docs.oracle.com/javafx/2/deployment/javafx_javascript.htm .
Filed on Jira : https://javafx-jira.kenai.com/browse/RT-30732
Looks it's an expected change since 7u21 : http://www.oracle.com/technetwork/java/javase/7u21-relnotes-1932873.html
This Packages attribute no more works.
So if you need callbacks from your JS to your applet, you must access directly it's methods.
And use the Trusted-Library manifest attribute to avoid warnings.
But you can't do what you want when using Trusted-Library. For example, if you use Axis2 webservices.
So you'll have to do it with Trusted-Only manifest attribute. But this forbids you to call methods from JS.
Simple workaround :
Have a thread periodically checking a JS callback queue and treat them. It's painful, horrible POJO but it works.
So to Oracle :
THANKS
for updating online doc
for announcing major changes made in minor releases
for making web integration so easy.
for adding other security checks in u25 that result in 20 seconds gray screen latency
Do you know Adobe ? Heard they have pretty good stuff... I'm pissed.