标准字体对话框按钮样式为JFoenix

时间:2019-03-24 16:25:00

标签: java css javafx dialog jfoenix

我正在制作javafx应用程序并使用Jfoenix库。

带有有关表中项目的特定信息的窗口将通过以下代码打开:

@FXML
public void showEditPolicyDialog() {
    Policy selectedPolicy = policyTable.getSelectionModel().getSelectedItem();
    int selectedPolicyIndex = policyTable.getSelectionModel().getSelectedIndex();

    if (selectedPolicy == null) {
        Alert alert = new Alert(Alert.AlertType.INFORMATION);
        alert.setTitle("No Policy Selected");
        alert.setHeaderText(null);
        alert.setContentText("Please select Policy you want to edit.");
        alert.showAndWait();
        return;
    } else {
        selectedPolicy = Datasource.getInstance().getFullPolicyById(selectedPolicy.getId());
    }

    Dialog<ButtonType> dialog = new Dialog<>();

    dialog.initOwner(mainWindowAnchorPane.getScene().getWindow());
    dialog.setTitle("Edit policy " + selectedPolicy.getNumber());
    FXMLLoader fxmlLoader = new FXMLLoader();
    fxmlLoader.setLocation(getClass().getResource("/ee/insa/views/PolicyDialogLayout.fxml"));

    try {
        dialog.getDialogPane().setContent(fxmlLoader.load());

    } catch (IOException e) {
        System.out.println("Couldn't load edit policy dialog");
        e.printStackTrace();
        return;
    }

    dialog.getDialogPane().getButtonTypes().add(ButtonType.OK);
    dialog.getDialogPane().getButtonTypes().add(ButtonType.CANCEL);

    Button okButton = (Button) dialog.getDialogPane().lookupButton(ButtonType.OK);
    okButton.setDefaultButton(false);

    dialog.getDialogPane().getStylesheets().add("ee/insa/CSS/PolicyWindowCSS.css");

    PolicyController policyController = fxmlLoader.getController();
    Policy fullPolicy = Datasource.getInstance().getFullPolicyById(selectedPolicy.getId());
    policyController.loadPolicyDataToForm(fullPolicy);

    Optional<ButtonType> result = dialog.showAndWait();

    if (result.isPresent() && result.get() == ButtonType.OK) {
        policyController.updatePolicyData(fullPolicy);
        Datasource.getInstance().updatePolicy(fullPolicy);
        Policy updatedPolicyForMainTable = Datasource.getInstance().getPolicyForMainTableById(fullPolicy.getId());
        policyTable.getItems().set(selectedPolicyIndex, updatedPolicyForMainTable);
        policyTable.getSelectionModel().select(selectedPolicyIndex);
        if (inClientSearch == false) {
            checkTableView();
        }
    }
}

如何将“对话框”按钮样式设置为JFoenix?现在,按钮具有标准的Windows风格。 enter image description here

在CSS中,我可以找到按钮所在的区域(粉红色):

.dialog-pane > .button-bar > .container {
    -fx-background-color: deeppink;
}

但是我找不到确切的那两个按钮。如果我开始使用

.dialog-pane > .button {
     -fx-background-color: deeppink;
}

然后重新设置所有按钮的样式。

已更新: 我试图通过以下代码实现JFXDialog:

    @FXML
public void showEditPolicyDialog(){
    Policy selectedPolicy = policyTable.getSelectionModel().getSelectedItem();
    selectedPolicy = Datasource.getInstance().getFullPolicyById(selectedPolicy.getId());

    try {
        Parent parent = FXMLLoader.load(getClass().getResource("/ee/insa/views/PolicyDialogLayout.fxml"));
        JFXDialogLayout dialogLayout = new JFXDialogLayout();
        dialogLayout.setBody(parent);
        JFXDialog dialog = new JFXDialog( mainWindowStackPane, dialogLayout, JFXDialog.DialogTransition.BOTTOM);
        dialog.show();
    } catch (IOException e) {
        e.printStackTrace();
    }
}

但是问题在于它不是单独的窗口,它仅在主窗口的空间中显示。

2 个答案:

答案 0 :(得分:1)

我找到了解决方案。使用此代码

for(ButtonType bt : dialog.getDialogPane().getButtonTypes()){
        Button button = (Button) dialog.getDialogPane().lookupButton(bt);
        button.getStyleClass().add("dialogButton");
}

我只能将CSS类应用于这两个按钮。

.dialogButton {
-fx-background-color: -fx-primary;
-fx-text-fill: -fx-primatytext;
-fx-font-size: 12pt;
-fx-background-radius: 0;
-fx-border-width: 1px;
-fx-border-color: -fx-secondary;
}
.dialogButton:hover {
    -fx-background-color: -fx-secondary;
}

现在看起来不错。 enter image description here

答案 1 :(得分:0)

尝试使用JFoenix JFXDialog代替默认的JavaFX`Dialog:

JFXDialog<ButtonType> dialog = new JFXDialog<>();