喜想和大家分享我的Warkaround到已採取了我時間來解決,因爲我無法找到任何簡單的答案的一個問題。隱藏和顯示JavaFX的圖表在Swing
我執行的是視其狀態的Swing應用程序,無論是顯示了JavaFX的圖表(火線圖)或其他一些內容(對我來說,這是一個JFreeChart的)。 我是添加和刪除面板到我的用戶界面,但對大多數內容工作正常。
當使用JavaFX,我的內容不會顯示出來,一旦它已經可見,然後隱藏。
參見下面非工作示例代碼:
CustomFXPanel pn1 = new CustomFXPanel(); //JPanel containing JavaFX Chart
CustomPanel pn2 = new CustomPanel(); //Extends JPanel
JPanel pnContent; //PlaceHolder for either Panel
/**
* Constructor
*/
public MyJFrame(){
init(); //Set up JFrame Size, title etc.
setLayout(new GridLayout(1,1)); //Show the Content all across the Frame
pnContent = pn1;
add(pnContent);
}
/**
* Changes Mode of Application
* @param showFX: True = show fxChart, False = show other Panel
*/
public void changePanel(boolean showFX){
if(showFX){ //Show FX Chart
if(!pnContent.equals(pn1)){
remove(pnContent);
pnContent = pn1;
add(pnContent);
}
}else{ //Show other Panel
if(!pnContent.equals(pn2)){
remove(pnContent);
pnContent = pn2;
add(pnContent);
}
}
}
問題: 它將顯示在啓動時罰款。但是,當切換到pn2然後返回到pn1時,pn1內部的JFXPanel將不會顯示。
我得到了它由內而外PN1回顧myFXPanel.setStage(新階段(myJFXChart))工作。然而,這總是拋出一個IllegalArgumentsException,...「已經設置爲另一個場景的根目錄」。 - 它的工作,但我認爲有例外飛行是醜陋的壞習慣。
煩人,處理這個異常的任何企圖將導致面板中不再顯示出來。 這包括:
JFXPanel myFXPanel = new JFXPanel();
LineChart<Number, Number> chart;
....
//Inside reload method
//With parts inside then outside Platform.runLater(new Runnable()) {...}
myFXPanel.invalidate();
myFXPanel.removeAll();
try{
setStage(newStage(chart));
}catch(Exception ex){}
而不是發佈您的問題的解決編輯的問題,並刪除它,而是發表您的解決方案作爲一個答案,並標記它是正確的。請參閱[我能回答自己的問題嗎?](http://meta.stackexchange.com/questions/17463/can-i-answer-my-own-questions-even-those-where-i-knew-the-answer - 詢問) – jewelsea
一個bug。。我不確定是否允許我這樣做。 根據要求更改。 – djpalme