gpt4 book ai didi

java - 从 ListView 中获取选定的行号

转载 作者:行者123 更新时间:2023-12-02 04:51:56 25 4
gpt4 key购买 nike

我正在编写一个程序,该程序从外部文件打开代码并将其显示在 ListView 中。界面部分如下所示:

enter image description here

当我从 ListView 中选择该行并单击“链接”按钮时,我使用以下代码获取所选行:

        String CodeArea = (String) lstCode.getSelectionModel().getSelectedItem();

Stage dialog = new Stage();
dialog.initStyle(StageStyle.UTILITY);
Scene scene = new Scene(new Group(new Text(100, 100, CodeArea))); //this is just to display the output for testing purpose

该程序的主要任务是存储用户选择的行号,稍后重新打开时,所选行号应突出显示。

有没有办法获取行号而不是选定的行内容?例如:而不是使用 getSelectedItem(); 获取选定的行内容我想获取选定的行号。

最佳答案

您可以尝试getSelectedIndex()来获取您选择的索引。

int selectedIndex = lstCode.getSelectionModel().getSelectedIndex();

现在,因为您需要行号,恕我直言,行号不应包含零 (0),只需将 1 添加到索引即可获取行号。

int lineNumber = lstCode.getSelectionModel().getSelectedIndex() + 1;

关于java - 从 ListView 中获取选定的行号,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29138136/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com