为什么TableView / TableColumn不可编辑?

时间:2014-04-25 18:47:43

标签: java javafx javafx-2 javafx-8 fxml

来自以下FXML:

<?xml version="1.0" encoding="UTF-8"?>

<?import javafx.geometry.*?>
<?import javafx.scene.control.*?>
<?import javafx.scene.image.*?>
<?import javafx.scene.layout.*?>
<?import javafx.scene.text.*?>

<VBox spacing="10" xmlns:fx="http://javafx.com/fxml">
   <Label     fx:id="_lru"     styleClass="wizard-title-centered" />
   <Label     fx:id="_prompt"  styleClass="wizard-label" />
   <TableView fx:id="_tableVw" maxWidth="560" prefHeight="200" editable="true">
     <columns>
       <TableColumn fx:id="_name"            prefWidth="220" editable="false" />
       <TableColumn fx:id="_partNum"         prefWidth= "80" editable="false" />
       <TableColumn fx:id="_serialNum"       prefWidth= "84" editable="true"  />
       <TableColumn fx:id="_calibrationDate" prefWidth="120" editable="true"  />
       <TableColumn fx:id="_available"       prefWidth= "40" editable="false" />
     </columns>
   </TableView>
   <Text fx:id="_note" wrappingWidth="550" styleClass="wizard-label" />
</VBox>

与以下Java类相关联:

public class Page extends AbstractPage<Data> implements Initializable {

   @FXML private Label                          _lru;
   @FXML private Label                          _prompt;
   @FXML private TableView<Record>              _tableVw;
   @FXML private TableColumn<Record, String>    _name;
   @FXML private TableColumn<Record, String>    _partNum;
   @FXML private TableColumn<Record, String>    _serialNum;
   @FXML private TableColumn<Record, LocalDate> _calibrationDate;
   @FXML private TableColumn<Record, Boolean>   _available;
   @FXML private Text                           _note;

   @Override
   public void initialize( URL location, ResourceBundle resources ) {
      _lru            .setText( _worker.getData()._tpsSoftName );
      _prompt         .setText( I18n.get("EXTERNAL_RC_PROMPT"   ));
      _name           .setText( I18n.get("EXTERNAL_RC_NAME"     ));
      _partNum        .setText( I18n.get("EXTERNAL_RC_PARTNUM"  ));
      _serialNum      .setText( I18n.get("EXTERNAL_RC_SERIALNUM"));
      _calibrationDate.setText( I18n.get("EXTERNAL_RC_CALIB"    ));
      _available      .setText( I18n.get("EXTERNAL_RC_AVAILABLE"));
      _note           .setText( I18n.get("EXTERNAL_RC_NOTE"     ));
      _name           .setCellValueFactory(new PropertyValueFactory<Record, String>   ("name"));
      _partNum        .setCellValueFactory(new PropertyValueFactory<Record, String>   ("partNum"));
      _serialNum      .setCellValueFactory(new PropertyValueFactory<Record, String>   ("serialNum"));
      _calibrationDate.setCellValueFactory(new PropertyValueFactory<Record, LocalDate>("calibrationDate"));
      _available      .setCellValueFactory(new PropertyValueFactory<Record, Boolean>  ("available"));
      _calibrationDate.setCellFactory(
         new Callback<TableColumn<Record,LocalDate>, TableCell<Record,LocalDate>>() {
         @Override public TableCell<Record,LocalDate> call( TableColumn<Record,LocalDate> c ) {
             return new DatePickerCell<>(); }});
      _available.setCellFactory(
         new Callback<TableColumn<Record,Boolean>, TableCell<Record,Boolean>>() {
         @Override public TableCell<Record,Boolean> call( TableColumn<Record,Boolean> c ) {
             return new CheckBoxTableCell<>(); }});
   }

   @Override
   public void processDone( boolean successfully ) {
      _tableVw.getItems().setAll( _worker.getData()._resources);
   }
}

当“校准日期”列可编辑时,双击serialNum列不会激活基础TextField。 为什么呢?

1 个答案:

答案 0 :(得分:2)

这是因为默认单元工厂不提供javafx.scene.control.cell.TextFieldTableCell文档中指定的编辑器。

  

默认情况下,TextFieldTableCell在没有时呈现为Label   正在编辑,在编辑模式下作为TextField。 TextField   默认情况下,将拉伸以填充整个表格单元格。

添加以下代码可以完成这项工作。

_serialNum.setCellFactory( TextFieldTableCell.<Record>forTableColumn());