- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我已经看到很多关于如何将标签面板的标签拖到另一个标签面板的方法,顺便说一下,我不明白这一点,我想它在某种程度上可能有用,但最自然的应用应该是重新排序同一个标签面板的标签,似乎没有人在互联网上谈论这个问题。
我想要几乎所有浏览器都实现的行为。
我已经尝试了一些方法,但它没有按照我的意愿进行。主要问题是 Tab
不能与 setOnDragDetected
、setOnDragOver
等方法绑定(bind)(出于某种原因,我逃避了方式 ...) 这导致算法过于复杂来弥补库的这一弱点。
所以这是我的代码,它没有做我想做的事情,因为(这只是一种解决方法)我只在放置完成后检测到“放置目标”,因此使其无用。是的,出于某种原因,在按住单击按钮的同时悬停对象不会导致 mouseover
事件 ...
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.Label;
import javafx.scene.control.Tab;
import javafx.scene.control.TabPane;
import javafx.scene.input.ClipboardContent;
import javafx.scene.input.Dragboard;
import javafx.scene.input.TransferMode;
import javafx.scene.paint.Color;
import javafx.scene.shape.Rectangle;
import javafx.stage.Stage;
public class DragAndDrop extends Application
{
public void start(Stage primaryStage)
{
TabPane tabPane = new TabPane();
final int[] target = {0};
for(int i = 0 ; i < 2 ; i ++)
{
Tab tab = new Tab();
tab.setClosable(false);
int index[] = {i};
tabPane.getTabs().add(tab);
Label label = new Label("Tab " + (i + 1), new Rectangle(16, 16, Color.TRANSPARENT));
tab.setGraphic(label);
tab.getGraphic().setOnMouseEntered(event ->
{
target[0] = index[0];
System.out.println("target : " + target[0]);
});
}
tabPane.setOnDragDetected(event ->
{
Dragboard dragboard = tabPane.startDragAndDrop(TransferMode.ANY);
ClipboardContent clipboardContent = new ClipboardContent();
clipboardContent.putString(Integer.toString(tabPane.getSelectionModel().getSelectedIndex()));
System.out.println("source : " + tabPane.getSelectionModel().getSelectedIndex());
dragboard.setContent(clipboardContent);
event.consume();
});
tabPane.setOnDragOver(event ->
{
if(event.getDragboard().hasString())
{
event.acceptTransferModes(TransferMode.COPY_OR_MOVE);
}
event.consume();
});
tabPane.setOnDragDropped(event ->
{
Dragboard dragboard = event.getDragboard();
boolean success = false;
if(dragboard.hasString())
{
System.out.println("from " + dragboard.getString() + " to " + target[0]);
success = true;
}
event.setDropCompleted(success);
event.consume();
});
tabPane.setOnDragDone(event ->
{
if(event.getTransferMode() == TransferMode.MOVE)
{
// tabPane.setText("");
}
event.consume();
});
primaryStage.setScene(new Scene(tabPane, 300, 200));
primaryStage.show();
}
public static void main(String[] args)
{
launch(args);
}
}
最佳答案
我认为解决这个问题的最简单方法是将拖动处理程序注册到与选项卡关联的图形,而不是注册到选项卡 Pane 。这使得管理选项卡的索引等变得更容易一些,并且您不依赖于鼠标拖动操作和选择之间的相互作用(您真的不知道它们将以什么顺序发生)。这种方法的缺点是您必须以某种方式确保每个选项卡都有一个图形(您可以使用标签作为图形来显示文本)。
这是一个通用的“选项卡 Pane 拖动支持”类。无论如何,这并不意味着生产质量,但基础似乎有效。这里的想法是您创建一个 DraggingTabPaneSupport
实例,并使用它为一个或多个 TabPane
添加拖动支持。然后可以在任何这些 Pane 内或之间拖动选项卡。如果您希望只能在每个选项卡 Pane 内拖动,请为每个选项卡 Pane 创建一个 DraggingTabPaneSupport
实例。
import java.util.concurrent.atomic.AtomicLong;
import javafx.collections.ListChangeListener.Change;
import javafx.scene.Node;
import javafx.scene.control.Label;
import javafx.scene.control.Tab;
import javafx.scene.control.TabPane;
import javafx.scene.input.ClipboardContent;
import javafx.scene.input.Dragboard;
import javafx.scene.input.TransferMode;
public class DraggingTabPaneSupport {
private Tab currentDraggingTab ;
private static final AtomicLong idGenerator = new AtomicLong();
private final String draggingID = "DraggingTabPaneSupport-"+idGenerator.incrementAndGet() ;
public void addSupport(TabPane tabPane) {
tabPane.getTabs().forEach(this::addDragHandlers);
tabPane.getTabs().addListener((Change<? extends Tab> c) -> {
while (c.next()) {
if (c.wasAdded()) {
c.getAddedSubList().forEach(this::addDragHandlers);
}
if (c.wasRemoved()) {
c.getRemoved().forEach(this::removeDragHandlers);
}
}
});
// if we drag onto a tab pane (but not onto the tab graphic), add the tab to the end of the list of tabs:
tabPane.setOnDragOver(e -> {
if (draggingID.equals(e.getDragboard().getString()) &&
currentDraggingTab != null &&
currentDraggingTab.getTabPane() != tabPane) {
e.acceptTransferModes(TransferMode.MOVE);
}
});
tabPane.setOnDragDropped(e -> {
if (draggingID.equals(e.getDragboard().getString()) &&
currentDraggingTab != null &&
currentDraggingTab.getTabPane() != tabPane) {
currentDraggingTab.getTabPane().getTabs().remove(currentDraggingTab);
tabPane.getTabs().add(currentDraggingTab);
currentDraggingTab.getTabPane().getSelectionModel().select(currentDraggingTab);
}
});
}
private void addDragHandlers(Tab tab) {
// move text to label graphic:
if (tab.getText() != null && ! tab.getText().isEmpty()) {
Label label = new Label(tab.getText(), tab.getGraphic());
tab.setText(null);
tab.setGraphic(label);
}
Node graphic = tab.getGraphic();
graphic.setOnDragDetected(e -> {
Dragboard dragboard = graphic.startDragAndDrop(TransferMode.MOVE);
ClipboardContent content = new ClipboardContent();
// dragboard must have some content, but we need it to be a Tab, which isn't supported
// So we store it in a local variable and just put arbitrary content in the dragbaord:
content.putString(draggingID);
dragboard.setContent(content);
dragboard.setDragView(graphic.snapshot(null, null));
currentDraggingTab = tab ;
});
graphic.setOnDragOver(e -> {
if (draggingID.equals(e.getDragboard().getString()) &&
currentDraggingTab != null &&
currentDraggingTab.getGraphic() != graphic) {
e.acceptTransferModes(TransferMode.MOVE);
}
});
graphic.setOnDragDropped(e -> {
if (draggingID.equals(e.getDragboard().getString()) &&
currentDraggingTab != null &&
currentDraggingTab.getGraphic() != graphic) {
int index = tab.getTabPane().getTabs().indexOf(tab) ;
currentDraggingTab.getTabPane().getTabs().remove(currentDraggingTab);
tab.getTabPane().getTabs().add(index, currentDraggingTab);
currentDraggingTab.getTabPane().getSelectionModel().select(currentDraggingTab);
}
});
graphic.setOnDragDone(e -> currentDraggingTab = null);
}
private void removeDragHandlers(Tab tab) {
tab.getGraphic().setOnDragDetected(null);
tab.getGraphic().setOnDragOver(null);
tab.getGraphic().setOnDragDropped(null);
tab.getGraphic().setOnDragDone(null);
}
}
这是一个使用它的简单示例。在此示例中,我创建了三个选项卡 Pane 。我为前两个 Pane 创建了一个拖动支持实例,因此可以在这两个 Pane 之间拖动选项卡,并为第三个 Pane 创建了第二个拖动支持实例,因此在第三个 Pane 中选项卡可以通过拖动重新排序但不能拖动到其他两个 Pane 。
import java.util.Random;
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.Tab;
import javafx.scene.control.TabPane;
import javafx.scene.layout.Region;
import javafx.scene.layout.VBox;
import javafx.scene.paint.Color;
import javafx.scene.shape.Rectangle;
import javafx.stage.Stage;
public class DraggingTabPaneExample extends Application {
private final Random rng = new Random();
@Override
public void start(Stage primaryStage) {
TabPane[] panes = new TabPane[] {new TabPane(), new TabPane(), new TabPane() };
VBox root = new VBox(10, panes);
for (int i = 1 ; i <= 15; i++) {
Tab tab = new Tab("Tab "+i);
tab.setGraphic(new Rectangle(16, 16, randomColor()));
Region region = new Region();
region.setMinSize(100, 150);
tab.setContent(region);
panes[(i-1) % panes.length].getTabs().add(tab);
}
DraggingTabPaneSupport support1 = new DraggingTabPaneSupport();
support1.addSupport(panes[0]);
support1.addSupport(panes[1]);
DraggingTabPaneSupport support2 = new DraggingTabPaneSupport();
support2.addSupport(panes[2]);
Scene scene = new Scene(root, 600, 600);
primaryStage.setScene(scene);
primaryStage.show();
}
private Color randomColor() {
return Color.rgb(rng.nextInt(256), rng.nextInt(256), rng.nextInt(256));
}
public static void main(String[] args) {
launch(args);
}
}
初始截图:
您还可以在前两个 Pane 之间拖动(但不能在第三个 Pane 和任何其他 Pane 之间拖动):
关于java - 如何拖放同一标签面板的标签?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41473987/
我在这里有一个我想要做的事情的例子:http://jsbin.com/OwoYAlEQ/1/edit 这是我的 HTML: person one person two person three per
我想知道是否有人知道是否有一个预先制定的解决方案:我在 ASP.net 网站上有一个列表,我希望用户能够通过拖放对列表进行重新排序。此外,我希望有第二个列表,用户可以将第一个列表中的项目拖到其中。 到
我想知道是否有人知道是否有一个预先制定的解决方案:我在 ASP.net 网站上有一个列表,我希望用户能够通过拖放对列表进行重新排序。此外,我希望有第二个列表,用户可以将第一个列表中的项目拖到其中。 到
我在我的 Web 应用程序中使用 Ajax ControlToolkit 中的 ModalPopupExtender。我将其 Drag 属性设置为 true,但是当我拖动弹出面板并将其放到新位置时,它
所以,基于this answer ,我有一组可以拖放并卡入到位的 div。唯一的问题是,可拖动的 div 具有不同的高度,我需要它们始终捕捉到目标的底部,而不是顶部。 您可以在this JsFiddl
我一直在使用 Bea 的解决方案 here一段时间后发现它非常有帮助。现在我遇到的问题是,当我将项目拖放到另一个 ListView 控件中或拖放到另一个 ListView 控件中,并且我想在拖动“期间
我试图在使用 QTreeWidget.setItemWidget() 重新父级(拖放)后将小部件放入 QTreeWidgetItem 但是,如果编译以下代码,结果是 QTreeWidgetItem 内
这是场景,我使用的是prototype和scriptaculous,但我相信jquery也会有同样的问题。我在相对定位的 div 中有一个可拖动图像的列表。问题是我无法将图像拖出父 div...好吧.
我正在使用一个普通(Bootstrap)表,我想在其中包含可排序的行。我正在使用 Angular CDK (DragDropModule) 来实现排序/排序。但是,当拖动行时,它会扭曲宽度,因为 cd
我正在尝试在我的 UICollectionView 中实现拖放机制,这与在快捷方式应用程序中重新排序快捷方式的组件非常相似。 截至目前,行为是当您开始拖动时,会留下一个透明的单元格 View ,而另一
我有以下 Jquery UI 拖放 jsfiddle https://jsfiddle.net/zoojsfiddle/ud96jdcp/ 具有
我希望创建一个基于网络的“公告板”,可以这么说,用户可以在其中创建/删除/拖放“图钉”,而不允许重叠“图钉”。 这是一个图表,应该有助于说明我正在尝试创建的内容: 'pins' 可能已创建双击;他们会
我是一名优秀的程序员,十分优秀!