0

I m trying to change the representation of the rating column of my tableView ( please look at picture 1 )
enter image description here
into rating stars of jfoenix library for better representation
here is the code (in Controller) of my table view :

@FXML
    public void initialize(URL url, ResourceBundle rb)  {

        
        nom_client.setCellValueFactory(new PropertyValueFactory<Review, String>("nom_client_review"));
        date_review.setCellValueFactory(new PropertyValueFactory<Review, String>("date_review")); 
        descrip_review.setCellValueFactory(new PropertyValueFactory<Review, String>("description_review"));
        rating.setCellValueFactory(new PropertyValueFactory<Review, Float>("rating"));
        
        try {
            ObservableList<Review> reviewlist = ReviewCrud.getAllRecords();
            
            
            populateTable(reviewlist);
            

        } catch (ClassNotFoundException ex) {
            Logger.getLogger(AfficherReview2Controller.class.getName()).log(Level.SEVERE, null, ex);
        } catch (SQLException ex) {
            Logger.getLogger(AfficherReview2Controller.class.getName()).log(Level.SEVERE, null, ex);
        }

    }
private void populateTable(ObservableList<Review> reviewlist) {
        reviewtable.setItems(reviewlist);
    }

please note that i m not allowed to use simplefloatproperty, that why my rating in the Review class is set to float
below the code that i use to get data from my DB:

    public static ObservableList<Review> getAllRecords() throws ClassNotFoundException, SQLException {

        Connection cnnx = ReviewConnection.getInstance().getCnx();
        String requete = "SELECT * FROM review_client ";
        try {
            Statement st = cnnx.createStatement();
            ResultSet rs = st.executeQuery(requete);

            ObservableList<Review> reviewList = getReviewObjects(rs);
            return reviewList;
        } catch (SQLException ex) {
            System.err.println(ex.getMessage());
            throw ex;
        }
    }

    private static ObservableList<Review> getReviewObjects(ResultSet rs) throws ClassNotFoundException, SQLException {

        try {
            ObservableList<Review> reviewList = FXCollections.observableArrayList();
            while (rs.next()) {
                Review r = new Review();
                r.setNom_client_review(rs.getString("nom_client_review"));
                r.setDate_review(rs.getString("date_review"));
                r.setDescription_review(rs.getString("description_review"));
                r.setRating(rs.getFloat("rating"));
                reviewList.add(r);
            }
            return reviewList;
        } catch (SQLException ex) {
            System.err.println(ex.getMessage());
            throw ex;
        }
    }

my only goal is to convert the float values of the rating columns into stars of jfoenix
if anyone knows how please help me
will be very appreciated

  • 1
    Please post [mre] and add a link to the documentation of Jfoenix stars. – c0der Mar 06 '21 at 09:07
  • i m kinda new to javafx , i m still trying to figure out what to post , sorry i will try to edit the post –  Mar 06 '21 at 09:10
  • stick to java naming conventions when showing code publicly, please (you mostly do, except for the underscores :) – kleopatra Mar 06 '21 at 11:51

1 Answers1

-1

have you looked at this solution: Turn a number into star rating display using jQuery and CSS

$.fn.stars = function() {
    return $(this).each(function() {
        // Get the value
        var val = parseFloat($(this).html());
        // Make sure that the value is in 0 - 5 range, multiply to get width
        var size = Math.max(0, (Math.min(5, val))) * 16;
        // Create stars holder
        var $span = $('<span />').width(size);
        // Replace the numerical value with stars
        $(this).html($span);
    });
}

This code is in jQuery but you can easily follow the logic.

Sizons
  • 640
  • 2
  • 8
  • 24
  • and what if i dont have know jQuery :/ –  Mar 06 '21 at 09:11
  • The point is that you use r.setRating(rs.getFloat("rating")); 's value (val) to first make sure the value ranges between 0 and 5, a size variable that you must create will have the number of stars between the range (use the math calc as is). Finally use your star image to where it says span, size indicating how many stars. – Sizons Mar 06 '21 at 09:16
  • this has no relation at all to javafx, not even to java ;) – kleopatra Mar 06 '21 at 11:49