2017-04-10 15 views
0

난 내 자신의 TableCell과 이중 값 열을 포함하는 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