2016-06-23 5 views
0

コメントに基づいて私の質問を改訂しています。私はチェックボックスの列の数が実行時にのみ分かっているJavaFX TableViewを持っています。したがって、列を作成するには、次のようにします。EDIT:動的列番号を持つTableViewのモデル

TableColumn attributeColumn = new TableColumn("Attribut"); 
    attributeColumn.setCellValueFactory(new PropertyValueFactory<AttributeRow, String>("name"));   
    attributeTable.getColumns().add(attributeColumn); 

    for (String group : companyGroups) 
    { 
     TableColumn< AttributeRow, Boolean > groupColumn = new TableColumn<>(group); 
     groupColumn.setCellFactory(CheckBoxTableCell.forTableColumn(groupColumn)); 
     groupColumn.setCellValueFactory(f -> f.getValue().activeProperty()); 
     groupColumn.setEditable(true); 
     attributeTable.getColumns().add(groupColumn); 
    } 

質問:テーブルモデルはこのTableViewのように見えますか?チェックボックス列の固定数があった場合は、2列を言う、私のモデルは次のようになります。

public class AttributeRow { 

private SimpleStringProperty name; 
private SimpleBooleanProperty active = new SimpleBooleanProperty(false);  

public AttributeRow(String name, Boolean active) { 
    this.name= new SimpleStringProperty(name);   
} 

public SimpleStringProperty nameProperty() { 
    if (name == null) { 
     name = new SimpleStringProperty(this, "name"); 
    } 
    return name; 
} 

public String getAttributeName() { 
    return name.get(); 
} 

public void setAttributeName(String fName) { 
    name.set(fName); 
} 

public final SimpleBooleanProperty activeProperty() { 
    return this.active; 
} 
public final boolean isActive() { 
    return this.activeProperty().get(); 
} 
public final void setActive(final boolean active) { 
    this.activeProperty().set(active); 
} 
} 

私は1つの文字列の列と1つのチェックボックス列を持っている場合は、このモデルでは動作します。しかし、実行時にその数だけが分かっているチェックボックス列がたくさんある場合、私はどうしますか?

+4

あなたが達成しようとしているかを説明できますか?テーブルに表示されるモデルの適切なプロパティを変更しないでください。 – Itai

+0

私は動的なチェックボックス列数を持つTableViewを持っているので、列数は実行時にのみ決定できます。だからモデルを使うことはできません。なぜなら、私が実際に必要とするプロパティーの数がわからないからです。 – user41854

+1

データはどのように表されますか?たとえ配列やリストに含まれていても、それは可能です。 'TableView'はセルの仮想化を採用しているので、コントロール(UI)を変更するとすべてのデータオブジェクトが確実に変更されない場合があります。 – Itai

答えて

1

は、あなたは本当にデータの構造を説明していないが、そこString S(companyGroups)のコレクションのいくつかの種類があり、それぞれの行は、テーブルにはStringname)で表され、1つのブールのためにされているように見えますそれぞれの要素はcompanyGroupsです。だから、これを実行するための1つの方法は、単にマップ内のキーはcompanyGroupsの要素であることを意図しているモデルクラスAttributeRow、中Map<String, BooleanProperty>を定義するには、次のようになります。セル値の工場についての特別なものは何もありません

import java.util.HashMap; 
import java.util.List; 
import java.util.Map; 

import javafx.beans.property.BooleanProperty; 
import javafx.beans.property.SimpleBooleanProperty; 
import javafx.beans.property.SimpleStringProperty; 
import javafx.beans.property.StringProperty; 

public class AttributeRow { 

    private final StringProperty name = new SimpleStringProperty(); 

    private final Map<String, BooleanProperty> activeByGroup = new HashMap<>(); 

    public AttributeRow(List<String> companyGroups) { 
     for (String group : companyGroups) { 
      activeByGroup.put(group, new SimpleBooleanProperty()) ; 
     } 
    } 

    public final BooleanProperty activeProperty(String group) { 
     // might need to deal with the case where 
     // there is no entry in the map for group 
     // (else calls to isActive(...) and setActive(...) with 
     // a non-existent group will give a null pointer exception): 

     return activeByGroup.get(group) ; 
    } 

    public final boolean isActive(String group) { 
     return activeProperty(group).get(); 
    } 

