2011-08-29 16 views
7

私はチェックボックス付きのテーブルを持っています。 3列目または4列目のチェックボックスをクリックすると、最初の列のチェックボックスの選択を変更します。私は同じ行の他のセルを変更できるようにしたい。私は既に列を持っているので、セルの行が何であるかを知りたいのですが、これまでのところそれが正しいかどうかは非常に不確実です。JavaFX 2:TableCell行インデックスを取得

私は

enter image description here

から主に考え出し私がこれまで行ってきた。ここに私SSCCE(ショートSELがありますfコンパイル可能な例が含まれています)

以下のコードに問題がある場合は、私に修正してください。 getTableView().edit(getTableRow().getIndex(), param)を呼び出すために

package javafxapplication5; 

import javafx.application.Application; 
import javafx.beans.property.StringProperty; 
import javafx.collections.FXCollections; 
import javafx.collections.ObservableList; 
import javafx.scene.Group; 
import javafx.scene.Scene; 
import javafx.scene.control.CheckBox; 
import javafx.scene.control.TableCell; 
import javafx.scene.control.TableColumn; 
import javafx.scene.control.TableView; 
import javafx.scene.paint.Color; 
import javafx.stage.Stage; 
import javafx.util.Callback; 

public class JavaFXApplication extends Application { 

    private static final ObservableList<ContactOptions> addContactOption = FXCollections.observableArrayList(
      new ContactOptions("Yes", "John Doe", "No", "Yes"), 
      new ContactOptions("Yes", "Jane Doe", "No", null), 
      new ContactOptions("Yes", "John Smith", "Yes", "Yes"), 
      new ContactOptions("Yes", "Patty Smith", "Yes", "No"), 
      new ContactOptions("Yes", "Jo Johnson", "Yes", "Yes"), 
      new ContactOptions("No", "Mary Johnson", "No", "No"), 
      new ContactOptions("Yes", "Clint Doe", "No", null), 
      new ContactOptions("Yes", "Sally Sue", "No", "Yes"), 
      new ContactOptions("Yes", "Bob Ryan", null, "Yes"), 
      new ContactOptions("No", "Mary Sue", "No", "No"), 
      new ContactOptions("Yes", "Bob Smith", "No", "Yes")); 
    private static TableView<ContactOptions> contactOptions = new TableView<ContactOptions>(); 

    public static void main(String[] args) { 
     Application.launch(JavaFXApplication.class, args); 
    } 

    @Override 
    public void start(Stage primaryStage) { 
     primaryStage.setTitle("Hello World"); 
     Group root = new Group(); 
     Scene scene = new Scene(root, 400, 200, Color.LIGHTGREEN); 

     Callback<TableColumn, TableCell> cellFactory = new Callback<TableColumn, TableCell>() { 

      @Override 
      public TableCell call(final TableColumn param) { 
       final CheckBox checkBox = new CheckBox(); 
       final TableCell cell = new TableCell() { 

        @Override 
        public void updateItem(Object item, boolean empty) { 
         super.updateItem(item, empty); 
         if (item == null) { 
          checkBox.setDisable(true); 
          checkBox.setSelected(false); 
         } else { 
          checkBox.setDisable(false); 
          checkBox.setSelected(item.toString().equals("Yes") ? true : false); 
          commitEdit(checkBox.isSelected() ? "Yes" : "No"); 
         } 
        } 
       }; 
       cell.setNode(checkBox); 
       return cell; 
      } 
     }; 

     TableColumn firstCol = new TableColumn("Contact?"); 
     firstCol.setPrefWidth(60); 
     firstCol.setProperty("one"); 
     firstCol.setCellFactory(cellFactory); 

     TableColumn secondCol = new TableColumn("Name"); 
     secondCol.setPrefWidth(200); 
     secondCol.setSortAscending(true); 
     secondCol.setProperty("two"); 

     TableColumn thirdCol = new TableColumn("Call"); 
     thirdCol.setPrefWidth(60); 
     thirdCol.setProperty("three"); 
     thirdCol.setCellFactory(cellFactory); 

     TableColumn fourthCol = new TableColumn("Email"); 
     fourthCol.setPrefWidth(60); 
     fourthCol.setProperty("four"); 
     fourthCol.setCellFactory(cellFactory); 

     contactOptions.setItems(addContactOption); 
     contactOptions.getColumns().addAll(firstCol, secondCol, thirdCol, fourthCol); 
     contactOptions.setPrefSize(400, 200); 

     root.getChildren().add(contactOptions); 
     primaryStage.setScene(scene); 
     primaryStage.setVisible(true); 
    } 

