2016-04-20 156 views
0

私はJavaFXので観測可能なリストが移入テーブルビューテーブルを持っているとすることを、複数のものが選択されている場合ので、私は、すべての項目のラジオボタンやチェックボックスを開催する列を必要とします次のGUI画面リストに使用できます。私はこのプロジェクトの前にTableView/JavaFXを使ったことが一度もありません。ボタン/チェックボックスを実装する方法は分かっていますが、私のリストやtableColumnにどのように置くのか分かりません。どんな助けも大歓迎です。あなたはあなたのRadioButtonまたは何か他のものを入れたいセルのための「updateItem」メソッドをオーバーライドする必要があることになるためにはラジオボタン/チェックボックス - JavaFXの

@FXML private TableView<SummerClass> table; 

     @FXML private TableColumn<SummerClass, Integer> id; 
     @FXML private TableColumn<SummerClass, String> dept; 
     @FXML private TableColumn<SummerClass,Integer> number; 
     @FXML private TableColumn<SummerClass, String> title; 
     @FXML private TableColumn<SummerClass, String> day; 
     @FXML private TableColumn<SummerClass, String> time; 

public ObservableList<SummerClass> list1 =  FXCollections.observableArrayList(
     new SummerClass (10001, "ACCT", 1010 , "Intro to Acct (3)", "MWF", "1:00 - 2:15"), 
     new SummerClass (10002, "ACCT", 2010 , "Acct for Bus. (3)", "MWF", "9:00 - 10:15"), 
     new SummerClass (10003, "ART", 1010 , "Fund. of Art (3)", "TR", "3:00 - 4:15"), 
     new SummerClass (10004, "ART", 1110 , "Art History (3)", "MWF", "1:00 - 2:15"), 
+1

vegetarianCol.setCellValueFactory(new PropertyValueFactory<>("vegetarian")); vegetarianCol.setCellFactory(CheckBoxTableCell.forTableColumn(vegetarianCol)); vegetarianCol.setEditable(true); 

私はちょうどコピーしていると、以下のOracleフォーラムのリンクからJames_Dのソリューションを貼り付け選択した行のセットを追跡するだけの場合は、チェックボックスを表示するのではなく、テーブルビューの組み込み選択機能を使用することができます。 –

答えて

0

。ここで

@Override 
    public void updateItem(Number item, boolean empty) { 
     super.updateItem(item, empty); 
     if (empty) { 
      setGraphic(null); 
     } else { 
      setGraphic(***YourNode!***); 
     } 

のようなものは、あなたがそれがどのように動作するかを確認するために使用することができますテーブルビューに追加、ボタンのサンプルです:James_D状態として http://java-buddy.blogspot.de/2013/03/javafx-embed-button-in-tableview.html

1

コメントで:

あなたの場合選択された行のセットを追跡するためだけにチェックボックスを使用すると、チェックボックスをまったく使用せずに、テーブルビューの組み込み選択機能を使用することができます。


しかし、内蔵の場合の選択機能は、あなたのために適切でないと、あなたは、チェックボックスの機能が必要なのです、あなたは、次のいずれか

  • Set a custom cell factory on a column
      、とは Ruslan's answer ORのようにupdateItem上書き
    1. 組み込みのCheckBoxTableCell機能を使用します。

    2つのオプションのうち、何らかの理由でCheckBoxTableCellが機能しない限り、カスタムセルファクトリと更新ハン​​ドラを作成する代わりにCheckBoxTableCellを使用することをお勧めします。

    キー:

    は、あなたがそれについての詳細情報が必要な場合CheckBoxTableCellを使用する方法を求めてさまざまな既存のStackOverflowの質問またはOracleフォーラムの質問がありますCheckBoxTableCellを使用する部分は、bacであるモデルクラスに関連するブール値プロパティを定義することです王あなたのテーブルおよび関連する編集可能なテーブルのカラムにリンクするには:あなたがチェックを使用している場合

    import javafx.application.Application; 
    import javafx.beans.property.*; 
    import javafx.collections.FXCollections; 
    import javafx.scene.Scene; 
    import javafx.scene.control.*; 
    import javafx.scene.control.cell.*; 
    import javafx.scene.layout.*; 
    import javafx.stage.Stage; 
    
    public class CheckBoxTableCellTest extends Application { 
        @Override 
        public void start(Stage primaryStage) { 
        final TableView<Person> tableView = new TableView<>(); 
        tableView.setItems(FXCollections.observableArrayList(
         new Person("Robert", "Plant"), 
         new Person("Neil", "Young"), 
         new Person("Willie", "Nelson"), 
         new Person("Natalie", "Merchant") 
        )); 
        tableView.getItems().get(3).setVegetarian(true); 
        final TableColumn<Person, String> firstNameCol = new TableColumn<>("First Name"); 
        final TableColumn<Person, String> lastNameCol = new TableColumn<>("Last Name"); 
        final TableColumn<Person, Boolean> vegetarianCol = new TableColumn<>("Vegetarian"); 
        tableView.getColumns().addAll(firstNameCol, lastNameCol, vegetarianCol); 
        firstNameCol.setCellValueFactory(new PropertyValueFactory<>("firstName")); 
        lastNameCol.setCellValueFactory(new PropertyValueFactory<>("lastName")); 
        vegetarianCol.setCellValueFactory(new PropertyValueFactory<>("vegetarian")); 
        vegetarianCol.setCellFactory(CheckBoxTableCell.forTableColumn(vegetarianCol)); 
        vegetarianCol.setEditable(true); 
        tableView.setEditable(true); 
    
        final BorderPane root = new BorderPane(); 
        root.setCenter(tableView); 
    
        final HBox controls = new HBox(5); 
        final Button infoButton = new Button("Show details"); 
        infoButton.setOnAction(event -> { 
         for (Person p : tableView.getItems()) 
         System.out.printf("%s %s (%svegetarian)%n", p.getFirstName(), 
          p.getLastName(), p.isVegetarian() ? "" : "not "); 
         System.out.println(); 
        }); 
        controls.getChildren().add(infoButton); 
        root.setBottom(controls); 
    
        Scene scene = new Scene(root, 300, 400); 
        primaryStage.setScene(scene); 
        primaryStage.show(); 
        } 
    
        public static void main(String[] args) { 
        launch(args); 
        } 
    
        public static class Person { 
        private StringProperty firstName; 
        private StringProperty lastName; 
        private BooleanProperty vegetarian; 
    
        public Person(String firstName, String lastName) { 
         this.firstName = new SimpleStringProperty(firstName); 
         this.lastName = new SimpleStringProperty(lastName); 
         this.vegetarian = new SimpleBooleanProperty(false); 
        } 
    
        public String getFirstName() { 
         return firstName.get(); 
        } 
    
        public String getLastName() { 
         return lastName.get(); 
        } 
    
        public boolean isVegetarian() { 
         return vegetarian.get(); 
        } 
    
        public void setFirstName(String firstName) { 
         this.firstName.set(firstName); 
        } 
    
        public void setLastName(String lastName) { 
         this.lastName.set(lastName); 
        } 
    
        public void setVegetarian(boolean vegetarian) { 
         this.vegetarian.set(vegetarian); 
        } 
    
        public StringProperty firstNameProperty() { 
         return firstName; 
        } 
    
        public StringProperty lastNameProperty() { 
         return lastName; 
        } 
    
        public BooleanProperty vegetarianProperty() { 
         return vegetarian; 
        } 
        } 
    }