2017-03-22 2 views
0

いいえ、私はGridPaneのバックテーブル実装を適切なTableViewに捨てました。VBox内のフィルタパラメータを使用したTableViewのフィルタリングJavaFX-8

この表では、左側の列はImageViewで、右側の列はラベル付きのVBoxです。

以下のコードは、データベースにデータを含まないように変更されています。

public ObservableList<Users> showUserData() { 
    for(int i = 0; i <= 5; i++) { 
     Image image = new Image("file:///" + [path to whatever image in your PC]); 
     VBox vbox = new VBox(); 

     vbox.getChildren().addAll(new Label("Name: " + "a name"), 
      new Label("Username: " + "a username"), 
      new Label("Position: " + "a position")); 

     ImageView pic = new ImageView(); 
     pic.setFitHeight(150); 
     pic.setFitWidth(150); 
     pic.setImage(image); 
     pic.setSmooth(true); 
     pic.setPreserveRatio(true); 
     users.add(new Users(pic, vbox)); 

    } 

    return users; 
} 

これは、テーブルの実際の母集団が行われる初期化メソッドに渡されます。

ユーザークラスは、途中で次のようになります。

public class Users { 
    private ImageView userImage; 
    private VBox userDetails; 

    public Users() { 
     this.userImage = null; 
     this.userDetails = null; 
    } 

    public Users(ImageView userImage, VBox userDetails) { 
     this.userImage = userImage; 
     this.userDetails = userDetails; 
    } 

    public ImageView getUserImage() { 
     return userImage; 
    } 

    public void setUserImage(ImageView userImage) { 
     this.userImage = userImage; 
    } 

    public VBox getUserDetails() { 
     return userDetails; 
    } 

    public void setUserDetails(VBox userDetails) { 
     this.userDetails = userDetails; 
    } 
} 

プログラムが意図したとおり、私はイメージを持つテーブルとその中の詳細とVBoxのを見ることができます動作します。

ここでは、テーブルのフィルタリングを行うTextFieldを追加します。フィルタパラメータはラベル内のテキストです。

私はObservableListをFilteredListに配置し、次にSortedListに配置する必要があることがわかりました。

しかし、フィルタパラメータは、個々の列にVBoxコンテナの内部ではなく配置されているので、(私はちょうどそのダムだ場合を除き)、私はインターネット上で見るすべての例が適用されません。さらに、パラメータはラベルの中にあり、単純な文字列ではありません。

このチュートリアルではフィルタリングのためにhttp://code.makery.ch/blog/javafx-8-tableview-sorting-filtering/に行っています。

これはかなり特殊なケースだと思いますが、私はそれに固執することを要求しています。私は助け、ヒント、またはちょうどあらゆる方向の指示が必要です。ありがとう。

答えて

1

Nodeを表示していないアイテムにデータを保存してください!多数のユーザー場合、これは劇的に、フットプリントを削減することができます

public class UserDetails { 

    private final String name; 
    private final String username; 
    private final String position; 

    public UserDetails(String name, String username, String position) { 
     this.name = name; 
     this.username = username; 
     this.position = position; 
    } 

    public String getName() { 
     return name; 
    } 

    public String getUsername() { 
     return username; 
    } 

    public String getPosition() { 
     return position; 
    } 

} 
public class User { 
    private UserDetails userDetails; 
    private Image image; 

    public User(UserDetails userDetails, Image image) { 
     this.userDetails = userDetails; 
     this.image = image; 
    } 

    public UserDetails getUserDetails() { 
     return userDetails; 
    } 

    public Image getImage() { 
     return image; 
    } 

} 
public ObservableList<User> showUserData() { 
    ObservableList<User> users = FXCollections.observableArrayList(); 
    for (int i = 0; i <= 5; i++) { 
     users.add(new User(
       new UserDetails("a name", "a username", "a position"), 
       new Image("https://cdn.sstatic.net/Sites/stackoverflow/company/img/logos/so/so-icon.png"))); 

    } 

    return users; 
} 

@Override 
public void start(Stage primaryStage) { 
    TableView<User> table = new TableView<>(showUserData()); 

    TableColumn<User, Image> imageColumn = new TableColumn<>(); 
    TableColumn<User, UserDetails> userDetailsColumn = new TableColumn<>(); 
    table.getColumns().addAll(imageColumn, userDetailsColumn); 

    imageColumn.setCellValueFactory(new PropertyValueFactory<>("image")); 
    userDetailsColumn.setCellValueFactory(new PropertyValueFactory<>("userDetails")); 

    imageColumn.setCellFactory(col -> new TableCell<User, Image>() { 

     private final ImageView imageView = new ImageView(); 

     { 
      imageView.setFitHeight(150); 
      imageView.setFitWidth(150); 
      imageView.setSmooth(true); 
      imageView.setPreserveRatio(true); 
     } 

     @Override 
     protected void updateItem(Image item, boolean empty) { 
      super.updateItem(item, empty); 

      if (empty) { 
       imageView.setImage(null); 
       setGraphic(null); 
      } else { 
       imageView.setImage(item); 
       setGraphic(imageView); 
      } 
     } 

    }); 

    userDetailsColumn.setCellFactory(col -> new TableCell<User, UserDetails>() { 

     private final Label nameLabel = new Label(); 
     private final Label userNameLabel = new Label(); 
     private final Label positionLabel = new Label(); 
     private final VBox vbox = new VBox(nameLabel, userNameLabel, positionLabel); 

     @Override 
     protected void updateItem(UserDetails item, boolean empty) { 
      super.updateItem(item, empty); 

      if (empty) { 
       setGraphic(null); 
      } else { 
       nameLabel.setText("Name: "+item.getName()); 
       userNameLabel.setText("Username: "+item.getUsername()); 
       positionLabel.setText("Position: "+item.getPosition()); 
       setGraphic(vbox); 
      } 
     } 

    }); 

    Scene scene = new Scene(table); 

    primaryStage.setScene(scene); 
    primaryStage.show(); 
} 

:あなたは、あなたがそれを好きなように表示するデータを表示cellFactoryの作成細胞を使用することができますそれはすべての項目の大Node Sクラスを格納しますが、すべてのセルに関連したものを再利用していないので...、

これを作成しています

TextField textField = ... 
FilteredList<User> filteredList = ... 

textField.textProperty().addListener((observable, oldValue, newValue) -> filteredList.setPredicate(newValue.isEmpty() 
         ? null 
         : user -> user.getName().contains(newValue) 
           || user.getUsername().contains(newValue) 
           || user.getPosition().contains(newValue))); 
+0

ちょっとした作業が必要でしたが、私はそれを実現しました。ありがとう!ところで、あなたは、あなたの操作を行い、行が挿入された後、テーブルを更新する方法を知っているではないでしょうか? –

+0

あなたが観測可能な特性を使用してのような意味(http://stackoverflow.com/documentation/javafx/2229/tableview/7289/propertyvaluefactory#t=201703230612594013393/http://stackoverflow.com/documentation/javafx/2229/tableview/7288/ sample-tableview-with-2-columns#t = 201703230612594013393)?あるいは、javafx 8u60から始めると、すべてのセルを更新する 'TableView'に' refresh'メソッドがあります。 – fabian

関連する問題