2013-07-26 18 views
2

經過多次嘗試和研究,我沒有設法從Pane獲得可見性事件。下面的示例似乎是我最好的嘗試,但它不起作用。 歡迎任何工作主張。如何在Pane上獲取JavaFX 2.0可見性事件?

import javafx.application.Application; 
import javafx.beans.value.ChangeListener; 
import javafx.beans.value.ObservableValue; 
import javafx.scene.Scene; 
import javafx.scene.control.Button; 
import javafx.scene.layout.Pane; 
import javafx.stage.Stage; 

public class VisibilityTestMain extends Application { 

public static void main(String[] args) { 
    VisibilityTestMain.launch(args); 
} 

@Override 
public void start(Stage stage) throws Exception { 
    Pane root = new Pane(); 
    ChangeListener<Boolean> visibilityListener = new ChangeListener<Boolean>() { 

     @Override 
     public void changed(ObservableValue<? extends Boolean> arg0, Boolean oldValue, Boolean newValue) { 
      System.out.println("####"); 
     } 
    }; 
    root.visibleProperty().addListener(visibilityListener); 

    Button button = new Button("Hello"); 
    button.setTranslateX(10); 
    button.setTranslateY(20); 
    root.getChildren().add(button); 

    stage.setScene(new Scene(root, 50, 50)); 
    stage.show(); 

} 

}

+0

我不認爲你將窗格添加到你的場景。我錯了嗎? – Fabinout

+0

@Fabinout通常我會,因爲按鈕在我運行時可見。它應該與行:'stage.setScene(新場景(根,50,50));' – Jakez

+0

是的,你是對的。 – Fabinout

回答

0

的問題是,你永遠不會改變你的窗格的可見性,從而永遠達不到你的聽衆。

試試這段代碼來代替:

import javafx.application.Application; 
import javafx.beans.value.ChangeListener; 
import javafx.beans.value.ObservableValue; 
import javafx.event.ActionEvent; 
import javafx.event.EventHandler; 
import javafx.scene.Scene; 
import javafx.scene.control.Button; 
import javafx.scene.layout.Pane; 
import javafx.stage.Stage; 
import org.slf4j.Logger; 
import org.slf4j.LoggerFactory; 

public class VisibilityTestMain extends Application { 

public static final Logger LOGGER = LoggerFactory.getLogger(VisibilityTestMain.class); 
public static void main(String[] args) { 
    VisibilityTestMain.launch(args); 
} 

@Override 
public void start(Stage stage) throws Exception { 
    final Pane root = new Pane(); 
    root.visibleProperty().addListener(new ChangeListener<Boolean>() { 
     @Override 
     public void changed(final ObservableValue<? extends Boolean> observableValue, final Boolean aBoolean, final Boolean aBoolean2) { 
      System.out.println("####"); 
      //To change body of implemented methods use File | Settings | File Templates. 
     } 
    }); 

    Button button = new Button("Hello"); 
    button.setTranslateX(10); 
    button.setTranslateY(20); 
    root.getChildren().add(button); 
    button.setOnAction(new EventHandler<ActionEvent>() { 
     @Override 
     public void handle(final ActionEvent actionEvent) { 
      root.setVisible(false); 
      try { 
       Thread.sleep(1000); 
      } catch (InterruptedException e) { 
       e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates. 
      } 
      root.setVisible(true); 
     } 
    }); 

    stage.setScene(new Scene(root, 50, 50)); 
    stage.show(); 

} 
} 

現在你可以看到你的窗格的Visible屬性被訪問您點擊按鈕,每一次。

+0

感謝您的回答,但第一次顯示呢? – Jakez

+0

@Jakez讓我們在這裏清楚,你無法首先到達可見性事件,是嗎? – Fabinout

+0

從嚴格的功能角度來看,我的需求非常簡單:它從所有新窗格可見性變化(從操作者的角度而不是「visibleProperty」角度)接收事件,從第一個到最後一個。過去我已經在其他圖書館使用過這種功能;我甚至實施了一些自己。不幸的是,它似乎沒有在JFX中正確執行,因爲第一次有效的顯示沒有提出。 – Jakez