如何获取 JavaFX 中 ListView 中项目的位置?

2023-12-26

如果我在 JavaFX 中创建一个 ListView,如下所示:

ObservableList<String> elements = FXCollections.observableArrayList("John", "Doe");
ListView<String> lView = new ListView<String>(elements);

我想要做的是从 ListView 中的一行末尾开始画一条线,比如从“John”开始

为此,我需要“John”行的位置(x,y)。可以获取位置吗?

Update

这是我使用 Swing 和 Piccolo2D 获得的示例界面。然而,使用该库是痛苦的。我想知道我是否可以在 JavaFX 中做同样的事情


这是可能的,但可能并不像您希望的那么简单。为了确定特定的布局坐标Cell在一个ListView (or TableView/TreeView)您需要有权访问该特定的Cell目的。最好的方法(也许是 JavaFX 2.2 中唯一的方法)是为容器提供自定义的Cell and CellFactory暴露了每个Cell。你如何揭露Cell取决于你划清界限的触发因素是什么。

根据您的插图,您将需要访问每个单元格ListViews 已填充。你可以用List<ListCell<String>>领域中的CellFactory。我会在这里提到一个警告ListCells. The ListViewSkin将重复使用Cell尽可能。这意味着,如果您要尝试填充并连接一个最终滚动的列表,那么将行保持在正确的位置将会更加困难。我建议尝试确保所有列表项都适合屏幕。

下面是一个示例,注释中包含一些注释。请注意,获取正确的坐标来绘制您的Line可能需要计算 SceneGraph 的偏移量,但我在本例中没有这样做。

package listviewcellposition;

import java.util.ArrayList;
import java.util.List;
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.*;
import javafx.scene.layout.*;
import javafx.scene.paint.Color;
import javafx.scene.shape.Line;
import javafx.stage.Stage;
import javafx.util.Callback;

public class ListViewCellPosition extends Application {

    // CustomCellFactory for creating CustomCells
    public class CustomCellFactory implements
            Callback<ListView<String>, ListCell<String>> {

        List<ListCell<String>> allCells = new ArrayList<>();

        @Override
        public ListCell<String> call(final ListView<String> p) {
            final CustomCell cell = new CustomCell();
            allCells.add(cell);
            return cell;
        }

        public List<ListCell<String>> getAllCells() {
            return allCells;
        }
    }

    // CustomCell is where the exposure occurs. Here, it's based on the
    // Cell being selected in the ListView. You could choose a different
    // trigger here but you'll need to explore.
    public class CustomCell extends ListCell<String> {
        // General display stuff
        @Override
        protected void updateItem(String item, boolean empty) {
            super.updateItem(item, empty);
            if (empty) {
                setText(null);
                setGraphic(null);
            } else {
                setText(item == null ? "" : item);
                setGraphic(null);
            }
        }
    }

    @Override
    public void start(Stage primaryStage) {
        // This pane will contain the lines after they are created.
        // I set it into an AnchorPane to avoid having to deal with 
        // resizing.
        Pane linePane = new Pane();
        AnchorPane pane = new AnchorPane();
        pane.setPrefSize(100, 250);
        AnchorPane.setBottomAnchor(linePane, 0.0);
        AnchorPane.setLeftAnchor(linePane, 0.0);
        AnchorPane.setRightAnchor(linePane, 0.0);
        AnchorPane.setTopAnchor(linePane, 0.0);
        pane.getChildren().add(linePane);

        ListView<String> lView = new ListView<>();
        lView.setPrefSize(100, 250);
        CustomCellFactory lCellFactory = new CustomCellFactory();
        lView.setCellFactory(lCellFactory);

        ListView<String> rView = new ListView<>();
        rView.setPrefSize(100, 250);
        CustomCellFactory rCellFactory = new CustomCellFactory();
        rView.setCellFactory(rCellFactory);

        lView.getItems().addAll("Bill", "Doctor", "Steve", "Joanne");
        rView.getItems().addAll("Seuss", "Rowling", "King", "Shakespeare");

        HBox root = new HBox();
        root.getChildren().addAll(lView, pane, rView);

        Scene scene = new Scene(root, 300, 250);
        primaryStage.setScene(scene);
        primaryStage.show();

        connectCells(lCellFactory, "Bill", rCellFactory, "Shakespeare", linePane);
        connectCells(lCellFactory, "Doctor", rCellFactory, "Seuss", linePane);
        connectCells(lCellFactory, "Steve", rCellFactory, "King", linePane);
        connectCells(lCellFactory, "Joanne", rCellFactory, "Rowling", linePane);
    }

