2

As I'm making JavaFX application three in three WebView in my application, I have to perform zoom in and out function while I press + and - key. Code is given below:

  StackPane root = new StackPane();

        HBox hbox = new HBox(30); // create a HBox to hold 2 vboxes        

        // create a vbox with a textarea that grows vertically
        VBox vbox = new VBox(10);  
        //Label label1 = new Label("");

        final WebView img = new WebView();
         final WebEngine Img = img.getEngine();
         final DoubleProperty zoomProperty = new SimpleDoubleProperty(200);
 img.addEventFilter(KeyEvent.KEY_RELEASED, (KeyEvent e) -> {
            if (e.getCode() == KeyCode.ADD || e.getCode() == KeyCode.EQUALS || e.getCode() == KeyCode.PLUS) {
                System.out.println("YES");
                    zoomProperty.set(zoomProperty.get() * 1.1);
            }
            else if(e.getCode()== KeyCode.SUBTRACT||e.getCode() == KeyCode.MINUS ){
                System.out.println("YES");
                zoomProperty.set(zoomProperty.get() / 1.1);
            }
        });

As through this code im able to listen to the key but zoom in and zoom out is not working. Please help as I have to change mouse pointer to double headed arrow then listen to arrow and + and - key for zoom in and out.

trashgod
  • 203,806
  • 29
  • 246
  • 1,045
Narendra.kr
  • 181
  • 3
  • 12
  • Please add a meaningful readable title to your question. Don't just copy your question to your title or vice versa. PS: There is a "shift" hey on your keyboard. Consider using it. – Clijsters Oct 09 '17 at 10:17
  • What happens if you update the zoom property of the WebView? – trashgod Oct 09 '17 at 11:36
  • nothing happens as it has already have the functionality with mouse listener to zoom in and zoom out but i dont want it with mouse listener i want it with key listener – Narendra.kr Oct 09 '17 at 11:39
  • As it works for me, I've elaborated below. – trashgod Oct 09 '17 at 11:56

1 Answers1

3

It looks you want to alter the zoom state of the WebView based on an event, so you should update the zoom property held by the WebView itself. Starting form this example, I added the following EventFilter based on your example. It appears to work as expected.

webView.addEventFilter(KeyEvent.KEY_RELEASED, (KeyEvent e) -> {
    if (e.getCode() == KeyCode.ADD || e.getCode() == KeyCode.EQUALS
            || e.getCode() == KeyCode.PLUS) {
        System.out.println("+");
        webView.setZoom(webView.getZoom() * 1.1);
    }
    else if(e.getCode() == KeyCode.SUBTRACT || e.getCode() == KeyCode.MINUS ){
        System.out.println("-");
        webView.setZoom(webView.getZoom() / 1.1);
    }
}); 

image

trashgod
  • 203,806
  • 29
  • 246
  • 1,045
  • trashgod how to change this to set zoom level so when we zoom first picture the second image should load with same level. – Narendra.kr Nov 30 '17 at 06:42
  • Maybe save the current zoom, perhaps among your current `Preferences` for [example](https://stackoverflow.com/a/34616583/230513), so you can restore it when needed. – trashgod Nov 30 '17 at 18:58