Open PDF file with associated program in JavaFX - java

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.

Related

JavaFX throws ERROR_MEDIA_INVALID when playing certain mp3 files

I keep getting an ERROR_MEDIA_INVALID error when trying to play some .mp3 files via the JavaFX media player. Previous google searches have just led to old bug reports, and I'm not sure if this an actual bug or something I missed.
Example:
package sample;
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;
import java.io.File;
public class Main extends Application {
#Override
public void start(Stage primaryStage) throws Exception{
primaryStage.setTitle("Hello World!");
Button btn = new Button();
btn.setText("Say 'Hello World'");
btn.setOnAction(new EventHandler<ActionEvent>() {
#Override
public void handle(ActionEvent event) {
//change filename as needed
var file = new File("C:/Users/tgaravaglia/Downloads/test.mp3");
var media = new Media(file.toURI().toString());
var player = new MediaPlayer(media);
player.setOnError(new Runnable()
{
#Override
public void run()
{
//ERROR_MEDIA_INVALID here
player.getError().printStackTrace();
}
});
player.play();
}
});
StackPane root = new StackPane();
root.getChildren().add(btn);
primaryStage.setScene(new Scene(root, 300, 250));
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
}
Stack trace:
MediaException: UNKNOWN : [com.sun.media.jfxmediaimpl.platform.gstreamer.GSTMediaPlayer#6f4b481f] ERROR_MEDIA_INVALID: ERROR_MEDIA_INVALID
at javafx.media/javafx.scene.media.MediaException.getMediaException(MediaException.java:160)
at javafx.media/javafx.scene.media.MediaPlayer$_MediaErrorListener.onError(MediaPlayer.java:2623)
at javafx.media/com.sun.media.jfxmediaimpl.NativeMediaPlayer$EventQueueThread.HandleErrorEvents(NativeMediaPlayer.java:692)
at javafx.media/com.sun.media.jfxmediaimpl.NativeMediaPlayer$EventQueueThread.run(NativeMediaPlayer.java:426)
Working file: https://drive.google.com/file/d/1e3k9gVhV_hDehWHwhHElXC2jU4aKn6oc/view?usp=sharing
Broken file: https://drive.google.com/file/d/1_VKx4zLH6lFLv6VJdDvrP6IP3c-TjOYo/view?usp=sharing
So... I figured it out. That bad "mp3" file is actually a .wav file in disguise. I ran it through a cloud-based wav->mp3 converter and now it works fine. Hopefully this helps someone in the future with the same problem!

JavaFX invalid URL or resource not found

i am having some trouble including images in my JavaFX project. The project stores the resources in the src/main/resources folder and it houses the FXML documents and all the images. The FXML documents load just fine, but the image locations all show null.
My code is as follows...
package VennDiagram;
import java.io.File;
import javax.imageio.ImageIO;
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.image.Image;
import javafx.stage.Stage;
public class View extends Application{
public static Stage primaryStage;
public static Scene promptWindow;
public static Scene refactor;
public static Scene scene;
public static void main(String[] args) {
launch(args);
}
#Override
public void start(Stage stage) throws Exception{
System.out.println(getClass().getResource("/views/View.fxml"));
System.out.println(getClass().getResource("/views/openingPage.fxml"));
System.out.println(getClass().getClassLoader().getResource("/views/openingPage.css"));
primaryStage = stage;
System.out.println(getClass().getResourceAsStream("/views/newFileButton.png"));
Parent root = FXMLLoader.load(getClass().getResource("/views/View.fxml"));
Parent root2 = FXMLLoader.load(getClass().getResource("/views/openingPage.fxml"));
scene = new Scene(root);
promptWindow = new Scene(root2,1020,580);
primaryStage.setOnCloseRequest(event ->{
quitProgramAlert.display("Confirm Exit", "Are you sure you want to exit?");
if(!quitProgramAlert.closePressed) {
event.consume();
}
});
primaryStage.setMinHeight(600);
primaryStage.setMinWidth(750);
primaryStage.setTitle("VennDiagram Creator");
primaryStage.setScene(promptWindow);
primaryStage.setResizable(false);
primaryStage.show();
}
}
It gives me an error saying the resource cannot be found.
Additional Details:
The project is built using gradle. I load my FXML files in using the same method as i do for the images.
It works just fine with the the FXML documents.
EDIT: The project structure is shown below...

Javafx Button With EventHandler. Getting cannot find symbol error

I am trying to get a button to print out Java is fun after being clicked and I keep getting a Cannot find symbol error when I run it. I'm not sure whether it is my import statements or an actual error in the code itself. I was wondering if someone could help me figure out why I'm getting the error?
import javafx.application.Application;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.layout.Pane;
import javafx.stage.Stage;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
public class ButtonHandler extends Application {
#Override // Override the start method in the Application class
public void start(Stage primaryStage) {
Pane pane = new Pane();
Button btOK = new Button("OK");
pane.getChildren().add(btOK);
btOK.setOnAction((ActionEvent e) -> {
System.out.println("Java is Fun");
});
// Create a scene and place it in the stage
Scene scene = new Scene(Pane);
primaryStage.setTitle("Button Demo"); // Set title
primaryStage.setScene(scene); // Place the scene in the stage
primaryStage.show(); // Display the stage
}
public static void main(String[] args) {
launch(args);
}
}

Html source in javafx HTMLEditor

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);
}
}

TextField right alignment issue javafx

I have textfield that containing a value and that value should be Right alignment.
When i run the application it display the text left alignment but i set the Right alignment.
and problem with 3rd textfield.
After clicking the update button it works fine.
So may i know why it is behave different.
Code:
import java.io.File;
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.control.Label;
import javafx.scene.control.TextField;
import javafx.scene.layout.VBox;
import javafx.stage.FileChooser;
import javafx.stage.Stage;
public class TextFieldAlignment extends Application {
TextField rText;
File file;
public static void main(String[] args) {
launch(args);
}
#Override
public void start(Stage stage) {
rText = new TextField("updated right1 updated right2 updated right3 updated right4");
rText.setAlignment(Pos.CENTER_RIGHT);
Button btn = new Button("update");
btn.setOnAction(new EventHandler<ActionEvent>() {
#Override
public void handle(ActionEvent t) {
rText.setText("updated right1 updated right2 updated right3 updated right4");
// applyWorkaround();
}
});
final Label labelFile = new Label();
Button btn2 = new Button();
btn2.setText("Open FileChooser'");
btn2.setOnAction(new EventHandler<ActionEvent>() {
#Override
public void handle(ActionEvent event) {
FileChooser fileChooser = new FileChooser();
//Set extension filter
FileChooser.ExtensionFilter extFilter = new FileChooser.ExtensionFilter("AVI files (*.exml)", "*.exml");
fileChooser.getExtensionFilters().add(extFilter);
//Show open file dialog
file = fileChooser.showOpenDialog(null);
// rText.setText(file.getPath());
}
});
VBox root = new VBox();
root.getChildren().addAll(rText, btn, btn2);
stage.setScene(new Scene(root, 200, 300));
stage.show();
}
}
Put the setalignment and settext after you add the TextField to the Scene.
Ref: Java API docs for Node
Node objects may be constructed and modified on any thread as long
they are not yet attached to a Scene. An application must attach nodes
to a Scene, and modify nodes that are already attached to a Scene, on
the JavaFX Application Thread.

Categories