    // Looks up the ListCell<> for each String and creates a Line
    // with the coordinates from each Cell. The calculation is very 
    // contrived because I know that all the components have the same 
    // x-coordinate. You'll need more complicated calculations if your
    // containers are not aligned this way.
    private void connectCells(CustomCellFactory lCellFactory, String lVal,
            CustomCellFactory rCellFactory, String rVal, Pane linePane) {

        List<ListCell<String>> lList = lCellFactory.getAllCells();
        ListCell<String> lCell = null;

        for (ListCell<String> lc : lList) {
            if (lc.getItem() != null && lc.getItem().equals(lVal)) {
                lCell = lc;
                break;
            }
        }

        List<ListCell<String>> rList = rCellFactory.getAllCells();
        ListCell<String> rCell = null;

        for (ListCell<String> rc : rList) {
            if (rc.getItem() != null && rc.getItem().equals(rVal)) {
                rCell = rc;
                break;
            }
        }

        if (lCell != null && rCell != null) {
            double startY = lCell.getLayoutY() +
                    (lCell.getBoundsInLocal().getHeight() / 2);
            double endY = rCell.getLayoutY() +
                    (rCell.getBoundsInLocal().getHeight() / 2);

            Line line = new Line(0, startY, 
                    linePane.getBoundsInParent().getWidth(), endY);
            line.setStrokeWidth(2);
            line.setStroke(Color.BLACK);

            linePane.getChildren().add(line);
        }
    }   

    public static void main(String[] args) {
        launch(args);
    }
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

如何获取 JavaFX 中 ListView 中项目的位置? 的相关文章

  • JavaFX:将像素写入 PixelWriter 的最快方法

    我正在寻找最快的方式来写入像素javafx scene image Image 写信给BufferedImage的后备数组要快得多 至少在我制作的测试图像上 只花了大约 20 毫秒BufferedImage WritableImage另一方
  • Java 和 GUI - 根据 MVC 模式,ActionListener 属于哪里?

    我目前正在编写一个模板 Java 应用程序 不知何故 如果我想完全遵循 MVC 模式 我不确定 ActionListener 属于哪里 该示例基于 Swing 但它不是关于框架 而是关于 Java 中 MVC 的基本概念 使用任何框架创建
  • 如何解决 Laravel 8 UI 分页问题?

    我在尝试最近发布的 laravel 8 时遇到了问题 我试图找出变化是什么以及它是如何工作的 当我这样做时 我遇到了分页 laravel 8 UI 变得混乱的问题 不知何故它发生了 有人可以帮助我吗 或者经历过同样的事情 像这样我在 lar
  • Javafx 组合框不会在实时更改时更新下拉列表大小?

    我正在使用 Javafx v8 0 25 b18 我出现的问题是动态组合框的下拉列表的大小不会改变 所以如果我最初在下拉列表中有两个项目 那么下拉列表大小将适合两个项目 但如果我现在用以下内容填充动态组合框三个项目 然后我在里面得到一个小滚
  • 如何在不同的分辨率/屏幕上提供相同的应用程序

    Scenario 您需要在不同的屏幕上展示相同的应用程序 假设标准的 15 英寸 17 英寸 便携式 10 英寸和移动 4 英寸 可能在不同的分辨率下工作 Question 您是否尝试采用一种根据可用空间重新排列的流动布局 或者您是否滚动
  • 将暂停屏幕绘制为播放屏幕上的一层 -LibGdx

    在我的 LibGdx 游戏中 我创建了暂停功能 在玩游戏时 如果我按下暂停按钮 则会显示一个带有恢复按钮的单独屏幕 实际上我想做的是暂停屏幕应该像一层一样出现在游戏屏幕上方 就像下面的游戏截图一样 我只能在我的游戏中使用单独的背景和所有内容
  • 在 tkinter Label 中漂亮地打印数据

