css - 使用 CSS 更改节点设计

标签 css tooltip javafx-8

我不知道我在为节点添加 CSS 样式时做错了什么。

我有一个主应用程序窗口。然后我单击菜单项,另一个模态窗口打开如下(ModalWindow extends Stage):

FXMLLoader loader = new FXMLLoader(getClass().getResource(CONSTANTS.ROOT_USER_EDIT.string));
BorderPane editPane;
   try {
        editPane = new BorderPane(loader.load());
        ModalWindow editWindow = new ModalWindow(Main.mainStage, editPane, "Edit user");
        //its new stage with new scene, so we need to load this file again
        editWindow.getScene().getStylesheets().add(getClass().getResource(CONSTANTS.CSS_PATH.string).toExternalForm()); 
        UserData userData = (UserData)usersTable.getSelectionModel().getSelectedItem();
        UserEditWindowController userEditWindowController = loader.getController();
        userEditWindowController.fillWithData(userData);
        editWindow.initModality(Modality.APPLICATION_MODAL);
        editWindow.showAndWait();
    } catch (IOException exception) {
        ExceptionDialog exceptionDialog = new ExceptionDialog("Couldn't load UserEditWindow",exception);
        exceptionDialog.showAndWait();
    }

添加了这个 CSS 文件:

#greenInfoTip {
    -fx-graphic-text-gap: 20;
    -fx-font-size: 44.0px ;
    -fx-background: green;
    -fx-background-color: rgb(128,255,128);
}

我尝试在根 Controller 类中执行此操作:

infoTip.setId("greenInfoTip");

但没有效果。我做错了什么吗?

编辑 ItachiUchiha 正是它的样子:

方法在 UserEditWindowController 类中:

@FXML
    private void buttSaveAction(ActionEvent event){
        //check if login and password doesn't contain spaces and is 4-16 characters long
        String login = textFLogin.getText();
        String password = textFPassword.getText();
        boolean hasLoginWhiteSpace = isContainingWhiteSpace(login);
        boolean hasPasswordWhiteSpace = isContainingWhiteSpace(password);
        boolean isLoginMoreThan3 = (login.length() > 3)? true : false;
        boolean isLoginLessThan17 = (login.length() < 17)? true : false;
        boolean isPasswordMoreThan3 = (password.length() > 3)? true : false;
        boolean isPasswordLessThan17 = (password.length() < 17)? true : false;

        InfoTip infoTip = new InfoTip();
        if( hasLoginWhiteSpace == false){
            if( hasPasswordWhiteSpace == false ){
                if( isLoginMoreThan3 == true && isLoginLessThan17 == true){
                    if( isPasswordMoreThan3 == true && isPasswordLessThan17 == true ){
                        //========login and password are correct
                        String query = "UPDATE users SET login = ?, password = ? WHERE employee_id = ?;";
                        try( Connection connection = Main.dataSource.getConnection() ){
                            try( PreparedStatement preparedStatement = connection.prepareStatement(query) ){
                                preparedStatement.setString(1, login);
                                preparedStatement.setString(2, password);
                                preparedStatement.setInt(3, currentUser.getiD());
                                preparedStatement.executeUpdate();

                                currentUser.setLogin(login);
                                currentUser.setPassword(password);

                                infoTip.getInfoTip().setId("greenInfoTip");
                                infoTip.showTip((Button)event.getSource(), "Saved");
                            }
                        }catch(Exception exception){
                            ExceptionDialog exceptionDialog = new ExceptionDialog("Error while loading data from database",exception);
                            exceptionDialog.showAndWait();
                        };
                    }else{ //password has less than 4 or more than 16 characters
                        infoTip.showTip(textFPassword, "no more than 16 and no less than 4 characters!");
                    }
                }else{ //login has less than 4 or more than 16 characters
                    infoTip.showTip(textFLogin, "no more than 16 and no less than 4 characters!");
                }
            }else{ //password has white space
                infoTip.showTip(textFPassword, "no spaces!");
            }
        }else{ //login has white space
            infoTip.showTip(textFLogin, "no spaces!");
        }
    }




public class InfoTip {
    private Tooltip infoTip;


    public InfoTip(){
        infoTip = new Tooltip();
    }

    private static Point2D getNodePos(Node node){  //getting node coordination on screen
        Scene nodeScene = node.getScene();
        final Point2D windowPos = new Point2D(nodeScene.getWindow().getX(),  nodeScene.getWindow().getY());
        final Point2D scenePos = new Point2D(nodeScene.getX(), nodeScene.getY());
        final Point2D nodePos = node.localToScene(0.0, 0.0);
        final Point2D nodePosOnScreen = new Point2D(windowPos.getX() + scenePos.getX() + nodePos.getX(),
                                                    windowPos.getY() + scenePos.getY() + nodePos.getY());
        return nodePosOnScreen;
    }
    public void showTip(Node node, String text){
        Point2D nodePosOnScreen = getNodePos(node);

        infoTip = new Tooltip(text);
        infoTip.setFont(new Font(15));
        infoTip.setOpacity(0.9);
        infoTip.setAutoFix(true);
        infoTip.setAutoHide(true);
        infoTip.show(node, nodePosOnScreen.getX()-30, nodePosOnScreen.getY() - 40);
    }

    public Tooltip getInfoTip(){
        return infoTip;
    }
}

最佳答案

问题在于

infoTip = new Tooltip(text); 

howTip(Node node, String text) 中。

您正在用一个新对象覆盖带有 id 的旧工具提示。尝试使用

infoTip.setText(text);

关于css - 使用 CSS 更改节点设计,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29551935/

相关文章:

javascript - 鼠标悬停时显示框

java - 内容出现在自定义单元格工厂的空行中

java - TableView 项目未删除但不响应

c# - 如何为 asp.net 中的每个项目添加复选框列表的工具提示

javascript - 在 angularjs 中点击时制作工具提示

JavaFX ComboBox 仅显示箭头

javascript - 如何使用 CSS 和/或 Javascript 实现这种效果(换行符)

html - CSS 下拉菜单/容器?

CSS - flex 布局,1 个流体 + 1 个固定列,居中对齐

html - float div 在其他 div 之上