2017-08-15 381 views
0

想象一下,您有一個包含字符串和List的列表視圖。我想選擇列表視圖中的所有字符串,匹配List<String>,但是,我試圖使用SelectionModel中的selectIndices函數,並且正確的整數出來,但它不會選擇它們,只有最後一個(與灰色)顏色。我希望他們都有灰色的顏色。JavaFX Listview - 多選編程

一個例子是:

Listview: Cat, Dog, Tiger, Gorilla, Monkey 
List: Dog & Gorilla. 

選擇狗&列表視圖中的大猩猩。 selectionMode是'MULTIPLE',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); 
} 

我在想什麼?

+1

你嘗試過的某些代碼可能會幫助人們幫助你 – Rafalon

+1

你是否已將'selectionMode'設置爲'MULTIPLE'? – fabian

+0

是的。我可以用我的鼠標和控制按鈕選擇多行,但我想要編程。 –

回答

0

這是一個你可以改變的小應用程序。這個應用程序選擇List中的任何ListView項目。

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,也可以外For Loop之後添加lvMain.requestFocus();

+0

我發現了這個問題。我什至沒有注意到它,但我有另一行代碼,所以我的代碼確實工作。有些東西正在覆蓋它。 –

0

我使用的代碼確實有效,但我有另一行覆蓋它。但是,Sedrick Jefferson提到的requestFocus()函數確實使其變爲藍色。謝謝。