• Post Reply Bookmark Topic Watch Topic
  • New Topic

how to set key event listener and mouse event listener in webview in Java?  RSS feed

 
Niti Kapoor
Ranch Hand
Posts: 251
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
actually i want to set the mouselistener and keylistener in webview for zoom in and zoom out mousevent for selecting the particular region of image and and to change cross hair cursor to arrow and keylistener with + key zoomout and -zoomin and with space key crop the image and f5 for saving .




so im trying to add event listener to webview like this but nothing happen so please help or tell me is it possible to do all this function in webview or not.
 
Niti Kapoor
Ranch Hand
Posts: 251
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
  this is working can some one tell how to add selecting particular region  function to mouse event first for and than cut the selected region using space key ?
 
Knute Snortum
Sheriff
Posts: 4209
123
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think you will nee multiple Regions all checking for keyboard input.
 
Niti Kapoor
Ranch Hand
Posts: 251
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
how knute?
 
Knute Snortum
Sheriff
Posts: 4209
123
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well, I'm not sure how you are going to get the x and y coordinates of a keypress (is there such a thing?) but getting the coordinates of a mouseClick is straightforward:

 
Niti Kapoor
Ranch Hand
Posts: 251
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
im getting error on first line incompatible types cannot infer functional interface descriptor for event handler<?super MouseEvent>
  img.setOnMouseClicked((MouseEvent event) -> {
 
Knute Snortum
Sheriff
Posts: 4209
123
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please post the complete code that produced that error, or an SSCCE.  My little program runs without error.
 
Niti Kapoor
Ranch Hand
Posts: 251
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
this is my code to run login userid- rajveersingh.kumar5@gmail.com  password-rajveer405
 
Knute Snortum
Sheriff
Posts: 4209
123
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There are a lot of problems with this code.  This is a good illustration of why bad formatting is a bug.  It's not just that it's not pretty, it actually causes errors in your coding.

First, let's start with your imports.  You have them in no particular order and you have a lot of imports that you're not using.  This is what your imports look like properly formatted:

(If you are using Eclipse, Ctrl-Alt-o will format your imports for you.)

Can you see the problems now?  You have Swing and AWT classes in your JavaFX program.  Change the MouseEvent to its JavaFX counterpart.  We'll have to deal with the JOptionPane later.

Now look at your code:

(In Eclipse, you can format your code automatically with Ctrl-Alt-f)

This poor formatting masks a syntax error.  There is an extra close parenthesis after img.setOnMouseClicked((MouseEvent event) -> {.  Remove it and the syntax error goes away.

But there's still a problem, and the poor formatting is hiding it again.

You probably didn't mean it put this code in the middle of your ChangeListener.  Let's put it nearer to where it's declared.

Now your MouseClicked event are captured and the code works as expected.
 
Knute Snortum
Sheriff
Posts: 4209
123
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here's what I mean by a SSCCE.  This program has nothing in it except what I want to test and what is needed to execute properly.

Since I could see that this code did what I expected, I knew the problem must be in your code somewhere.
 
Niti Kapoor
Ranch Hand
Posts: 251
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
 
Niti Kapoor
Ranch Hand
Posts: 251
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
and im running in netbeans so there is no error in my code
 
Niti Kapoor
Ranch Hand
Posts: 251
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
because for javafx eclipse have jar problem and netbeans doesnt need any jar
 
Niti Kapoor
Ranch Hand
Posts: 251
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
see the output image i have to set mouse clicked and key event on left webview which is rendered from right webview but im not able to do it help
refrence.image.png
[Thumbnail for refrence.image.png]
 
Niti Kapoor
Ranch Hand
Posts: 251
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
is it possible to do Action on image in webview or i have to add it in panel ?
 
Knute Snortum
Sheriff
Posts: 4209
123
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Niti Kapoor wrote:

Whenever you post that you get an error, also post the full error message.  But I'm guessing that the import for MouseEvent is still pointing to AWT, not JavaFX.
 
Knute Snortum
Sheriff
Posts: 4209
123
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Niti Kapoor wrote:and im running in netbeans so there is no error in my code

This is a runtime error, not a compile error (I'm guessing) so it doesn't matter which IDE you are using.
 
Knute Snortum
Sheriff
Posts: 4209
123
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Niti Kapoor wrote:because for javafx eclipse have jar problem and netbeans doesnt need any jar

It's fine that you're using NetBeans -- I'm sure it has similar formatting abilities.  Just for the record, Eclipse has a plugin that fixes the JavaFX jar access problems.
 
Niti Kapoor
Ranch Hand
Posts: 251
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
how we get to knwon + key is pressed
 
Niti Kapoor
Ranch Hand
Posts: 251
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


like this im getting error cannot find symbol
 
Tim Moores
Saloon Keeper
Posts: 3961
94
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Knute Snortum wrote:Whenever you post that you get an error, also post the full error message.


Also be sure to tell us in which line of code that error occurred (not just the line number, the actual line of code - with incomplete code the line number doesn't tell us anything).
 
Niti Kapoor
Ranch Hand
Posts: 251
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
why this is not working error
 
Niti Kapoor
Ranch Hand
Posts: 251
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ADD one is running but zoom out function not working and dragging and selecting the region also not working.
 
Niti Kapoor
Ranch Hand
Posts: 251
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
and 84th line is dragMeasure.x = mouseEvent.getSceneX() - position.x; this in code
 
Knute Snortum
Sheriff
Posts: 4209
123
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It's not obvious, but this are the important lines in the stack trace: So the error is in your position class, line 16.  You will find the variable y is already defined.

Note that all the classes in your project should be compilable before you execute the project.
 
Niti Kapoor
Ranch Hand
Posts: 251
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


this is working now instead of yes i have to zoom in and zoom out how can i do ?
 
Knute Snortum
Sheriff
Posts: 4209
123
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here is a SSCCE that illustrates zooming in and out of an image:
It has some "magic numbers" in it that I didn't take the time to put into constants, but you get the idea.
 
Niti Kapoor
Ranch Hand
Posts: 251
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thnks knute but i have solve the problem just by changing and setting it with webview like this

 
Niti Kapoor
Ranch Hand
Posts: 251
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
other than that knute how to crop by selecting on webview like zoomin and zoom out has done
 
Niti Kapoor
Ranch Hand
Posts: 251
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
  in this im getting error cannot find symbol: method addMouseMotionListener
 
Campbell Ritchie
Marshal
Posts: 56223
171
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Niti Kapoor wrote:. . . im getting error cannot find symbol: method addMouseMotionListener
Go through the documentation and find whichever class img is and see which methods it has.
 
Niti Kapoor
Ranch Hand
Posts: 251
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
img is webview object
 
Niti Kapoor
Ranch Hand
Posts: 251
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
it is nothing regarding motion mouse listener as im using mouse pressed also its also not working

 
Knute Snortum
Sheriff
Posts: 4209
123
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Niti Kapoor wrote:other than that knute how to crop by selecting on webview like zoomin and zoom out has done

I don't know about a WebView but with an ImageView you can set a ViewPort that is a piece of the image.  See setViewPort in ImageView.  My thinking is that you could listen for a mouse click and drag and get the coordinates from that.
 
Niti Kapoor
Ranch Hand
Posts: 251
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
how to listen to coordinates at the time of dragging?
 
Niti Kapoor
Ranch Hand
Posts: 251
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
as im trying to do like this im getting error?
[code=java] img.addMouseMotionListener(new MouseMotionAdapter() {

            Point start = new Point();

            public void mouseMoved(MouseEvent me) {
                start = me.getPoint();
                img.setText("Start Point: " + start);
                img.repaint();
            }

            public void mouseDragged(MouseEvent me) {
                Point end = me.getPoint();
                Rectangle captureRect = new Rectangle(start,
                        new Dimension(end.x-start.x, end.y-start.y));
                img.repaint();
                img.setText("Rectangle: " + captureRect);
            }
        });/code]  that symbol method add mousemotionlistener cannot find.
 
Tim Moores
Saloon Keeper
Posts: 3961
94
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You seem not to have read what Campbell said - neither WebView nor WebEngine has an addMouseMotionListener method.

By the way, it is very confusing to have both "Img" and "img" variables. you should name them so anyone reading the code -like the people here who are trying to help- can easily understand which variable is of which type.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!