JavaFX - 如何在TableView中调整SVG路径的大小 [英] JavaFX - How to resize a SVG Path right in a TableView

查看:226
本文介绍了JavaFX - 如何在TableView中调整SVG路径的大小的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在使用CellFactory在TableView中呈现SVG图像时遇到问题。



我在这里使用此代码,但它不起作用,svg图像已缩放,但不会调整大小。

  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;

公共类SVGTable扩展Application {
private ObservableList< SVGExample>例子;

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)抛出异常{
AnchorPane pane = new AnchorPane();
场景场景=新场景(窗格);
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();
}

公共类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);
}
}

公共类PingCell扩展TableCell< SVGExample,Integer> {
private HBox hBox = new HBox();
私人标签;
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);
}
}
}
}

之后运行,它看起来像这样:



解决方案

您可以将Shape包装在一个组中以强制重新设置布局边界大小。

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

Scale是一种变换,根据


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:

解决方案

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.

这篇关于JavaFX - 如何在TableView中调整SVG路径的大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