2017-04-10 3 views
0

私は自分のアプリケーションで、独自のTableCellを持つ二重値の列を含む3つの異なるテーブルを持っています。 2つの値の入力と表示は、3つのテーブルすべてで同じです。 残念ながら、最初のコード行が異なるため、同じTableCellDoubleクラス3xを作成する必要がありました。JavaFX/TableView:3つの異なるテーブルが同じTableCellDoubleを使用しますが、3x TableCellDoubleクラスが必要... 1xに減らすには?

//3x different data class with the getters and setters 
DataLineExpectedValue.java 
DataLineInputMoney.java 
DataLinePayPosition.java 

//3x TableCellDouble, although these are the same except for the first line 
TableCellDouble_expectedValue.java: 
public class TableCellDouble_expectedValue extends TableCell<DataLineExpectedValue, String> { //for DataLineExpectedValue 

    private MyTextFieldOnlyDoubleWithComma textFieldOnlyDouble = new MyTextFieldOnlyDoubleWithComma(); 

    public TableCellDouble_expectedValue() { ... } 

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

    @Override 
    public void startEdit() { ... } 

    @Override 
    public void commitEdit(String newValue) { ... } 

    @Override 
    public void cancelEdit() { ...} 
} 


TableCellDouble_inputMoney.java: 
public class TableCellDouble_inputMoney extends TableCell<DataLineInputMoney, String> { //for DataLineInputMoney 
    The rest is the same code as above. 
    ... 
} 


TableCellDouble_payPosition.java: 
public class TableCellDouble_payPosition extends TableCell<DataLinePayPosition, String> { //for DataLinePayPosition 
    The rest is the same code as above. 
    ... 
} 

//Question: 
//How to get the 3 almost same classes: 
//TableCellDouble_expectedValue.java, 
//TableCellDouble_inputMoney.java and 
//TableCellDouble_payPosition.java 
//=> in a class called TableCellDouble.java 
//And then use it uniformly in all tables in the application. 

//E.g. Instead of: 
table01Column01.setCellFactory((param) -> { return new TableCellDouble_inputMoney(); }); 
table02Column04.setCellFactory((param) -> { return new TableCellDouble_expectedValue(); }); 
table03Column11.setCellFactory((param) -> { return new TableCellDouble_payPosition(); }); 

//Then uniformly so: 
table01Column01.setCellFactory((param) -> { return new TableCellDouble(); }); 
table02Column04.setCellFactory((param) -> { return new TableCellDouble(); }); 
table03Column11.setCellFactory((param) -> { return new TableCellDouble(); }); 

答えて

2

使用の一般的な定義

public class TableCellDouble<T> extends TableCell<T, String> { 

... your code 

} 
+0

おかげで、コードは今よりコンパクトです。 – Sten

関連する問題