2013-04-22 9 views
13

一部のTableView行の外観を変更するのにはいくつかの困難があります。行にはストロークのテキストが赤で表示されます。実際には、私は赤色で表示することができますが、まだストロークを行うことはできません。これは私がラインの外観を変更するために使用しているCSSクラスです:TableViewの行の外観を更新する

.itemCancelado { 
    -fx-strikethrough: true; 
    -fx-text-fill: red; 
} 

キャンセルとして、ユーザーがアイテムをマークしたときに、このスタイルクラスが追加されます。

public class ItemCanceladoCellFactory implements Callback<TableColumn, TableCell> { 
    @Override 
    public TableCell call(TableColumn tableColumn) { 
     return new TableCell<ItemBean, Object>() { 
      @Override 
      public void updateItem(Object item, boolean empty) { 
       super.updateItem(item, empty); 
       setText(empty ? "" : getItem().toString()); 
       setGraphic(null); 
       int indice=getIndex(); 
       ItemBean bean=null; 
       if(indice<getTableView().getItems().size()) 
        bean = getTableView().getItems().get(indice); 
       if (bean != null && bean.isCancelado()) 
        getStyleClass().add("itemCancelado"); 
      } 
     }; 
    } 
} 

別の問題がここにありますキャンセルされた行は、ユーザーが観測可能なリストに要素を追加または削除するときにのみ色が変わります。私はTableViewの更新を強制することができる方法はありますか?

編集した情報

私はBooleanPropertyを使用するItemBeanクラスを変更し、それが部分的に解決:残念ながら

public class ItemBean { 
    ... 
    private BooleanProperty cancelado = new SimpleBooleanProperty(false); 
    ... 
    public Boolean getCancelado() { 
     return cancelado.get(); 
    } 

    public void setCancelado(Boolean cancelado){ 
     this.cancelado.set(cancelado); 
    } 

    public BooleanProperty canceladoProperty(){ 
     return cancelado; 
    } 
} 

、唯一のコラム「canceladoを」(これが最終的に働くときには隠してか、削除されます)外観を変更します。

ここ

cancelado column changes the appearance

私は列とテーブルを設定:

public class ControladorPainelPreVenda extends ControladorPainel { 

    @FXML 
    private TableView<ItemBean> tabelaItens; 
    private ObservableList<ItemBean> itens = FXCollections.observableArrayList(); 
    ... 

    private void configurarTabela() { 
     colunaCodigo.setCellValueFactory(new MultiPropertyValueFactory<ItemBean, String>("produto.id")); 
     colunaCodigo.setCellFactory(new ItemCanceladoCellFactory()); 
     colunaDescricao.setCellValueFactory(new MultiPropertyValueFactory<ItemBean, String>("produto.descricao")); 
     colunaDescricao.setCellFactory(new ItemCanceladoCellFactory()); 
     colunaLinha.setCellValueFactory(new MultiPropertyValueFactory<ItemBean, String>("produto.nomeLinha")); 
     colunaLinha.setCellFactory(new ItemCanceladoCellFactory()); 
     colunaQuantidade.setCellValueFactory(new PropertyValueFactory<ItemBean, BigDecimal>("quantidade")); 
     colunaQuantidade.setCellFactory(new ItemCanceladoCellFactory()); 
     colunaValorLiquido.setCellValueFactory(new PropertyValueFactory<ItemBean, BigDecimal>("valorLiquido")); 
     colunaValorLiquido.setCellFactory(new ItemCanceladoCellFactory()); 
     colunaValorTotal.setCellValueFactory(new PropertyValueFactory<ItemBean, BigDecimal>("valorTotal")); 
     colunaValorTotal.setCellFactory(new ItemCanceladoCellFactory()); 
     colunaCancelado.setCellValueFactory(new PropertyValueFactory<ItemBean, Boolean>("cancelado")); 
     colunaCancelado.setCellFactory(new ItemCanceladoCellFactory()); 
     tabelaItens.setItems(itens); 
    } 
    ... 
} 

すべての列を更新するにはどうすればよいですか?

答えて

9

は、私はテーブルビューの更新を強制することができます方法はありますか?

CanceladoItemBeanクラスのプロパティを作成します。

private BooleanProperty cancelado = new SimpleBooleanProperty(false); 
public BooleanProperty canceladoProperty() { 
    return cancelado; 
} 

今、リストビューのデフォルトのセルの実装がcanceladoプロパティの変更をリッスンし、関連するリストビュー電池用updateItem呼び出しなどをトリガします適切な。

プロパティを返す関数の名前が重要であることに注意してください。JavaFXはJavaBXメンバのgetterおよびsetterパターンをプロパティとして拡張するため、canceladoProperty()である必要があります。条約の背景に命名

プロパティ

JavaFXのプロパティにアクセスするための命名規則は、Oracle Using JavaFX Properties and Bindingチュートリアルからこのコード・スニペットに示されています。

package propertydemo; 

import javafx.beans.property.DoubleProperty; 
import javafx.beans.property.SimpleDoubleProperty; 

class Bill { 

    // Define a variable to store the property 
    private DoubleProperty amountDue = new SimpleDoubleProperty(); 

    // Define a getter for the property's value 
    public final double getAmountDue(){return amountDue.get();} 

