2013-04-23 97 views
2

我對編輯數據這個可編輯的JavaFX表自動創建的TableView列:基於數據類信息

public class DataTable { 

    public void initTable(Stage primaryStage, Group root, Scene scene) { 

     tableView.setEditable(true); 
     Callback<TableColumn, TableCell> cellFactory = 
       new Callback<TableColumn, TableCell>() { 
      @Override 
      public TableCell call(TableColumn p) { 
       return new EditingCell(); 
      } 
     }; 

     TableColumn columnMonth = new TableColumn("Month"); 
     columnMonth.setCellValueFactory(
       new PropertyValueFactory<Record, String>("fieldMonth")); 

     TableColumn columnValue = new TableColumn("Value"); 
     columnValue.setCellValueFactory(
       new PropertyValueFactory<Record, Double>("fieldValue")); 

     //--- Add for Editable Cell of Value field, in Double 
     columnValue.setCellFactory(cellFactory); 
     columnValue.setOnEditCommit(
       new EventHandler<TableColumn.CellEditEvent<Record, Double>>() { 
      @Override 
      public void handle(TableColumn.CellEditEvent<Record, Double> t) { 
       ((Record) t.getTableView().getItems().get(
         t.getTablePosition().getRow())).setFieldValue(t.getNewValue()); 
      } 
     }); 
     //--- 

     tableView.setItems(dataList); 
     tableView.getColumns().addAll(columnMonth, columnValue); 

     VBox vBox = new VBox(); 
     vBox.setSpacing(10); 
     vBox.getChildren().add(tableView); 
     vBox.setLayoutX(410); 
     vBox.setLayoutY(34); 

     root.getChildren().add(vBox); 


    } 

    public class Record { 

     private SimpleStringProperty fieldMonth; 
     private SimpleDoubleProperty fieldValue; 

     Record(String fMonth, double fValue) { 
      this.fieldMonth = new SimpleStringProperty(fMonth); 
      this.fieldValue = new SimpleDoubleProperty(fValue); 
     } 

     public String getFieldMonth() { 
      return fieldMonth.get(); 
     } 

     public double getFieldValue() { 
      return fieldValue.get(); 
     } 

     public void setFieldMonth(String fMonth) { 
      fieldMonth.set(fMonth); 
     } 

     public void setFieldValue(Double fValue) { 
      fieldValue.set(fValue); 
     } 
    } 
    private TableView<Record> tableView = new TableView<>(); 
    private ObservableList<Record> dataList = 
      FXCollections.observableArrayList(
      new Record("January", 100), 
      new Record("February", 200), 
      new Record("March", 50), 
      new Record("April", 75), 
      new Record("May", 110), 
      new Record("June", 300), 
      new Record("July", 111), 
      new Record("August", 30), 
      new Record("September", 75), 
      new Record("October", 55), 
      new Record("November", 225), 
      new Record("December", 99)); 

    class EditingCell extends TableCell<Record, Double> { 

     private TextField textField; 

     public EditingCell() { 
     } 

     @Override 
     public void startEdit() { 
      super.startEdit(); 

      if (textField == null) { 
       createTextField(); 
      } 

      setGraphic(textField); 
      setContentDisplay(ContentDisplay.GRAPHIC_ONLY); 
      textField.selectAll(); 
     } 

     @Override 
     public void cancelEdit() { 
      super.cancelEdit(); 

      setText(String.valueOf(getItem())); 
      setContentDisplay(ContentDisplay.TEXT_ONLY); 
     } 

     @Override 
     public void updateItem(Double item, boolean empty) { 
      super.updateItem(item, empty); 

      if (empty) { 
       setText(null); 
       setGraphic(null); 
      } else { 
       if (isEditing()) { 
        if (textField != null) { 
         textField.setText(getString()); 
        } 
        setGraphic(textField); 
        setContentDisplay(ContentDisplay.GRAPHIC_ONLY); 
       } else { 
        setText(getString()); 
        setContentDisplay(ContentDisplay.TEXT_ONLY); 
       } 
      } 
     } 

     private void createTextField() { 
      textField = new TextField(getString()); 
      textField.setMinWidth(this.getWidth() - this.getGraphicTextGap() * 2); 
      textField.setOnKeyPressed(new EventHandler<KeyEvent>() { 
       @Override 
       public void handle(KeyEvent t) { 
        if (t.getCode() == KeyCode.ENTER) { 
         commitEdit(Double.parseDouble(textField.getText())); 
        } else if (t.getCode() == KeyCode.ESCAPE) { 
         cancelEdit(); 
        } 
       } 
      }); 
     } 

     private String getString() { 
      return getItem() == null ? "" : getItem().toString(); 
     } 
    } 
} 

我要創建的小程序,可以顯示從數據庫表中的數據和編輯表。當我點擊一個表時我遇到了這個問題,我必須從JPA對象中生成動態表視圖,所以我每次都有一個表,但具有不同的列。我能以某種方式在每次使用來自JPA的數據生成表格時根據情況自動更新列嗎?

+0

? – tarrsalah 2013-04-23 19:30:14

+0

我想根據Java對象屬性的數量更改列的數量。 – 2013-04-23 19:38:35

+0

您需要學習[java反射API](http://docs.oracle.com/javase/tutorial/reflect/)。 – tarrsalah 2013-04-23 19:44:13

回答

5

您可以使用反射來實現這一點。例如,分析你的數據類的方法,併爲每一個與Property結束創建列:

public class SmartTableView extends Application { 

    private void populateColumns(Class clazz, TableView table) { 
     for (Method method : clazz.getMethods()) { 
      String name = method.getName(); 
      if (name.endsWith("Property")) { 
       String propName = name.replace("Property", ""); 

       TableColumn column = new TableColumn(propName); 
       column.setCellValueFactory(new PropertyValueFactory<>(propName)); 
       table.getColumns().add(column); 
      } 
     } 
    } 

    @Override 
    public void start(Stage primaryStage) { 
     final ObservableList<Person> data = FXCollections.observableArrayList(
       new Person("Jacob", "Smith", "[email protected]"), 
       new Person("Isabella", "Dow", "[email protected]"), 
       new Person("Ethan", "Williams", "[email protected]"), 
       new Person("Emma", "Jones", "[email protected]"), 
       new Person("Michael", "Brown", "[email protected]")); 

     TableView table = new TableView(data); 
     populateColumns(Person.class, table); 

     Scene scene = new Scene(table, 300, 250); 
     primaryStage.setTitle("Hello"); 
     primaryStage.setScene(scene); 
     primaryStage.show(); 
    } 

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

    public static class Person { 

     private final SimpleStringProperty firstName; 
     private final SimpleStringProperty lastName; 
     private final SimpleStringProperty email; 

     private Person(String fName, String lName, String email) { 
      this.firstName = new SimpleStringProperty(fName); 
      this.lastName = new SimpleStringProperty(lName); 
      this.email = new SimpleStringProperty(email); 
     } 

     public String getFirstName() { 
      return firstName.get(); 
     } 

     public void setFirstName(String fName) { 
      firstName.set(fName); 
     } 

     public StringProperty firstNameProperty() { 
      return firstName; 
     } 

     public String getLastName() { 
      return lastName.get(); 
     } 

     public void setLastName(String fName) { 
      lastName.set(fName); 
     } 

     public StringProperty lastNameProperty() { 
      return lastName; 
     } 

     public String getEmail() { 
      return email.get(); 
     } 

     public void setEmail(String fName) { 
      email.set(fName); 
     } 

     public StringProperty emailProperty() { 
      return lastName; 
     } 
    } 
} 
要調整表列