JavaFX - How to resize a SVG Path right in a TableView
Asked Answered
S

1

0

I'm having issues to render a SVG Image in a TableView with a CellFactory.

Im using this code here, but it don't work, the svg image is scaled, but it don't resize.

import javafx.application.Application;
import javafx.beans.property.IntegerProperty;
import javafx.beans.property.SimpleIntegerProperty;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Label;
import javafx.scene.control.TableCell;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;
import javafx.scene.control.cell.PropertyValueFactory;
import javafx.scene.layout.AnchorPane;
import javafx.scene.layout.HBox;
import javafx.scene.shape.SVGPath;
import javafx.scene.shape.Shape;
import javafx.stage.Stage;

public class SVGTable extends Application {
    private ObservableList<SVGExample> examples;

    public static void main(String[] args) {
        launch(args);
    }

    public SVGTable() {
        examples = FXCollections.observableArrayList();
        examples.addAll(new SVGExample(289),
                new SVGExample(42),
                new SVGExample(120));
    }

    @Override
    public void start(Stage primaryStage) throws Exception {
        AnchorPane pane = new AnchorPane();
        Scene scene = new Scene(pane);
        TableView<SVGExample> tableView = new TableView<>();
        tableView.setMinWidth(500);
        tableView.setMinHeight(400);
        tableView.setItems(examples);
        final TableColumn<SVGExample, Integer> ping = new TableColumn<>("Ping");
        ping.setCellValueFactory(new PropertyValueFactory<>("ping"));
        ping.setCellFactory(param -> new PingCell());
        tableView.getColumns().add(ping);
        pane.getChildren().add(tableView);
        primaryStage.setScene(scene);
        primaryStage.show();
    }

    public class SVGExample {
        private final IntegerProperty ping = new SimpleIntegerProperty();

        public SVGExample(int ping) {
            setPing(ping);
        }

        public int getPing() {
            return ping.get();
        }

        public IntegerProperty pingProperty() {
            return ping;
        }

        public void setPing(int ping) {
            this.ping.set(ping);
        }
    }

    public class PingCell extends TableCell<SVGExample, Integer> {
        private HBox hBox = new HBox();
        private Label label;
        private int oldValue;

        private PingCell() {
            label = new Label();
            hBox.setAlignment(Pos.CENTER_LEFT);
            oldValue = Integer.MIN_VALUE;
        }

        @Override
        protected void updateItem(final Integer item, final boolean empty) {
            if (item != null) {
                label.setText(item + "ms");
                int i = (item + 50) / 100;
                if (i < 1)
                    i = 1;
                if (4 < i)
                    i = 4;
                if (i != oldValue) {
                    SVGPath svgPath1 = new SVGPath();
                    svgPath1.setContent("M149.2,8.3L127-13.9c42.4-42.4,98.7-65.8,158.5-65.8c59.8,0,116.1,23.4,158.5,65.8L421.8,8.3c-36.5-36.5-84.9-56.6-136.3-56.6C234.1-48.2,185.7-28.1,149.2,8.3z");
                    SVGPath svgPath2 = new SVGPath();
                    svgPath2.setContent("M190.9,50.1l-22.2-22.2C200-3.4,241.4-20.6,285.5-20.6c44.1,0,85.5,17.2,116.8,48.4l-22.2,22.2c-25.3-25.3-58.9-39.2-94.6-39.2C249.8,10.8,216.2,24.8,190.9,50.1z");
                    SVGPath svgPath3 = new SVGPath();
                    svgPath3.setContent("M232.7,91.8l-22.2-22.2c20.1-20.1,46.7-31.1,75-31.1s55,11.1,75,31.1l-22.2,22.2c-14.1-14.1-32.9-21.9-52.8-21.9C265.6,69.9,246.8,77.7,232.7,91.8z");
                    SVGPath svgPath4 = new SVGPath();
                    svgPath4.setContent("M285.5,98.1c-12.8,0-24.5,5.2-32.9,13.6l32.9,32.9l32.9-32.9C310,103.3,298.3,98.1,285.5,98.1z");
                    Shape s = SVGPath.union(SVGPath.union(SVGPath.union(svgPath1, svgPath2), svgPath3), svgPath4);
                    s.setScaleX(0.1);
                    s.setScaleY(0.1);
                    hBox.getChildren().clear();
                    hBox.getChildren().addAll(s, label);
                }
                setGraphic(hBox);
            }
        }
    }
}

After run, it's look like this:

enter image description here

Selimah answered 12/8, 2016 at 8:39 Comment(1)
You are missing super.updateItem(item, empty);Phenformin
N
0

You can wrap the Shape in a Group to force re-size of layout bounds.

hBox.getChildren().addAll(new Group(s), label);

Scale is a type of transform and according to Javadocs:

Any transform, effect, or state applied to a Group will be applied to all children of that group. Such transforms and effects will NOT be included in this Group's layout bounds, however if transforms and effects are set directly on children of this Group, those will be included in this Group's layout bounds.

enter image description here

Naoise answered 12/8, 2016 at 9:15 Comment(1)
Thank you, for the solution and for the editing. Why is a Group needed? Why it don't resize automatic?Selimah

© 2022 - 2024 — McMap. All rights reserved.