Sten
Sten

Reputation: 91

JavaFX/TableView: 3 different tables use the same TableCellDouble, but 3x TableCellDouble classes necessary ... how to reduce to 1x?

I have in my application 3 different tables that contain double-value columns with their own TableCell. The input and display of the double values is the same in all three tables. Unfortunately, I had to create the identical TableCellDouble class 3x, only because of the different 1st code line.

//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(); });

Upvotes: 0

Views: 56

Answers (1)

purring pigeon
purring pigeon

Reputation: 4209

Use generic definition

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

... your code

}

Upvotes: 2

Related Questions