如何在添加新项目后保持列表中的方框选中?

huangapple go评论136阅读模式
英文:

How to keep the box selected in the list, after adding a new item?

问题

我正在努力弄清楚如何在将新数据输入列表后将该数据的框转换为所选框。我实现了getSelectionModel()方法,以便可以点击列表的任何项目,但是我希望在添加新数据后可以选择新数据(即在其周围有蓝色/灰色框)。我有以下代码将新数据添加到列表中,但我不确定listView.getSelectionModel().select(name);是否是我想要的结果。

String newLine = name + "," + artist + "," + year + "," + album;
obsList.add(name.trim() + " " + artist.trim());
writeToFile(newLine);
nameBox.setText(name);
artistBox.setText(artist);
albumBox.setText(year);
yearBox.setText(year);
listView.getSelectionModel().select(name); // 这是我添加的部分,尝试在添加新信息后选择框
sortOBS();

这是我运行代码时列表弹出的方式。

如何在添加新项目后保持列表中的方框选中?

这是我输入新数据后列表的外观。

如何在添加新项目后保持列表中的方框选中?

我希望在添加到列表后可以选择新输入的数据。换句话说,在输入新数据后,我如何使添加的项目被选择(具有蓝色/灰色框)?

英文:

I'm trying to figure out how to make the box of the new data that was entered into my List after into a selected box after entering it. I implemented the getSelectionModel() method to where I can click on any item of the list but I want the new data that I entered to be selected (to have the blue/gray box around it) after I add new data. I have this code that adds new data into the list but I'm not sure if listView.getSelectionModel().select(name); is the way to get the result that I want.

String newLine = name + "," + artist + "," + year + "," + album;
obsList.add(name.trim() + " " + artist.trim());
writeToFile(newLine);
nameBox.setText(name);
artistBox.setText(artist);
albumBox.setText(year);
yearBox.setText(year);
listView.getSelectionModel().select(name); //this is what I added to try to select the box after I added  new info
sortOBS();

This is how my List pops up as when I run code.

如何在添加新项目后保持列表中的方框选中?

This is how my List looks like after I enter new data.

如何在添加新项目后保持列表中的方框选中?

I want the new data that I entered to be selected after I add to the list. In other words, how do I get the item that I added to be selected (blue/gray box around it)after entering new data?

答案1

得分: 1

你的代码无法工作是因为 select() 方法需要一个描述元素索引的整数,而不是实际的元素。

你需要保留刚刚添加的元素,以便稍后可以找到索引。而不是像这样匿名添加元素:

obsList.add(name.trim() + " " + artist.trim());

给元素命名,然后再添加,就像这样:

String entry = name.trim() + " " + artist.trim();
obsList.add(entry);

现在,代替这个:

listView.getSelectionModel().select(name); // 错误的

你可以这样做:

listView.getSelectionModel().select(obsList.indexOf(entry));

你可能更愿意有点懒惰,假设每个新条目的索引。因为你只是使用 obsList.add() 而没有指定位置,这意味着你总是添加到列表的末尾。新添加元素的索引将始终是 obsList.size() - 1,所以你可以这样做:

listView.getSelectionModel().select(obsList.size() - 1);
英文:

Your code doesn't work because the select() method takes an integer describing the index of an element rather than the actual element.

You need to retain the element that you just added so that you can find the index later. Instead of adding the element anonymously like this:

obsList.add(name.trim() + " " + artist.trim());

name the element and then add it, like this:

String entry = name.trim() + " " + artist.trim();
obsList.add(entry);

Now, instead of this:

listView.getSelectionModel().select(name); // wrong

you can do this:

listView.getSelectionModel().select(obsList.indexOf(entry));

You might prefer to be a bit lazy and assume the index of each new entry. Since you just do obsList.add() without specifying a position, that means that you're always adding to the end of the list. The index of the newly added element will always be obsList.size() - 1, so you can do:

listView.getSelectionModel().select(obsList.size() - 1);

huangapple
  • 本文由 发表于 2020年9月26日 12:34:09
  • 转载请务必保留本文链接:https://go.coder-hub.com/64073930.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定