2016-08-12 71 views
0

我在使用CellFactory在TableView中呈現SVG圖像時遇到了問題。JavaFX - 如何在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; 

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

運行後,它看起來像這樣:

enter image description here

+0

你缺少'super.updateItem(項目,空);' –

回答

0

你可以用形狀在組強制佈局界限的重新大小。

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

量表是一種類型的變換,並根據Javadocs的:

任何變換,效果或狀態應用於一組將被應用到該組的所有兒童。此類轉換和效果不會包含在本組的佈局範圍內,但如果轉換和效果直接針對本集團的子女設置,則這些轉換和效果將包含在本組的佈局範圍內。

enter image description here

+0

謝謝你,爲解決方案和編輯。爲什麼需要一個組?爲什麼它不能自動調整大小? – Eisphoenix

+0

是否可以設置固定寬度? – Eisphoenix

+0

@Eisphoenix查看http://stackoverflow.com/a/38960929/1759128 – ItachiUchiha