2015-03-19 75 views
0

我在做一个程序,它从外部文件打开一个代码并在列表视图中显示它。一个界面的部分看起来像为:从列表视图中获取所选行号

 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 

enter image description here

当我选择从列表视图行,并点击“链接”按钮,我用下面的代码获取选中行

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

有什么方法可以获得行号而不是选定的行内容吗?例如:而不是使用getSelectedItem();获取选定的行内容我想获取选定的行号。

回答

2

您可以尝试getSelectedIndex()获取所选内容的索引。

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

现在,因为你需要的行号,这恕我直言不应该包含zero (0),只需添加1到索引获得的行号。

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

很简单..它的工作!我不知道为什么我以同样的方式尝试过它没有工作。谢谢。 – 2015-03-19 06:38:20

+0

它也可以以同样的方式用于多选模式吗?你有什么主意吗? – 2015-03-19 06:42:00

+0

如果您有多种选择模式,请使用['getSelectedIndices()'](https://docs.oracle.com/javafx/2/api/javafx/scene/control/MultipleSelectionModel.html#getSelectedIndices%28%29) getSelectedIndex()' – ItachiUchiha 2015-03-19 06:55:33

相关问题