2016-08-12 13 views
0

CellFactoryを使用してTableView内でSVGイメージをレンダリングする際に問題が発生しています。JavaFX - SVGパスをTableView内で右にサイズ変更する方法

ここでこのコードを使用していますが、動作しません.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

関連する問題