    public static class ContactOptions { 

     private final StringProperty one; 
     private final StringProperty two; 
     private final StringProperty three; 
     private final StringProperty four; 

     ContactOptions(String col1, String col2, String col3, String col4) { 
      this.one = new StringProperty(col1); 
      this.two = new StringProperty(col2); 
      this.three = new StringProperty(col3); 
      this.four = new StringProperty(col4); 
     } 

     public String getOne() { 
      return one.get(); 
     } 

     public String getTwo() { 
      return two.get(); 
     } 

     public String getThree() { 
      return three.get(); 
     } 

     public String getFour() { 
      return four.get(); 
     } 
    } 
} 

答えて

6

はほとんど

commitEditを呼び出す前に、それは必要があります。これは、セルを「編集モード」にします。 startEditメソッドがないので、編集モードに入ることはほとんどありませんが、依然として必要です。その後

、ここで説明するように:http://download.oracle.com/javafx/2.0/ui_controls/table-view.htm

今私が知る必要があるすべてはhow to update the table's display once the data is updatedある

firstCol.setOnEditCommit(new EventHandler<EditEvent<String>>() { 
    @Override 
    public void handle(EditEvent<String> event) { 
     String newValue = event.getNewValue(); 
     ContactOptions data = (ContactOptions) event.getTableView().getItems().get(event.getTablePosition().getRow()); 
     data.one.set(newValue) 
     if(newValue.equals("No")) { 
      data.three.set("No"); 
      data.four.set("No"); 
     } 
    } 
} 

を呼び出す必要があります。

1

Observablesを使用する利点は、JavaFX UIエレメントがあなたのためにバインディングを「裏口」で実行できることです。つまり、データモデルクラスをJavaFX Beanとして実装すると、UIは変更されるたびに自動的に更新されます。これは、モデル内の観測可能なデータのバインディングが自動的に割り当てられ、通知イベントが自動的に生成されるためです。

しかし、JavaFX Beanのパラダイムに従ってデータモデルを定義する必要があります。そうしないと、変更が発生してもUIは更新されません。

データ・モデルは次のように定義されています。この応答を

public static class ContactOptions { 

    private final StringProperty one; 
    private final StringProperty two; 
    private final StringProperty three; 
    private final StringProperty four; 

    ContactOptions(String col1, String col2, String col3, String col4) { 
     this.one = new StringProperty(col1); 
     this.two = new StringProperty(col2); 
     this.three = new StringProperty(col3); 
     this.four = new StringProperty(col4); 
    } 

    public String getOne() { 
     return one.get(); 
    } 

    public String getTwo() { 
     return two.get(); 
    } 

    public String getThree() { 
     return three.get(); 
    } 

    public String getFour() { 
     return four.get(); 
    } 
} 

は、私はあなたの第一インスタンスフィールド、1にのみ焦点を当てます。 、あなたのコードをこのように書くことがJavaFXのプロパティのためのJavaFX豆のパラダイムに準拠するように、例えば、これを変換するには、次の

public static class ContactOptions { 

    private final StringProperty one = new SimpleStringProperty(); 

    public final String getOne() { return this.one.get(); } 
    public final void setOne(String v) { this.one.set(v); } 
    public final StringProperty oneProperty() { return this.one; } 

lazierを提供するJavaFXのBeanのプロパティ定義を記述することが可能です初期化しますが、これはうまくいきます。 Java BeanとJavaFX Beanの違いは、プロパティのアクセサーも提供する必要があることです(上記の最後の行)。

すべてのフィールドを上記と同様のプロパティにすると、変更が反映されるようにUIが更新されます。

関連する問題