2014-12-01 173 views
0

我有一個包含視圖的BorderLayout。在左側和右側有兩個ImageViews。我通過另一個線程通過ObejctProperty設置圖像。 TypedImage只是javaFXs Image的容器。JavaFX ImageView不在BorderLayout中顯示圖像

@Override 
public void changed(ObservableValue<? extends TypedImage> observable, 
     TypedImage oldValue, 
     TypedImage newValue) { 
    switch (newValue.getTyp()) { 
    case TYPE1: 
     Platform.runLater(new Runnable() { 
      @Override 
      public void run() { 
       imageView1.setImage(newValue.getImage()); 
      } 
     }); 
    case TYPE2: 
     Platform.runLater(new Runnable() { 
      @Override 
      public void run() { 
       imageView2.setImage(newValue.getImage()); 
      } 
     }); 
     break; 
    default: 
     break; 
    } 
} 

由於監聽器正在監聽兩個ObjectProperties,所以改變的方法通常會調用兩次。當第一次調用方法時,匹配的ImageView(取決於類型)將被更新並顯示UI中的圖像。當方法從第二個屬性第二次調用時,對於第二個ImageView。從這一點只是最後更新的ImageView在用戶界面中顯示圖像,第一個更新的ImageView是不可見或空的..任何人都可以幫助我理解和解決這個問題嗎?

Java的Implementaion

public class MainApp extends Application { 
    private ObjectProperty<TypedImage> image1 = new SimpleObjectProperty<TypedImage>(); 
    private ObjectProperty<TypedImage> image2 = new SimpleObjectProperty<TypedImage>(); 

    public vois start(){ 
    [..] 
    image1.addListener(mainView.getController()); 
    image2.addListener(mainView.getController()); 
    { 
     Image1Drawer drawer = new Image1Drawer(); 
     drawer.setParent(this); 
     Thread t = new Thread(drawer); 
     t.start(); 
    } 
    { 
     Image2Drawer drawer = new Image2Drawer(); 
     drawer.setParent(this); 
     Thread t = new Thread(drawer); 
     t.start(); 
    } 
    [..] 
    } 

    public void notifyDrawerFinish(AbstractDrawer source, TypedImage img){ 
    if(source instanceof Image1Drawer){ image1.setValue(img); } 
    if(source instanceof Image2Drawer){ image2.setValue(img); } 
    } 
} 

/** 
    * extended by Image1Drawer and Image2Drawer, run-methode draws stuff on image 
    */ 
public abstract class AbstractDrawer implements Runable { 
    private MainApp parent; 
    private void fireFinish(TypedImage img){ 
    mainApp.notifyDrawerFinish(this,img); 
    } 
    public void setParent(MainApp parent){ 
    this.parent = parent; 
    } 
} 

public class MianViewController implements Initializable, ChangeListener { 
    @FXML 
    private ImageView imageView1; 
    @FXML 
    private ImageView imageView2; 

    [..] 
    public void changed(ObservableValue<? extends TypedImage> observable, 
     TypedImage oldValue, 
     TypedImage newValue) { 
    /* see above */ 
    } 
    [..] 
} 

FXML

<?xml version="1.0" encoding="UTF-8"?> 

<?import javafx.scene.image.*?> 
<?import java.lang.*?> 
<?import javafx.scene.layout.*?> 
<?import javafx.scene.layout.AnchorPane?> 

<BorderPane maxHeight="-Infinity" maxWidth="-Infinity" 
    minHeight="-Infinity" minWidth="-Infinity" prefHeight="600.0" 
    prefWidth="800.0" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1" fx:controller="org.nemo.view.MainViewController">> 
    <left> 
     <ImageView fx:id="imageView1" fitHeight="600.0" fitWidth="50.0" 
      pickOnBounds="true" preserveRatio="true" BorderPane.alignment="CENTER" /> 
    </left> 
    <right> 
     <ImageView fx:id="imageView2" fitHeight="600.0" fitWidth="50.0" 
      pickOnBounds="true" preserveRatio="true" BorderPane.alignment="CENTER" /> 
    </right> 
</BorderPane> 
+0

你可以發佈[MCVE](http://stackoverflow.com/help/mcve),以便我可以更好地理解你的問題嗎? – ItachiUchiha 2014-12-01 18:07:31

回答

0

嘗試將控制器的路徑在FXML文件。我想你錯過了:

<BorderPane maxHeight="-Infinity" maxWidth="-Infinity" 
minHeight="-Infinity" minWidth="-Infinity" prefHeight="600.0" 
prefWidth="800.0" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1" fx:controller="YOUR_PACKAGE_NAME.MianViewController"> 
+0

Soory我忘了複製這個..它沒有效果 – Nemo 2014-12-03 08:52:20