想象一下,你有一个包含字符串的listview和一个包含字符串的List。我想选择listview中与List<String>
匹配的所有字符串,但是,我尝试使用SelectionModel中的selectIndices函数并输出正确的整数,但它不会选择它们,只会选择它们最后一个(灰色&#39; ish)颜色。我希望他们都有灰色的颜色。
一个例子是:
Listview: Cat, Dog, Tiger, Gorilla, Monkey
List: Dog & Gorilla.
选择Dog&amp;列表视图中的大猩猩。 selectionMode是&#39; MULTIPLE&#39; SelectionModel是MultipleSelectionModel。
ObservableList<String> names = MainModel.getInstance().getGroupNames();
names.remove(group.getName());
listviewInheritance.setItems(names);
int[] indices = new int[group.getInheritance().size()];
List<String> inheriNames = group.getInheritance();
for(int i = 0; i < inheriNames.size(); i++) {
for(int j = 0; j < names.size(); j++) {
if(inheriNames.get(i).equals(names.get(j))) {
System.out.println("Inheri: " + inheriNames.get(i) + " | Name: " + names.get(j)); // test purpose
indices[i] = j;
}
}
}
if(indices.length > 0) {
System.out.println(Arrays.toString(indices));
listviewInheritance.getSelectionModel().selectIndices(-1, indices);
}
我错过了什么?
答案 0 :(得分:0)
这是一个你可以改变的小应用程序。此应用会选择ListView
中的任何List
项。
import java.util.ArrayList;
import java.util.List;
import javafx.application.Application;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.ListView;
import javafx.scene.control.SelectionMode;
import javafx.scene.layout.StackPane;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;
/**
*
* @author blj0011
*/
public class JavaFXApplication4 extends Application {
@Override
public void start(Stage primaryStage) {
ListView lvMain = new ListView();//Create ListView
lvMain.getSelectionModel().setSelectionMode(SelectionMode.MULTIPLE);//Change ListView selection mode to multiple
ObservableList<String> items = FXCollections.observableArrayList("Julia", "Ian", "Sue", "Matthew", "Hannah", "Stephan", "Denise");//ObseravableList that will be used to set the ListView
lvMain.setItems(items);//Set the ListView's items
List<String> list = new ArrayList();//List that will hold potential items that are in the ListView
list.add("Sue");//Add Sue to List
list.add("Stephan");//Add Stephan to List
Button btn = new Button();
btn.setText("Say 'Hello World'");
btn.setOnAction(new EventHandler<ActionEvent>() {
@Override
public void handle(ActionEvent event) {
//When the Button is pressed, loop through the list checking it's items against the ObservableList's items.
for(String entry : list)
{
for(String itemEntry : items)
{
if(entry.equals(itemEntry))//If the items are equal
{
lvMain.getSelectionModel().select(itemEntry);//select the item in the ListView
break;//If you have duplicates in the ListView remove this line!
}
}
}
}
});
VBox vbox = new VBox();
vbox.getChildren().addAll(lvMain, btn);
StackPane root = new StackPane();
root.getChildren().add(vbox);
Scene scene = new Scene(root, 300, 250);
primaryStage.setTitle("Hello World!");
primaryStage.setScene(scene);
primaryStage.show();
}
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
launch(args);
}
}
如果您希望所选项目在选中后显示为蓝色,则只需点击ListView
,或者您可以在外部lvMain.requestFocus();
之后添加For Loop
。
答案 1 :(得分:0)
我使用的代码确实有效,但我还有另一行覆盖它。然而,Sedrick Jefferson提到的requestFocus()函数确实让它成为蓝色。感谢。