2011-08-29 33 views
7

Tôi có một hộp với các hộp kiểm. Tôi muốn thay đổi lựa chọn hộp kiểm trong cột đầu tiên khi tôi nhấp vào hộp kiểm trong cột thứ ba hoặc thứ tư. Tôi muốn có thể thay đổi các ô khác trên cùng một hàng. Tôi đã có các cột vì vậy tôi muốn biết những gì các tế bào là trong. Tôi cũng rất không chắc chắn cho dù tôi có nó cho đến nay hay không.JavaFX 2: Nhận chỉ mục hàng của TableCell

Những gì tôi đã làm cho đến nay tôi đã tìm chủ yếu từ

enter image description here

Đây là SSCCE tôi (ngắn Sel f Ví dụ có thể Compilable)

Hãy sửa tôi nếu có điều gì đó sai với mã bên dưới.

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

Trả lời

6

Hầu Có

Trước khi gọi commitEdit, nó là cần thiết để gọi getTableView().edit(getTableRow().getIndex(), param). Thao tác này sẽ đưa ô đó vào "chế độ chỉnh sửa". Vì không có phương thức startEdit, có rất ít việc tham gia vào chế độ chỉnh sửa nhưng vẫn bắt buộc.

Sau đó, như mô tả ở đây: http://download.oracle.com/javafx/2.0/ui_controls/table-view.htm

Nó là cần thiết để gọi

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

Bây giờ tất cả tôi cần phải biết là how to update the table's display once the data is updated.

1

Lợi thế của việc sử dụng Observables là các phần tử giao diện người dùng JavaFX có thể thực hiện các ràng buộc cho bạn "đằng sau hậu trường". Nói cách khác, nếu bạn triển khai lớp mô hình dữ liệu của mình dưới dạng một JavaFX Bean, giao diện người dùng của bạn sẽ tự động cập nhật bất cứ khi nào nó thay đổi. Nó thực hiện điều này vì các ràng buộc cho dữ liệu quan sát được trong mô hình của bạn được tự động gán và thay đổi các sự kiện thông báo được tạo tự động.

Nhưng bạn phải xác định mô hình dữ liệu của mình theo mô hình đậu JavaFX để điều này xảy ra, nếu không giao diện người dùng của bạn sẽ không cập nhật khi thay đổi xảy ra.

mô hình dữ liệu của bạn được định nghĩa như thế này:

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

Đối với câu trả lời này, tôi sẽ chỉ tập trung vào lĩnh vực ví dụ 1 của bạn, một. Để chuyển đổi này để nó là phù hợp với mô hình đậu JavaFX cho một tài sản JavaFX, viết mã của bạn theo cách này, ví dụ:

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

Có thể viết định nghĩa tài sản cho một bean JavaFX cung cấp cho một lazier khởi tạo, nhưng điều này sẽ làm việc. Sự khác biệt giữa một hạt Java và một bean JavaFX là bạn cũng phải cung cấp một trình truy cập cho thuộc tính (dòng cuối cùng ở trên).

Nếu bạn đặt tất cả các trường của bạn thành các thuộc tính tương tự như ở trên, bạn sẽ thấy rằng bản cập nhật giao diện người dùng của mình phản ánh thay đổi.

Các vấn đề liên quan