I tried to run the following code in my Netbeans 7.2 (Java 1.7.u79) and Netbeans 8.0.2 (Java 1.8 u45) and its not working!
package com.main;
import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.layout.VBox;
import javafx.scene.web.WebEngine;
import javafx.scene.web.WebView;
import javafx.stage.Stage;
public class RBrowser extends Application {
#Override
public void start(Stage primaryStage) {
WebView browser = new WebView();
final WebEngine webEngine = browser.getEngine();
webEngine.load("http://docs.oracle.com/javafx/2/webview/jfxpub-webview.htm");
Button b = new Button("Show Console");
b.setOnAction(new EventHandler<ActionEvent>(){
#Override
public void handle(ActionEvent t) {
webEngine.executeScript("if (!document.getElementById('FirebugLite')){E = document['createElement' + 'NS'] && document.documentElement.namespaceURI;E = E ? document['createElement' + 'NS'](E, 'script') : document['createElement']('script');E['setAttribute']('id', 'FirebugLite');E['setAttribute']('src', 'https://getfirebug.com/' + 'firebug-lite.js' + '#startOpened');E['setAttribute']('FirebugLite', '4');(document['getElementsByTagName']('head')[0] || document['getElementsByTagName']('body')[0]).appendChild(E);E = new Image;E['setAttribute']('src', 'https://getfirebug.com/' + '#startOpened');}");
}
});
VBox root = new VBox();
root.setAlignment(Pos.CENTER);
root.getChildren().addAll(browser,b);
Scene scene = new Scene(root, 700, 550);
primaryStage.setTitle("Google Map");
primaryStage.setScene(scene);
primaryStage.show();
}
/**
* #param args the command line arguments
*/
public static void main(String[] args) {
launch(args);
}
}
I tried to access http://docs.oracle.com/javafx/2/webview/jfxpub-webview.htm in the Google Chrome Browser and it was loaded successfully, WHAT'S WRONG WITH MY CODE?
Which version of JDK have you installed. I am using IDK 8u77 on Windows 8.1 and running on Eclipse Mars2 your code, it worked perfectly. Try latest version from here. Check it works or not.
Related
I'm using OpenJDK11.
I have two Java files in the current folder, that are supposed to run together to be a JavaFX application.
One of them is called Main.java and runs the main window. Another is Alert.java, and is supposed to run an alternate window which is an alert type.
Now, I ran the following command:
javac -cp "c:\projects\java\currentProject" --module-path "c:\Program Files\Java\javafx-sdk-11.0.1\lib" --add-modules=javafx.controls,javafx.fxml Alert.java Main.java
While Alert.java compiled just fine, Main.java could not import the Alert class and gave an error on "import Alert". I tried "import Alert.Alert" and "import currentProject.Alert" but still, it didn't work.
Also, I declared package "package currentProject" at the start of each file and it still gave an error.
What am I supposed to do to get it running? I already failed on installing JavaFX on all available IDEs, so I'm not going to use an IDE other than Atom. But how do I compile it properly?
more info -
file structure:
c->projects->java->economicManager->( Alert.java ,Main.java, financialManager.fxml, alert.fxml, Alert.class, Alert$Controller.class, Main.class [previously compiled version])
Alert.java:
package financialManager;
import javafx.stage.Stage;
import javafx.stage.Modality;
import javafx.scene.Scene;
import javafx.scene.Parent;
import javafx.scene.control.Button;
import java.util.Map;
public class Alert {
public Stage stage;
private Controler_Class controler;
public Alert(Parent root) {
Controler_Class clas = new Controler_Class(root);
this.controler = clas;
Scene scene = new Scene(root);
Stage stage = new Stage();
stage.initModality(Modality.APPLICATION_MODAL);
stage.setTitle("financial report");
stage.setScene(scene);
this.stage = stage;
stage.showAndWait();
}
private class Controler_Class{
Parent root;
public Controler_Class(Parent root){
}
}
}
Main.java:
package financialManager;
import Alert;
import javafx.application.Application;
import javafx.stage.Stage;
import javafx.fxml.FXMLLoader;
import javafx.scene.Scene;
import javafx.scene.Parent;
import javafx.scene.control.ListView;
import javafx.scene.control.SplitPane;
import javafx.scene.control.Button;
import javafx.scene.layout.AnchorPane;
import javafx.scene.layout.VBox;
import java.util.Map;
public class Main extends Application{
#Override
public void start(Stage stage) throws Exception{
final int width = 300;
final int height = 450;
stage.setTitle("hello mofos");
FXMLLoader loader = new FXMLLoader(getClass().getResource("financialManager.fxml"));
Parent root = loader.load();
Scene scene = new Scene(root, width, height);
Map<String, Object> mapper = loader.getNamespace();
AnchorPane pane = (AnchorPane) mapper.get("splitpane1_anchorpane");
if(pane != null)
SplitPane.setResizableWithParent(pane, false);
else
System.out.println("it's null you idiot!");
Button btn = (Button) mapper.get("economicReport");
btn.setOnMouseClicked((event) -> {
FXMLLoader loader2 = new FXMLLoader(getClass().getResource("alert.fxml"));
Parent parent = loader2.load();
Alert alert = new Alert(parent);
});
/*
ChangeListener<Number> stageSizeListener = (observable, oldValue, newValue) ->
pane.setDividerPositions(0.20219435736677116);
stage.widthProperty().addListener(stageSizeListener);
stage.heightProperty().addListener(stageSizeListener);
*/
stage.setScene(scene);
stage.show();
}
public static void main(String[] args){
launch();
}
}
I see that you are importing the Alert class wrongly. Your package is financialManager, so you should use it in the import line like this:
import financialManager.Alert;
About your issues with IDEs, I made JavaFX work fine with Eclipse and IntelliJ on OpenJDK 11 without any issues a few days ago - For OpenJDK you will need OpenJFX, and if you are interesting in some reading, this is the link from Oracle's blog on their plans for JavaFX.
Good luck!
I want to implement file drag and upload in java webview, however, it keeps crashing JVM when any type of upload is initiated. Tested on latest JDK 8 and 9 on windows 10. I'm lost as to what can be causing this issue.
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.layout.VBox;
import javafx.scene.web.WebEngine;
import javafx.scene.web.WebView;
import javafx.stage.Stage;
public class DropFileUpload extends Application {
public static void main(String[] args) {
Application.launch(args);
}
#Override
public void start(final Stage stage) {
WebView webView = new WebView();
final WebEngine webEngine = webView.getEngine();
//webEngine.load("https://html5demos.com/dnd-upload/");
webEngine.load("http://www.dropzonejs.com");
VBox root = new VBox();
root.getChildren().add(webView);
Scene scene = new Scene(root);
stage.setScene(scene);
stage.show();
}
}
I was trying to play some audio and video files on a laptop usingJavaFX MediaPlayer and MediaView but the media didn't work and also the application didn't give any exceptions, but when i moved the application to another laptop it worked well
Both Laptops are HP and working on MS Widows 8 OS, under JDK 8u102
here's the source code
import java.io.File;
import java.net.MalformedURLException;
import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.layout.StackPane;
import javafx.scene.media.Media;
import javafx.scene.media.MediaPlayer;
import javafx.stage.Stage;
/**
*
* #author Anoos
*/
public class Task1 extends Application {
#Override
public void start(Stage primaryStage) throws MalformedURLException {
Button btn = new Button();
btn.setText("Say 'Hello World'");
btn.setOnAction(new EventHandler<ActionEvent>() {
#Override
public void handle(ActionEvent event) {
try {
Media media
= new Media(new File("002.mp3").
toURI().toURL().toString());
System.out.println(media.getSource());
MediaPlayer player = new MediaPlayer(media);
player.play();
} catch (Exception e) {
e.printStackTrace();
}
}
});
StackPane root = new StackPane();
root.getChildren().add(btn);
Scene scene = new Scene(root, 300, 250);
primaryStage.setTitle("Hello World!");
primaryStage.setScene(scene);
primaryStage.show();
}
/**
* #param args the command line arguments
*/
public static void main(String[] args) {
launch(args);
}}
so, can any one help me please ?
and thank you in advance (:
I want my java application such that if user chooses to click on a button the PDF opens using the default PDF reader that is installed in the computer.
The PDF which i want to be opened is present in same package "application".
The code which I am using is
package application;
import java.io.File;
import javafx.application.Application;
import javafx.application.HostServices;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.layout.StackPane;
import javafx.stage.FileChooser;
import javafx.stage.Stage;
public class Main extends Application {
#Override
public void start(final Stage primaryStage) {
Button btn = new Button();
btn.setText("Load PDF");
btn.setOnAction(new EventHandler<ActionEvent>() {
#Override
public void handle(ActionEvent event) {
File pdfFile = new File("computer_graphics_tutorial.pdf");
getHostServices().showDocument(pdfFile.toURI().toString());
}
});
StackPane root = new StackPane();
root.getChildren().add(btn);
Scene scene = new Scene(root, 300, 250);
primaryStage.setScene(scene);
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
}
If the PDF file is in the same package as the caller file (as you state), then
getHostServices().showDocument(getClass()
.getResource("computer_graphics_tutorial.pdf").toString());
should solve the problem.
The getResource method can be used really flexibly to locate files. Here is a small description how to use it: JavaFX resource handling: Load HTML files in WebView.
Is there a way to use javaFx HTMLEditor in source mode, like when you select show source code in firefox?
I need this because I want to load strings which include xml tags in the editor and the wigdet is not showing them.
Not HTMLEditor nor Webengine or Webview contains a method like your needs. The only thing I've found is something to display the html in a different textarea. Maybe this will help you.
import javafx.application.Application;
import javafx.event.Event;
import javafx.event.EventType;
import javafx.scene.Scene;
import javafx.scene.control.TextArea;
import javafx.scene.layout.Priority;
import javafx.scene.layout.VBox;
import javafx.scene.web.HTMLEditor;
import javafx.stage.Stage;
public class JavaFXApplication1 extends Application {
#Override
public void start(Stage primaryStage) {
HTMLEditor editor = new HTMLEditor();
editor.setHtmlText("<html>"
+ "<head>"
+ "<title>A Test</title>"
+ "</head>"
+ "<body>This is just a Test</body>"
+ "</html>");
TextArea area = new TextArea();
area.setText(editor.getHtmlText());
editor.addEventHandler(EventType.ROOT, (Event event) -> {
area.setText(editor.getHtmlText());
});
VBox root = new VBox();
VBox.setVgrow(area, Priority.ALWAYS);
root.getChildren().addAll(editor, area);
Scene scene = new Scene(root, 300, 250);
primaryStage.setScene(scene);
primaryStage.show();
}
/**
* #param args the command line arguments
*/
public static void main(String[] args) {
launch(args);
}
}