    // Define a setter for the property's value 
    public final void setAmountDue(double value){amountDue.set(value);} 

    // Define a getter for the property itself 
    public DoubleProperty amountDueProperty() {return amountDue;} 

} 

プロパティと読み取り専用のプロパティと怠惰な性質のような様々な、より高度な利用シナリオの命名規則の詳細openfxのwikiにJavaFX Property Architectureの偉大な概要があります。

Futher情報とブール行の値に基づいて、テーブルの行の外観をカスタマイズする例は、このgist sample codeに設けられているテーブルビューの行の外観のカスタマイズ

private TableColumn<Friend, Boolean> makeBooleanColumn(String columnName, String propertyName, int prefWidth) { 
    TableColumn<Friend, Boolean> column = new TableColumn<>(columnName); 
    column.setCellValueFactory(new PropertyValueFactory<Friend, Boolean>(propertyName)); 
    column.setCellFactory(new Callback<TableColumn<Friend, Boolean>, TableCell<Friend, Boolean>>() { 
    @Override public TableCell<Friend, Boolean> call(TableColumn<Friend, Boolean> soCalledFriendBooleanTableColumn) { 
     return new TableCell<Friend, Boolean>() { 
     @Override public void updateItem(final Boolean item, final boolean empty) { 
      super.updateItem(item, empty); 

      // clear any custom styles 
      this.getStyleClass().remove("willPayCell"); 
      this.getStyleClass().remove("wontPayCell"); 
      this.getTableRow().getStyleClass().remove("willPayRow"); 
      this.getTableRow().getStyleClass().remove("wontPayRow"); 

      // update the item and set a custom style if necessary 
      if (item != null) { 
      setText(item.toString()); 
      this.getStyleClass().add(item ? "willPayCell" : "wontPayCell"); 
      this.getTableRow().getStyleClass().add(item ? "willPayRow" : "wontPayRow"); 
      } 
     } 
     }; 
    } 
    }); 
    column.setPrefWidth(prefWidth); 
} 

関連

StackOverflowの質問Background with 2 colors in JavaFX?、同様のソリューションを提供します。その質問への答えの議論では、JavaFXの表の行ハイライトの注意点と微妙な点について詳しく説明しています(基本的に、フォーカスリング、選択したバー、ホバーフィードバックなどの擬似クラススタイルを得るのは本当に難しい)スタイル)。

+0

あなたの言ったことはしましたが、TableViewはまだ更新されません。 – brevleq

+0

あなたの質問の追加情報のように 'cancelado'プロパティ' canceladoProperty() 'を取得するメソッドの名前を' getCancelado() 'ではなく私の例のように指定する必要があります。 – jewelsea

+0

編集した質問で分かるように、部分的に解決されました:) – brevleq

5

あなたは.textのクラスに取り消し線を設定する必要があります;-)

.itemCancelado { 
    -fx-text-fill: red; 
} 
.itemCancelado .text { 
    -fx-strikethrough: true; 
} 
+0

ストライキの問題を解決しましたが、私はまだTableViewを更新する際に問題があります。 – brevleq

3

どのようにしてすべての列を更新できますか?あなたはストライキアウトして行全体を、すなわち、すべてのセルを行にしたい場合 、他のすべてのCellValueFactoryは、その条件のチェックが必要です:

... 
    if (indice < getTableView().getItems().size()) 
     bean = getTableView().getItems().get(indice); 
    if (bean != null && bean.isCancelado()) 
     getStyleClass().add("itemCancelado"); 
    ... 

をそれとも、decoratorsとして、あなたのコールバックを実装し、このようなものを持つことができます:

public class CellDecorator implements Callback<TableColumn, TableCell> { 
    private Callback<TableColumn, TableCell> decorated; 

    public CellDecorator(Callback<TableColumn, TableCell> toDecorate) { 
     this.decorated = toDecorate; 
    } 

    // Override this to do your thing. 
    public abstract void doStyle(TableCell tableCell); 

    @Override 
    public void style(TableCell tableCell) { 
     // Let the decorated styler do its thing. 
     decorated.style(tableCell); 

     // Now we do our thing. 
     doStyle(cell); 
    } 
} 

public class ItemCanceladoCellFactory extends CellDecorator { 
    public ItemCanceladoCellFactory(Callback<TableColumn, TableCell> toDecorate) { 
     super(toDecorate); 
    } 

    @Override 
    public void doStyle(TableCell tableCell) { 
     ... 
     if (bean != null && bean.isCancelado()) 
      getStyleClass().add("itemCancelado"); 
    } 
} 

... 
colunaCodigo.setCellValueFactory(new ItemCanceladoCellFactory(new MultiPropertyValueFactory<ItemBean, String>("produto.id"))); 
colunaDescricao.setCellValueFactory(new ItemCanceladoCellFactory(new MultiPropertyValueFactory<ItemBean, String>("produto.descricao"))); 
colunaDescricao.setCellFactory(new ItemCanceladoCellFactory(new ItemCanceladoCellFactory())); 
.... 

この方法で、「キャンセル済み」のスタイルコードを繰り返す必要はなく、行全体に適用することができます。 注:は、これはJavaFX対応のコードではありません。これは一般的な考えです。

関連する問題