V.Dev
V.Dev

Reputation: 167

Align text in center in combo box added into table cell

Code for editing table column is as below

@FXML
private TableView<StateTransition> stateTransitionTable;
@FXML
private TableColumn<StateTransition, Long> stId;
@FXML
private TableColumn<StateTransition, String> stState;
@FXML
private TableColumn<StateTransition, String> stNewState;

private void setTableEditable(Boolean val){

ObservableList<String> list = FXCollections.observableArrayList(workFlowSet);
stState.setCellFactory(ComboBoxTableCell.forTableColumn(new DefaultStringConverter(), list));

}

This code is giving combo box in table cell but text in combo box is in left side, I want text in center of that ComboBoxTableCell.

Stuck here, help me with that.

Thanks

Upvotes: 0

Views: 1513

Answers (2)

Kachna
Kachna

Reputation: 2961

You can do this:

stState.setCellFactory(tableCol -> {
            ComboBoxTableCell<StateTransition, String> comboBoxTableCell = new ComboBoxTableCell<>(new DefaultStringConverter(), list);
           comboBoxTableCell.setAlignment(Pos.CENTER);
           return comboBoxTableCell;
        });

Update: Try it this way:

stState.setCellFactory(col -> {

            return new TableCell<StateTransition, String>() {

                @Override
                protected void updateItem(String item, boolean empty) {
                    super.updateItem(item, empty);
                    if (item == null || empty) {
                        setText(null);

                    }
                    else{
                        ComboBox<String> cb = new ComboBox<>(list);
                        cb.setValue(item);
                        cb.setConverter(new DefaultStringConverter());
                        cb.setStyle("-fx-padding:0.25 0 0.25 3.0");
                        setGraphic(cb);

                    }
                }

            };

        });

Upvotes: 0

Slaw
Slaw

Reputation: 45806

You can use CSS. If you add a stylesheet to the Scene have the following in the file:

.table-column .combo-box .list-cell {
    -fx-alignment: center;
}

The .list-cell is the important part (for non-editable ComboBox), I just put .table-column and .combo-box so not every ListCell on the scene-graph is aligned to the center.

Have a look at the JavaFX CSS Reference Guide for more CSS information.

Upvotes: 1

Related Questions