    public final void setActive(String group, boolean active) { 
     activeProperty(group).set(active); 
    } 

    public final StringProperty nameProperty() { 
     return this.name; 
    } 


    public final String getName() { 
     return this.nameProperty().get(); 
    } 


    public final void setName(final String name) { 
     this.nameProperty().set(name); 
    } 



} 

あなただけのモデルを更新値を更新する

for (String group : groups) { 
    TableColumn<AttributeRow, Boolean> groupColumn = new TableColumn<>(group); 
    groupColumn.setCellFactory(CheckBoxTableCell.forTableColumn(groupColumn)); 
    groupColumn.setCellValueFactory(cellData -> cellData.getValue().activeProperty(group)); 
    attributeTable.getColumns().add(groupColumn); 
} 

そしてもちろん: - 列のためにそれはまだちょうど列の適切な観察可能なプロパティにそれぞれの行をマップするために持っている

Button selectAll = new Button("Select all"); 
selectAll.setOnAction(e -> { 
    for (AttributeRow row : attributeTable.getItems()) { 
     for (String group : groups) { 
      row.setActive(group, true); 
     } 
    } 
}); 
ここ

SSCCEです:

import java.util.Arrays; 
import java.util.List; 
import java.util.stream.Collectors; 

import javafx.application.Application; 
import javafx.geometry.Insets; 
import javafx.geometry.Pos; 
import javafx.scene.Scene; 
import javafx.scene.control.Button; 
import javafx.scene.control.TableColumn; 
import javafx.scene.control.TableView; 
import javafx.scene.control.cell.CheckBoxTableCell; 
import javafx.scene.layout.BorderPane; 
import javafx.scene.layout.HBox; 
import javafx.stage.Stage; 

public class TableWithMappedBooleans extends Application { 

    private static final List<String> groups = Arrays.asList("Group 1", "Group 2", "Group 3", "Group 4"); 

    @Override 
    public void start(Stage primaryStage) { 

     TableView<AttributeRow> attributeTable = new TableView<>(); 
     attributeTable.setEditable(true); 

     TableColumn<AttributeRow, String> attributeColumn = new TableColumn<>("Attribute"); 
     attributeColumn.setCellValueFactory(cellData -> cellData.getValue().nameProperty()); 

     attributeTable.getColumns().add(attributeColumn); 

     for (String group : groups) { 
      TableColumn<AttributeRow, Boolean> groupColumn = new TableColumn<>(group); 
      groupColumn.setCellFactory(CheckBoxTableCell.forTableColumn(groupColumn)); 
      groupColumn.setCellValueFactory(cellData -> cellData.getValue().activeProperty(group)); 
      attributeTable.getColumns().add(groupColumn); 
     } 

     // generate data: 
     for (int i = 1 ; i <= 10; i++) { 
      AttributeRow row = new AttributeRow(groups); 
      row.setName("Attribute "+i); 
      attributeTable.getItems().add(row); 
     } 

     // button to select everything: 

     Button selectAll = new Button("Select all"); 
     selectAll.setOnAction(e -> { 
      for (AttributeRow row : attributeTable.getItems()) { 
       for (String group : groups) { 
        row.setActive(group, true); 
       } 
      } 
     }); 

     // for debugging, to check data are updated from check boxes: 
     Button dumpDataButton = new Button("Dump data"); 
     dumpDataButton.setOnAction(e -> { 
      for (AttributeRow row : attributeTable.getItems()) { 
       String groupList = groups.stream() 
         .filter(group -> row.isActive(group)) 
         .collect(Collectors.joining(", ")); 
       System.out.println(row.getName() + " : " + groupList); 
      } 
      System.out.println(); 
     }); 

     HBox buttons = new HBox(5, selectAll, dumpDataButton); 
     buttons.setAlignment(Pos.CENTER); 
     buttons.setPadding(new Insets(5)); 

     BorderPane root = new BorderPane(attributeTable, null, null, buttons, null); 

     Scene scene = new Scene(root); 
     primaryStage.setScene(scene); 
     primaryStage.show(); 
    } 

    public static void main(String[] args) { 
     launch(args); 
    } 
} 
+0

うわー、ありがとうJames_D。私は感銘を受けて。できます。私の貧しい初期の質問をお詫び申し上げます。とにかく私を助けてくれてありがとう。 :-) – user41854

関連する問題