2017-08-03 40 views
0

我想在JavaFX Tableview中的單元格中設置文本樣式。我的模型有StringProperty title, description;BooleanProperty warning;JavaFX TableView:如何將條件單元格樣式移動到FXML/CSS?

public class MyItem { 
    @FXML 
    private final StringProperty title = new SimpleStringProperty(); 
    @FXML 
    private final StringProperty description = new SimpleStringProperty();  
    @FXML 
    private final BooleanProperty warning = new SimpleBooleanProperty(); 
    //... 
} 

的表應該有titledescription。說明始終是-fx-font-weight: normal。如果是warning-fx-font-weight: normal,則標題爲-fx-font-weight: bold。這是一個可用的POJO方法。

column.setCellFactory(new Callback<TableColumn<Message, String>, TableCell<Message, String>>() { 
     public TableCell<Message, String> call(TableColumn<Message, String> param) { 
      return new TableCell<Message, String>() { 

       @Override 
       protected void updateItem(String item, boolean empty) { 
        super.updateItem(item, empty); 

        if (empty || null == getTableRow() || null == getTableRow().getItem()) { 
         setText(null); 
        } else { 
         MyItem m = (MyItem) this.getTableRow().getItem(); 

         if (null != m) { 
          setText(m.getTitle());         
          String style = m.isWarning()? "-fx-font-weight: bold" : "-fx-font-weight: normal"; 
          setStyle(style); 
         } 
        } 
       } 
      }; 
     } 
    }); 

我想將樣式移動到FXML和CSS。這裏是我的嘗試:

public class MyController{ 

    @FXML 
    private TableView myTable; 
    @FXML 
    private TableColumn<MyItem, String> titleColumn; 
    @FXML 
    private TableColumn<MyItem, String> descriptionColumn; 

    //... 
} 
<content> 
    <TableView styleClass="fxml-table-style" stylesheets="@my_table_style.css" fx:id="myTable" > 
     <columns> 
     <TableColumn text="Title"> 
      <cellValueFactory><PropertyValueFactory property="title" /> 
      </cellValueFactory> 
     </TableColumn> 
     <TableColumn text="Body"> 
      <cellValueFactory><PropertyValueFactory property="description" /> 
      </cellValueFactory> 
     </TableColumn> 
     </columns> 
    </TableColumn> 
</columns> 

如何告訴Java的「如果使用自定義樣式1列標題和行isWarning;使用自定義風格的-2,否則」?

+0

可以移動的實際樣式規則('「-fx-字體 - 重量:粗體「:」-fx-font-weight:normal「'),但條件部分將需要保留在您的單元格工廠的Java中。你可以讓Java代碼開關一個僞類。 –

回答

2

你可以用CSS做的一件事是編寫選擇器,考慮到Node的父母。

這將是適合於preudoclass添加到TableRow爲什麼會產生警告屬性是true

public class WarningRowFactory implements Callback<TableView<MyItem>, TableRow<MyItem>> { 

    private static final PseudoClass WARNING = PseudoClass.getPseudoClass("warning"); 

    @Override 
    public TableRow<MyItem> call(TableView<MyItem> table) { 
     return new TableRow<MyItem>() { 

      private final InvalidationListener listener = observable -> pseudoClassStateChanged(WARNING, getItem() != null && getItem().isWarning()); 

      @Override 
      public void updateItem(MyItem item, boolean empty) { 
       if (getItem() != null) { 
        getItem().warningProperty().removeListener(listener); 
       } 

       super.updateItem(item, empty); 

       if (item != null) { 
        item.warningProperty().addListener(listener); 
       } 

       listener.invalidated(null); 
      } 
     }; 
    } 
} 
<TableView styleClass="fxml-table-style" stylesheets="@my_table_style.css" fx:id="myTable" > 
    <rowFactory> 
     <WarningRowFactory /> 
    </rowFactory> 
    ... 
.table-row-cell .table-cell { 
    -fx-font-weight: normal; 
} 

.table-row-cell:warning .table-cell { 
    -fx-font-weight: bold; 
} 
+0

應該在這裏:''? IDE以紅色突出顯示:無法解析。 – Stepan

+0

您需要在fxml文件開頭的處理指令中爲該類添加相應的導入作爲導入。 – fabian

+0

無法強制... WarningRowFactory to javafx.util.Callback Stepan

相關問題