2011-08-29 46 views
7

我有一個帶有複選框的表。當我點擊第三或第四列中的複選框時,我想更改第一列中複選框的選擇。我想能夠更改同一行上的其他單元格。我已經有了專欄,所以我想知道這個單元格在哪一行。我也很不確定我是否擁有這個單元。JavaFX 2:獲取TableCell行索引

我做了什麼,到目前爲止我從

想通大多

enter image description here

這裏是我的SSCCE(短SEL f包含可編譯示例)

如果下面的代碼有問題,請更正我的錯誤。

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,有必要調用getTableView().edit(getTableRow().getIndex(), param)。這使單元進入「編輯模式」。由於沒有startEdit方法,所以進入編輯模式幾乎沒有涉及,但它仍然是必需的。

之後,如下所述:http://download.oracle.com/javafx/2.0/ui_controls/table-view.htm

有必要呼籲

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"); 
     } 
    } 
} 

現在我需要知道的是how to update the table's display once the data is updated

1

使用Observables的優點是JavaFX UI元素可以在「幕後」爲您執行綁定。換句話說,如果您將數據模型類實現爲JavaFX Bean,則只要UI發生更改,您的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(); 
    } 
} 

對於這個答覆,我將只專注於你的第一個實例字段,一個。要改變這一點,以便它符合了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; } 

有可能寫一個JavaFX Bean,它提供了一個懶惰的屬性定義初始化,但這會起作用。 Java bean和JavaFX bean之間的區別在於,您還必須爲屬性提供訪問者(上面的最後一行)。

如果您將所有字段設置爲與上述類似的屬性,則會發現UI更新以反映更改。