0

I'm learning javafx and am creating a TodoList application. I want to include certain features like text styling, using bullet lists etc. and for that I have added an HTMLEditor to my app, which stores html files for my WebView to load. To test the saving and loading of the html files I have saved a sample 'test.html' file (which is not in my classpath) and want the WebView to load it. here's some of my code:

Main.fxml

<?xml version="1.0" encoding="UTF-8"?>

<?import java.lang.*?>
<?import java.util.*?>
<?import javafx.scene.*?>
<?import javafx.scene.control.*?>
<?import javafx.scene.layout.*?>

<?import javafx.scene.web.WebView?>
<GridPane xmlns="http://javafx.com/javafx" xmlns:fx="http://javafx.com/fxml" fx:controller="sample.Controller" hgap="10" vgap="10">
    <WebView GridPane.rowIndex="0" GridPane.columnIndex="0" fx:id="webView"/>
</GridPane>

Fxml Controller.java

public class Controller{

@FXML private WebView webView;
private WebEngine engine = webView.getEngine();

 @FXML
    public void initialize() throws IOException{
        //code for some ArrayList initialization

        engine.load("/home/jyotiproy/TodoOutput/test.html");

    }
}

Main.java that loads the program

public class Main extends Application {

    @Override
    public void start(Stage primaryStage) throws Exception{
        Parent root = FXMLLoader.load(getClass().getResource("Main.fxml"));
        primaryStage.setTitle("Todo List");
        primaryStage.setScene(new Scene(root, 1200, 600));
        primaryStage.show();
        primaryStage.setResizable(false);
    }


    public static void main(String[] args) {
        launch(args);
    }
}

I'm getting no errors or exceptions. There is no typo in the path of the 'test.html' and the html editor works fine and saves the test.html, but the WebView loads nothing. Here is my app structure: WebView HTMLEditor

