java - JavaFx Popup Window With MouseClick -
this question exact duplicate of:
is there way make popup window appear using onmouseclick , if how go doing this? need add code make new window appear when click on building in map. if seems vague might ask , ill try explain further. cheers!
package application; import java.net.url; import java.util.collection; import java.util.resourcebundle; import javafx.beans.invalidationlistener; import javafx.beans.observable; import javafx.beans.property.doubleproperty; import javafx.event.actionevent; import javafx.event.eventhandler; import javafx.fxml.fxml; import javafx.fxml.initializable; import javafx.scene.node; import javafx.scene.control.scrollbar; import javafx.scene.control.slider; import javafx.scene.image.imageview; import javafx.scene.input.mouseevent; import javafx.stage.popup; import javafx.stage.stage; public class mapcontroller implements initializable { //t&l protected static double tandltop_x = 154.0; protected static double tandltop_y = 283.0; protected static double tandlbottom_x = 215.0; protected static double tandlbottom_y = 298.0; //bussiness building protected static double buistop_x = 313.0; protected static double buistop_y = 238.0; protected static double buisbottom_x = 354.0; protected static double buisbottom_y = 260.0; //it building protected static double ittop_x = 374.0; protected static double ittop_y = 252.0; protected static double itbottom_x = 387.0; protected static double itbottom_y = 268.0; //nursing building protected static double nursetop_x = 425.0; protected static double nursetop_y = 259.0; protected static double nursebottom_x = 452.0; protected static double nursebottom_y = 278.0; //audiotorium protected static double audtop_x = 247.0; protected static double audtop_y = 211.0; protected static double audbottom_x = 297.0; protected static double audbottom_y = 254.0; //libary protected static double libtop_x = 328.0; protected static double libtop_y = 278.0; protected static double libbottom_x = 382.0; protected static double libbottom_y = 291.0; @fxml public imageview map; @fxml public slider zoomslider; @fxml public scrollbar scrollpane; doubleproperty zoomproperty; @override public void initialize(url arg0, resourcebundle arg1) { zoomproperty = zoomslider.valueproperty(); // center scroll pane scrollpane.setvalue((scrollpane.getmax() - scrollpane.getmin()) / 2); scrollpane.setvalue((scrollpane.getmax() - scrollpane.getmin()) / 2); zoomproperty.addlistener(new invalidationlistener() { @override public void invalidated(observable observable) { double prevhvalue = scrollpane.getvalue(); double prevvvalue = scrollpane.getvalue(); map.setscalex(zoomproperty.get()); map.setscaley(zoomproperty.get()); scrollpane.setvalue(prevhvalue); scrollpane.setvalue(prevvvalue); } }); } private boolean isintandl(double x, double y) { return x > tandltop_x && x < tandlbottom_x && y > tandltop_y && y < tandlbottom_y; } private boolean isinbuisness(double x, double y) { return x > buistop_x && x < buisbottom_x && y > buistop_y && y < buisbottom_y; } private boolean isit(double x, double y) { return x > ittop_x && x < itbottom_x && y > ittop_y && y < itbottom_y; } private boolean isinnursing(double x, double y) { return x > nursetop_x && x < nursebottom_x && y > nursetop_y && y < nursebottom_y; } private boolean isinaudi(double x, double y) { return x > audtop_x && x < audbottom_x && y > audtop_y && y < audbottom_y; } private boolean isinlibary(double x, double y) { return x > libtop_x && x < libbottom_x && y > libtop_y && y < libbottom_y; } public void hovermap(mouseevent e) { double x = e.getx(); double y = e.gety(); system.out.println(x +"," + y); if (isintandl(x, y)) { system.out.println("in tourism , leisure building"); } if (isinbuisness(x, y)) { system.out.println("in business building"); } if (isit(x, y)) { system.out.println("in building"); } if (isinnursing(x, y)) { system.out.println("in nursing building"); } if (isinaudi(x, y)) { system.out.println("in auditorium building"); } if (isinlibary(x, y)) { system.out.println("in libary building"); } }} package application; import javafx.application.application; import javafx.fxml.fxmlloader; import javafx.stage.popup; import javafx.stage.stage; import javafx.scene.parent; import javafx.scene.scene; public class main extends application { public static stage stage; public static scene slideshow; public static scene map; public static scene building; @override public void start(stage primarystage) { try { stage = primarystage; //load scene building parent root1 = fxmlloader.load(getclass().getresource("map.fxml")); map = new scene(root1, 700, 500); root1.getstylesheets().add(getclass().getresource("application.css").toexternalform()); parent root2 = fxmlloader.load(getclass().getresource("building.fxml")); building = new scene(root2, 600, 600); root2.getstylesheets().add(getclass().getresource("application.css").toexternalform()); parent root3 = fxmlloader.load(getclass().getresource("slideshow.fxml")); slideshow = new scene(root3, 600, 600); root3.getstylesheets().add(getclass().getresource("application.css").toexternalform()); primarystage.settitle("popup example"); final popup popup = new popup(); popup.setx(300); popup.sety(200); setmap(); primarystage.centeronscreen(); primarystage.show(); } catch(exception e) { e.printstacktrace(); } } public static void setmap() { stage.setscene(map); } public static void setbuilding() { stage.setscene(building); } public static void setslideshow() { stage.setscene(slideshow); } public static void main(string[] args) { launch(args); } }
there few various answers this. i'll give 1 example creates new child stages user clicks in main window. need adjustments purposes, might perhaps provide directional example use gain further insight task.
this sample uses plain stages rather popups or popupwindows, can adjust implementation use window type wish required.
with example image below, user has clicked on background of main window 3 times bring 3 separate pop-up windows @ different locations.
import javafx.application.application; import javafx.scene.scene; import javafx.scene.input.mouseevent; import javafx.scene.layout.pane; import javafx.stage.stage; import javafx.stage.stagestyle; public class windowdisplay extends application { private stage primarystage; @override public void start(stage stage) throws exception { primarystage = stage; pane layout = new pane(); layout.setprefsize(100, 100); layout.setonmouseclicked(this::shownewwindow); stage.setscene(new scene(layout)); stage.show(); } private void shownewwindow(mouseevent event) { stage stage = new stage(stagestyle.undecorated); stage.initowner(primarystage); stage.setx(event.getscreenx()); stage.sety(event.getscreeny()); pane layout = new pane(); layout.setstyle("-fx-background-color: paleturquoise;"); layout.setprefsize(40, 40); layout.setonmouseclicked(e -> stage.close()); stage.setscene(new scene(layout)); stage.show(); } public static void main(string[] args) { launch(args); } }
Comments
Post a Comment