    我有以下示例数据 data 1 JohnCena Peter 24 74 2 James Peter 24 70 3 Cena Peter 14 64 14 John Mars 34 174 我想在 tkinter 输出窗口上以漂亮的表格方
  • setSize() 不起作用?

    我有一个程序 需要两个按钮 一个是常规按钮 另一个具有根据鼠标悬停而变化的图片 目前 由于图片很大 JButton自定义也很大 我可以更改自定义的大小并保持图像 和翻转图像 成比例吗 我尝试过 setSize 但它没有任何作用 对于任何反馈
  • 如何从具有重复条目的过滤列表中删除特定索引?

    我有一个TableView由一个支持SortedList包裹一个FilteredList包裹一个ObservableList 过滤列表中的项目可以重复 也就是说 有可能是这样的情况list get 5 list get 10 用户可以选择行
  • 我如何抓取标题中含有特定单词的所有窗口?

    我正在运行 gnome 并且有一个程序可以生成大量单独的进程 每个进程都有自己的 GUI 窗口 我希望能够有选择地抓取标题与特定模式匹配的打开窗口来关闭它们 有人知道一种方法可以轻松做到这一点吗 你肯定想用python wnck 对于文档
  • 在 JavaFX 中拖动未装饰的舞台

    我希望将舞台设置为 未装饰 使其可拖动且可最小化 问题是我找不到这样做的方法 因为我遇到的示例是通过插入到主方法中的方法来实现的 我想通过控制器类中声明的方法来完成此操作 就像我如何使用下面的 WindowClose 方法来完成此操作 这是
  • JavaFX HTMLEditor - 插入图像功能

    我正在使用 JavaFX 集成的 HTMLEditor 它具有的所有功能都很好 但我还需要具有在 HTML 文本中插入图像的功能 你知道我可以使用的一些来源吗 或者其他一些可以在 JavaFX 中使用的 HTML WYSIWYG 编辑器并且
  • QObject多重继承

    我正在尝试在 C Qt 类中使用 mix 来提供一大堆具有通用接口的小部件 该接口是以这样的方式定义的 如果它被定义为其他小部件类的基类 那么小部件本身将具有这些信号 class SignalInterface public QObject
  • 在 Pyinstaller、语音识别和 Pyttsx3 中使用“-w”时,PySimpleGUI 中出现“OSError:[WinError 6] 句柄无效”

    所以我用 PySimpleGUI 创建了一个程序 然后用 Pyinstaller 从它创建了 exe 文件 这是我的命令 pyinstaller hidden import pyttsx3 drivers hidden import pyt
  • 使android listview布局可滚动

    我有一个 xml 文件 其布局为 ASCII 形式 ImageView TextView List
  • 禁用 com.android.systemui 是否安全?

    我发现 Android 最近的应用程序对话框可以通过禁用来禁用 包裹com android systemui 我想在信息亭模式下运行我的 已取得 root 权限的 设备 因此长按时不要显示最近的应用程序对话框至关重要 现在 到底是什么com
  • Gluon 移动 iOS 音频播放器

    由于 JavaFx Media 尚未移植到移动平台 任何人都可以帮助我使用本机 iOS APi 来播放声音 mp3 文件 该文件将存储在我的 gluon 项目的 main resources 文件夹中 在 Android 上 我们可以轻松地
  • 我可以双击 tkinter 列表框选项来调用 Python 中的函数吗?

    我有一个带有关联的 选择 按钮的列表框 我希望我的 GUI 能够双击任何列表框值来调用此按钮的命令 当选择一个选项并且用户双击窗口中的任何位置时 我的尝试 如下 有效 我希望它仅在双击选择本身 蓝色突出显示的行 时才起作用 做这个的最好方式
  • 如何从表列javafx中删除行

    这些是我的表格列 Course and 描述 如果单击一行 该行变为 活动 突出显示 并且他们按下Delete按钮它应该删除该行 我该怎么做 我的代码Course列 以及我要添加什么事件侦听器到我的delete按钮 SuppressWarn
  • 如何在Netbeans中设置JList的ListModel?

    我在 Netbeans IDE 的帮助下设计了一个 Swing GUI 该 GUI 包含一个 JList 默认情况下 它使用 QAbstractListModel 将其作为 JList 构造函数中的参数传递以创建该 JList 我想在 Ne

随机推荐