2015-01-29 3 views
2

하나의 단일 백업 모델 인스턴스에서 여러보기를 사용하기 위해 여러 차트를 동일한 데이터 속성에 바인딩하려고했습니다. 그러나 이상하게도 첫 번째 차트의 getData()가 기본 속성의 내용을 업데이트 한 후에도 예상 된 결과를 산출하기는하지만, 마지막으로 바인딩 된 차트 만 데이터를 표시합니다. 첫 번째 차트는 완전히 비어있는 것처럼 보입니다.JavaFX : 여러 차트를 단일 데이터 속성에 바인딩

나는 내 문제를 설명하기 위해 누군가 내 실수를 밝히고 올바르게 수행하는 방법을 보여주기를 희망하는 간단한 예를 썼다.

package javafx.databinding; 

import java.util.ArrayList; 

import javafx.application.Application; 
import javafx.application.Platform; 
import javafx.beans.property.Property; 
import javafx.beans.property.SimpleListProperty; 
import javafx.collections.FXCollections; 
import javafx.collections.ObservableList; 
import javafx.concurrent.Task; 
import javafx.scene.Scene; 
import javafx.scene.chart.PieChart; 
import javafx.scene.chart.PieChart.Data; 
import javafx.scene.layout.HBox; 
import javafx.stage.Stage; 

public class TestDataBinding extends Application 
{ 
    public static void main(String[] args) 
    { 
     launch(args); 
    } 

    @Override 
    public void start(Stage primaryStage) throws Exception 
    { 
     Property<ObservableList<Data>> sourceData = 
      new SimpleListProperty<Data>(FXCollections.observableList(new ArrayList<Data>())); 

     sourceData.getValue().add(new Data("first", 1)); 
     sourceData.getValue().add(new Data("second", 2)); 

     PieChart firstChart = new PieChart(); 
     PieChart secondChart = new PieChart(); 

     firstChart.dataProperty().bind(sourceData); 
     secondChart.dataProperty().bind(sourceData); 

     HBox layout = new HBox(); 
     layout.getChildren().addAll(firstChart, secondChart); 

     Scene scene = new Scene(layout, 500d, 200d); 
     primaryStage.setScene(scene); 
     primaryStage.show(); 

     System.out.println(firstChart.getData()); 
     System.out.println(secondChart.getData()); 

     Platform.runLater(new Task<Void>() 
     { 
      @Override 
      protected Void call() throws Exception 
      { 
       Thread.sleep(5000); 

       sourceData.getValue().get(0).setPieValue(5); 
       sourceData.getValue().get(1).setPieValue(3); 

       System.out.println(firstChart.getData()); 
       System.out.println(secondChart.getData()); 

       return null; 
      } 
     }); 
    } 
} 

으로는

[Data[first,1.0], Data[second,2.0]] 
[Data[first,1.0], Data[second,2.0]] 
[Data[first,5.0], Data[second,3.0]] 
[Data[first,5.0], Data[second,3.0]] 

에 로깅 결과를 제외하지만 차트

PieChart.Data 클래스는입니다 ... this

답변

0

이 참으로 아주 나쁘게 자바 FX에 의해 설명되어 있습니다처럼 데이터와보기의 혼합. 예를 들어, 범례는 데이터 클래스 내에 Text 노드로 저장됩니다. 아시다시피 Node은 하나의 Parent 요소에만 포함될 수 있습니다.

다음 변형 예는 잘 작동 :

@Override 
public void start(Stage primaryStage) throws Exception { 

    IntegerProperty first = new SimpleIntegerProperty(1); 
    IntegerProperty second = new SimpleIntegerProperty(1); 

    Data d11 = new Data("first", 0); 
    d11.pieValueProperty().bind(first); 
    Data d12 = new Data("first", 0); 
    d12.pieValueProperty().bind(second); 
    ObservableList<Data> sourceData1 = FXCollections.observableArrayList(d11,d12); 

    Data d21 = new Data("first", 0); 
    d21.pieValueProperty().bind(first); 
    Data d22 = new Data("first", 0); 
    d22.pieValueProperty().bind(second); 
    ObservableList<Data> sourceData2 = FXCollections.observableArrayList(d21,d22); 

    PieChart firstChart = new PieChart(sourceData1); 
    PieChart secondChart = new PieChart(sourceData2); 

    HBox layout = new HBox(); 
    layout.getChildren().addAll(firstChart, secondChart); 

    Scene scene = new Scene(layout, 500d, 200d); 
    primaryStage.setScene(scene); 
    primaryStage.show(); 

    System.out.println(firstChart.getData()); 
    System.out.println(secondChart.getData()); 

    new Thread(new Task<Void>() { 
     @Override 
     protected Void call() throws Exception { 
      Thread.sleep(5000); 

      first.set(5); 
      second.set(3); 

      System.out.println(firstChart.getData()); 
      System.out.println(secondChart.getData()); 

      return null; 
     } 
    }).start(); 
} 
관련 문제