java 8 - How to draw a line with JavaFX -
i have below code , try create 4 labels 2 of them in 2 vboxes. want connect these vboxes line.
i not able find correct x,y values place line in correct position.
@override public void start(stage primarystage) { group root = new group(); scene scene = new scene(root); label label1 = new label("info \n 1"); label1.setstyle("-fx-border-color: black;-fx-padding: 10px;"); label1.setwraptext(true); label label2 = new label("info 2222222222222"); label2.setstyle("-fx-border-color: black;-fx-padding: 10px;"); label2.setwraptext(true); vbox vbox1 = new vbox(5); vbox1.setmaxwidth(50); vbox1.getchildren().add(label1); vbox1.getchildren().add(label2); label label3 = new label("info \n 3"); label3.setstyle("-fx-border-color: black;-fx-padding: 10px;"); label3.setwraptext(true); label label4 = new label("info 444444444444444444"); label4.setstyle("-fx-border-color: black;-fx-padding: 10px;"); label4.setwraptext(true); vbox vbox2 = new vbox(5); vbox2.setmaxwidth(50); vbox2.getchildren().add(label3); vbox2.getchildren().add(label4); hbox hbox = new hbox(100); hbox.getchildren().addall(vbox1, vbox2); bounds bounds = label1.getboundsinlocal(); double startx = bounds.getmaxx(); double starty = (bounds.getmaxy() - bounds.getminy()) /2; line line = new line(startx, starty, startx+100, starty); line.setstrokewidth(5); line.setstroke(color.black); pane stack = new pane(); stack.getchildren().addall(hbox, line); root.getchildren().addall(stack); primarystage.setscene(scene); primarystage.show(); 
layout bounds not computed until first pulse (i.e. until layout performed). best way fix bindings.
additionally, using local bounds of label, instead of bounds relative container line in. need make appropriate transformation.
i'm not clear trying connect, example connect label1 label3:
import javafx.application.application; import javafx.beans.binding.bindings; import javafx.beans.binding.doublebinding; import javafx.beans.binding.objectbinding; import javafx.geometry.bounds; import javafx.scene.group; import javafx.scene.scene; import javafx.scene.control.label; import javafx.scene.layout.hbox; import javafx.scene.layout.pane; import javafx.scene.layout.vbox; import javafx.scene.paint.color; import javafx.scene.shape.line; import javafx.stage.stage; public class connectedvboxes extends application { @override public void start(stage primarystage) { group root = new group(); scene scene = new scene(root); label label1 = new label("info \n 1"); label1.setstyle("-fx-border-color: black;-fx-padding: 10px;"); label1.setwraptext(true); label label2 = new label("info 2222222222222"); label2.setstyle("-fx-border-color: black;-fx-padding: 10px;"); label2.setwraptext(true); vbox vbox1 = new vbox(5); vbox1.setmaxwidth(50); vbox1.getchildren().add(label1); vbox1.getchildren().add(label2); label label3 = new label("info \n 3"); label3.setstyle("-fx-border-color: black;-fx-padding: 10px;"); label3.setwraptext(true); label label4 = new label("info 444444444444444444"); label4.setstyle("-fx-border-color: black;-fx-padding: 10px;"); label4.setwraptext(true); vbox vbox2 = new vbox(5); vbox2.setmaxwidth(50); vbox2.getchildren().add(label3); vbox2.getchildren().add(label4); hbox hbox = new hbox(100); hbox.getchildren().addall(vbox1, vbox2); line line = new line(); line.setstrokewidth(5); line.setstroke(color.black); pane stack = new pane(); stack.getchildren().addall(hbox, line); objectbinding<bounds> label1instack = bindings.createobjectbinding(() -> { bounds label1inscene = label1.localtoscene(label1.getboundsinlocal()); return stack.scenetolocal(label1inscene); }, label1.boundsinlocalproperty(), label1.localtoscenetransformproperty(), stack.localtoscenetransformproperty()); objectbinding<bounds> label3instack = bindings.createobjectbinding(() -> { bounds label3inscene = label3.localtoscene(label3.getboundsinlocal()); return stack.scenetolocal(label3inscene); }, label3.boundsinlocalproperty(), label3.localtoscenetransformproperty(), stack.localtoscenetransformproperty()); doublebinding startx = bindings.createdoublebinding(() -> label1instack.get().getmaxx(), label1instack); doublebinding starty = bindings.createdoublebinding(() -> { bounds b = label1instack.get(); return b.getminy() + b.getheight() / 2 ; }, label1instack); doublebinding endx = bindings.createdoublebinding(() -> label3instack.get().getminx(), label3instack); doublebinding endy = bindings.createdoublebinding(() -> { bounds b = label3instack.get(); return b.getminy() + b.getheight() / 2 ; }, label3instack); line.startxproperty().bind(startx); line.startyproperty().bind(starty); line.endxproperty().bind(endx); line.endyproperty().bind(endy); root.getchildren().addall(stack); primarystage.setscene(scene); primarystage.show(); } public static void main(string[] args) { launch(args); } } with technique, if labels move when user resizes window (or other reasons), line stays connecting labels.
Comments
Post a Comment