custom controls - How to get location of mouse in JavaFX? -


i beginner in java(fx).
how mouse location in x , y in javafx? tried using awt's mouseinfo(also imported it), it's not working. saw code in ensembles(that dragging ball-window in "advanced stage", that's need do, drag undecorated javafx stage), doesn't work. using fxml controller, , guess that's main problem. should switch single-file simple javafx? know fxml better laying out ui, can't many of such codes work. or need other sort of code controller? please give proper codes comments wherever possible.
if need bit of code inspect, feel free ask.

there few items in question - i'll tackle them 1 @ time.

how mouse location in x , y in javafx?

add mouse event handler appropriate javafx component want track mouse location in. javafx mouse event report multiple different kinds of co-ordinates. x , y co-ordinates relative top left corner of node location being monitored. scenex , sceney co-ordinates relative scene's top left 0,0 co-ordinates. screenx , screeny co-ordinates relative top left 0,0 co-ordinates of current screen.

these co-ordinates documented in mouseevent documentation. there information in understanding co-ordinate systems in node , scene documentation.

mouselocationmonitor

import javafx.application.application; import static javafx.application.application.launch; import javafx.event.*; import javafx.scene.*; import javafx.scene.control.*; import javafx.scene.input.mouseevent; import javafx.scene.layout.vbox; import javafx.stage.*;  public class mouselocationreporter extends application {   private static final string outside_text = "outside label";    public static void main(string[] args) { launch(args); }    @override public void start(final stage stage) {     final label reporter = new label(outside_text);     label monitored = createmonitoredlabel(reporter);      vbox layout = new vbox(10);     layout.setstyle("-fx-background-color: cornsilk; -fx-padding: 10px;");     layout.getchildren().setall(       monitored,       reporter     );     layout.setprefwidth(500);      stage.setscene(       new scene(layout)     );      stage.show();   }    private label createmonitoredlabel(final label reporter) {     final label monitored = new label("mouse location monitor");      monitored.setstyle("-fx-background-color: forestgreen; -fx-text-fill: white; -fx-font-size: 20px;");      monitored.setonmousemoved(new eventhandler<mouseevent>() {       @override public void handle(mouseevent event) {         string msg =           "(x: "       + event.getx()      + ", y: "       + event.gety()       + ") -- " +           "(scenex: "  + event.getscenex() + ", sceney: "  + event.getsceney()  + ") -- " +           "(screenx: " + event.getscreenx()+ ", screeny: " + event.getscreeny() + ")";          reporter.settext(msg);       }     });      monitored.setonmouseexited(new eventhandler<mouseevent>() {       @override public void handle(mouseevent event) {         reporter.settext(outside_text);       }     });      return monitored;   } } 

i tried using awt's mouseinfo(also imported it), it's not working.

don't this. mixing different graphical toolkits (for example swing/awt , javafx) advanced topic. in general, if writing javafx application, avoid importing java.awt namespace , javax.swing namespace. need use if have large, existing swing based application or framework need inter-operate javafx application. in case, don't have situation.

i saw code in ensembles(that dragging ball-window in "advanced stage", that's need do, drag undecorated javafx stage), doesn't work.

i tried ensemble advanced stage sample , dragging stage around worked me.

another sample dragging undecorated stage in javafx in answer how draw clock javafx 2? has associated sample code. method used make undecorated stage draggable clock sample is:

/** makes stage draggable using given node */ public static void makedraggable(final stage stage, final node bynode) {   final delta dragdelta = new delta();   bynode.setonmousepressed(new eventhandler<mouseevent>() {     @override public void handle(mouseevent mouseevent) {       // record delta distance drag , drop operation.       dragdelta.x = stage.getx() - mouseevent.getscreenx();       dragdelta.y = stage.gety() - mouseevent.getscreeny();       bynode.setcursor(cursor.move);     }   });   bynode.setonmousereleased(new eventhandler<mouseevent>() {     @override public void handle(mouseevent mouseevent) {       bynode.setcursor(cursor.hand);     }   });   bynode.setonmousedragged(new eventhandler<mouseevent>() {     @override public void handle(mouseevent mouseevent) {       stage.setx(mouseevent.getscreenx() + dragdelta.x);       stage.sety(mouseevent.getscreeny() + dragdelta.y);     }   });   bynode.setonmouseentered(new eventhandler<mouseevent>() {     @override public void handle(mouseevent mouseevent) {       if (!mouseevent.isprimarybuttondown()) {         bynode.setcursor(cursor.hand);       }     }   });   bynode.setonmouseexited(new eventhandler<mouseevent>() {     @override public void handle(mouseevent mouseevent) {       if (!mouseevent.isprimarybuttondown()) {         bynode.setcursor(cursor.default);       }     }   }); } 

i using fxml controller, , guess that's main problem. should switch single-file simple javafx? know fxml better laying out ui, can't many of such codes work.

lack of understanding , familiarity underlying javafx apis main problem rather use of fxml. additional complexity fxml implies lighter documentation , samples on web may contributing hardships. if use of fxml making difficult understand how javafx functions work, advise stop using fxml now. code logic hand using java apis , refer oracle javafx tutorials , ensemble sample code when encounter things difficult you.

once comfortable coding directly javafx api, switch using fxml larger projects contain many gui elements. fxml elements , attributes built upon reflection of standard javafx apis. so, if understand core javafx apis, understand fxml.

please not post follow comments answer (as answer long enough is). if have new questions, create new question (one question per question).


Comments

Popular posts from this blog

SPSS keyboard combination alters encoding -

Add new record to the table by click on the button in Microsoft Access -

javascript - jQuery .height() return 0 when visible but non-0 when hidden -