Search code examples
csstooltipjavafx-8

using CSS to change node design


I dont know what I'am doing wrong with adding CSS style to node.

I have a main application window. Then I click menu item and another modal window opens as below(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();
    }

this CSS file is added:

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

And I try to do this in root controller class:

infoTip.setId("greenInfoTip");

but with no effect. Is there something I'm doing wrong?

EDIT ItachiUchiha here is precisely what it looks like:

method is inside UserEditWindowController class:

@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;
    }
}

Solution

  • The issue is with

    infoTip = new Tooltip(text); 
    

    inside howTip(Node node, String text).

    You are over-writing the old tooltip with id with a new object. Try using

    infoTip.setText(text);