我尝试通过拖放连接到列表视图,所以当我从视图1中获取列表单元格时 并将ist放到视图2中的列表单元格上,在这些twit之间应该画一条线以反映连接。
我试图完成的任务应该是这样的
到目前为止,我设法为列表添加了基本的拖放支持,但是线条部分似乎很棘手。
当我开始在列表视图1中拖动时,我保存鼠标事件的x / y坐标 通过使用此代码,但它不适用于正确的拖放坐标
testdevice_list.setCellFactory(new Callback<ListView<TestDevice>, ListCell<TestDevice>>() {
@Override
public ListCell call(ListView<TestDevice> param) {
TestDeviceCell cell = new TestDeviceCell();
cell.setOnDragDetected((MouseEvent event) -> {
/*first try was to use event.getSceneX() but this didn´t work
now by using a method inside the custom cell seems to work better
to find the correct start point to draw line
*/
startX = cell.getStartX();
startY = cell.getStartY();
/* drag was detected, start drag-and-drop gesture*/
System.out.println("onDragDetected");
Dragboard db = testdevice_list.startDragAndDrop(TransferMode.LINK);
/* put a string on dragboard */
ClipboardContent content = new ClipboardContent();
content.putString(testdevice_list.getSelectionModel().getSelectedItem().getTester_typ());
db.setContent(content);
event.consume();
});
cell.setOnDragDone(new EventHandler<DragEvent>() {
public void handle(DragEvent event) {
/* the drag-and-drop gesture ended */
System.out.println("onDragDone");
/* if the data was successfully moved, clear it */
if (event.getTransferMode() == TransferMode.LINK) {
stopX = ((PruefvorschriftListCell) event.getSource()).getStopX();
stopY = ((PruefvorschriftListCell) event.getSource()).getStopY();
//stopX = ((TestDeviceCell)event.getSource()).getLayoutX() - ((TestDeviceCell)event.getSource()).getHeight() / 2;
//stopY = ((TestDeviceCell)event.getSource()).getLayoutY() - ((TestDeviceCell)event.getSource()).getWidth()/ 2;
//stopX = event.getSceneX();
//stopY = event.getSceneY();
addConnection();
//testdevice_list.getItems().remove(testdevice_list.getSelectionModel().getSelectedIndex());
}
event.consume();
}
});
return cell;
}
});
addConnection方法只是尝试绘制一条线
private void addConnection(){
System.out.println("Drawing line");
Path path = new Path();
MoveTo moveTo = new MoveTo();
moveTo.setAbsolute(true);
moveTo.setX(startX);
moveTo.setY(startY);
LineTo lineTo = new LineTo();
lineTo.setAbsolute(false);
lineTo.setX(stopX);
lineTo.setY(stopY);
path.getElements().add(moveTo);
path.getElements().add(lineTo);
path.setStrokeWidth(5);
path.setStroke(Color.BLACK);
drawRegion.getChildren().add(path);
}
以下是我的问题:
为什么它在错误的地方开始画线看图片
我如何将线条精确地贴在绿点上(从点到点)?
编辑: 这是TestDeviceCell的代码(只是重要部分):
import javafx.scene.control.Label;
import javafx.scene.control.ListCell;
import javafx.scene.input.MouseEvent;
import javafx.scene.layout.AnchorPane;
import javafx.scene.layout.GridPane;
import javafx.scene.paint.Color;
import javafx.scene.shape.Circle;
import protokollTool.model.TestDevice;
public class TestDeviceCell extends ListCell<TestDevice> {
private static final String DEVICE_LIST_SERIAL_CLASS = "device-list-serial";
private static final String DEVICE_LIST_ICON_CLASS = "device-list-icon";
private static final String DEVICE_LIST_TYP_CLASS = "device-list-typ";
private static final String DEVICE_LIST_GRID_CLASS = "device-list-grid";
private static final String FONT_AWESOME = "FontAwesome";
private GridPane grid = new GridPane();
private Label serial = new Label();
private Label typ = new Label();
private AnchorPane icon = new AnchorPane();
private Circle circ = new Circle();
private double startX =0;
private double startY =0;
public TestDeviceCell() {
configureGrid();
configureIcon();
configureCycle();
configureTyp();
configureSerial();
addControlsToGrid();
}
private void configureGrid() {
grid.getStyleClass().add(DEVICE_LIST_GRID_CLASS);
}
private void configureIcon() {
icon.setMinHeight(40);
icon.setMinWidth(40);
icon.getStyleClass().add(DEVICE_LIST_ICON_CLASS);
}
private void configureCycle() {
circ.setRadius(10);
circ.setFill(Color.GREEN);
circ.setOnDragDetected((MouseEvent event) -> {
setStartX(circ.getLayoutX());
setStartY(circ.getLayoutY());
System.out.println("X-Start:"+getStartX()+"-Y-Start:"+getStartY());
});
}
private void configureSerial() {
serial.getStyleClass().add(DEVICE_LIST_SERIAL_CLASS);
}
private void configureTyp() {
typ.getStyleClass().add(DEVICE_LIST_TYP_CLASS);
}
private void addControlsToGrid() {
grid.add(icon, 0, 0, 1, 2);
grid.add(serial, 1, 0);
grid.add(typ, 1, 1);
grid.add(circ, 1, 2);
}
@Override
public void updateItem(TestDevice dev, boolean empty) {
super.updateItem(dev, empty);
if (empty) {
clearContent();
} else {
addContent(dev);
}
}
private void clearContent() {
setText(null);
setGraphic(null);
}
private void addContent(TestDevice dev) {
setText(null);
serial.setText(dev.getTester_name());
typ.setText(dev.getTester_typ());
setStyleClassDependingOnTyp(dev);
setGraphic(grid);
}
private void setStyleClassDependingOnTyp(TestDevice dev) {
icon.getStyleClass().remove(dev.getTester_typ());
icon.getStyleClass().add(dev.getTester_typ());
}
public double getStartX() {
return startX;
}
public void setStartX(double startX) {
this.startX = startX;
}
public double getStartY() {
return startY;
}
public void setStartY(double startY) {
this.startY = startY;
}
}
另一个ListCell类(PruefvorschriftListCell)与TestDeviceCell相同,除了 以下功能:
private void configureCycle() {
circ.setRadius(10);
circ.setFill(Color.GREEN);
circ.setOnDragDropped((DragEvent event) -> {
setStopX(event.getSceneX());
setStopY(event.getSceneY());
System.out.println("X-Stop:"+getStopX()+"-Y-Stop:"+getStopY());
});
}
THX 英格