jyoti proy
  • 138
  • 3
  • 9
  • Read as a `File`. Convert the file content to a `String`. – SedJ601 Jul 28 '19 at 07:17
  • `String htmlString = new String (Files.readAllBytes(Paths.get(filePath)));` – SedJ601 Jul 28 '19 at 07:19
  • @Sedrick used the following: `` File f = new File("/home/jyotiproy/TodoOutput/test.html"); engine.load(f.toString());`` still nothing – jyoti proy Jul 28 '19 at 07:19
  • That's not how to convert a file's content to a string. – SedJ601 Jul 28 '19 at 07:23
  • Did you try `System.out.println(f.toString())` to see what the problem is? – SedJ601 Jul 28 '19 at 07:25
  • the output is the file path: /home/....../test.html – jyoti proy Jul 28 '19 at 07:25
  • Which is what it should be. Use my second comment. – SedJ601 Jul 28 '19 at 07:29
  • @Sedrick Pardon me if I get it wrong, but the code from the second comment converts the entire file contents to a string and then by the .load method, we pass that string to the WebEngine? I did that, but nothing happens :( – jyoti proy Jul 28 '19 at 07:36
  • 1
    The [`WebEngine#load(String)`](https://openjfx.io/javadoc/12/javafx.web/javafx/scene/web/WebEngine.html#load(java.lang.String)) methods expects a URL in the form of a `String`. Try something like `"file:///home/jyotiproy/TodoOutput/test.html"`. If you use the approach suggested by @Sedrick (i.e. read the _contents_ of the file into a `String`) then you'll want to use [`WebEngine#loadContent(String)`](https://openjfx.io/javadoc/12/javafx.web/javafx/scene/web/WebEngine.html#loadContent(java.lang.String)). – Slaw Jul 28 '19 at 07:53
  • @Slaw I used this ```engine.load("file:///home/jyotiproy/TodoOutput/test.html");``` but still nothing happens. Now I'm thinking whether I have done something wrong in the FXML declaration or have not associated it with the controller properly. Is there something wrong with that code? – jyoti proy Jul 28 '19 at 08:09
  • @Slaw good catch on the `loadContent(String)`. – SedJ601 Jul 28 '19 at 08:11
  • Well, `private WebEngine engine = webView.getEngine();` looks like it should be throwing an NPE. If it's not, then you're doing something that isn't being shown (i.e. your code is not a [mre]). – Slaw Jul 28 '19 at 08:13
  • @Slaw used the .loadContent this way ```File f = new File("/home/jyotiproy/TodoOutput/test.html");``` ```String htmlString = new String (Files.readAllBytes(Paths.get(f.toString())));``` ``` engine.loadContent(htmlString);``` still nothing happens – jyoti proy Jul 28 '19 at 08:13
  • @Slaw I've added the complete code (which concerns the WebView) please check it. – jyoti proy Jul 28 '19 at 08:29
  • 3
    Never instantiate fields that are meant to be injected by an `FXMLLoader`. The `WebEngine` you have a reference to is associated with the `WebView` you created, not the `WebView` that was created (and injected) by the `FXMLLoader` (i.e. the one being displayed). – Slaw Jul 28 '19 at 08:30
  • @Slaw Thank you very much. I rewrote the code and this time did not instantiate the WebView but only the WebEngine and now it work as it should! – jyoti proy Jul 28 '19 at 08:40

1 Answers1

1

Thanks to @Slaw and @Sidrick the question was solved. The changes that were needed in the original code were the addition of the file:// in the engine.load() part of the code.

Working Code

@FXML private WebView webView;



    @FXML
    public void initialize() throws IOException{
        //Some ArrayList Initialization 

        WebEngine engine = webView.getEngine();
        engine.load("file:///home/jyotiproy/Todolist/test2.html");
    }

Screenshot:

Working app

jyoti proy
  • 138
  • 3
  • 9
  • it's not forum but a knowledge base produced by the community :) Personally, I agree with explaining a downvote (wasn't mine but could have been because you didn't do your very best - provide a [mcve] - to make the question answerable, piling up comments is not the way to go, so your code indeed is far from _adequate_). But it's explitely _not_ mandatory (please read the help pages on voting). And with all due respect: judging by your reputation, a statement like _doesn't fullfill the purpose of this site_ is quite daring ;) – kleopatra Jul 28 '19 at 10:31
  • 1
    it's not complete - I can't throw the files into my IDE, compile and run to reproduce your problem. – kleopatra Jul 28 '19 at 10:52
  • 1
    Almost. One of the problems you've described is that the code runs without errors but fails to produce the desired results. The reason was evident in the images of your code that you added (and then [correctly removed](https://meta.stackoverflow.com/questions/285551/why-not-upload-images-of-code-on-so-when-asking-a-question)). However, the code you're currently showing (assuming normal execution) will result in a `NullPointerException` because you don't show the erroneous initialization of the `webView` field. It'd also be nice to have the bare-bones `Application` class. – Slaw Jul 28 '19 at 11:14
  • 1
    Note I also was not the downvoter, but just to add: Keep in mind a [mre] is just that—an example of the problem. One of the things I often see with newer users is a tendency to code dump their entire project (not minimal) or simply omit at least half the code which they _believe_ is not relevant (not complete). The example should be _specially created_ for Stack Overflow and contain the _minimal_ amount of code to _reproduce_ the problem. The [mre] link provides some useful tips on how to isolate the problem, which while in the process of doing one might even solve the problem themselves. – Slaw Jul 28 '19 at 11:27
  • @Slaw thank you for clarifying. The code I didn't provide was that of the Main class which creates the stage to show the webView and launches the program. It may be sufficient now. – jyoti proy Jul 28 '19 at 11:30
  • 1
    looks good now :) As a very last nit-pick: _minimal_ shouldn't contain anything that's unrelated .. such as code comments about irrelevant ommitted lines, but that's certainly not important – kleopatra Jul 28 '19 at 11:44
  • 1
    @kleopatra got it. Next time I'll keep such things in mind! Thanks! :) – jyoti proy Jul 28 '19 at 12:00