2013-05-26 32 views
1

喜想和大家分享我的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){} 
+0

而不是發佈您的問題的解決編輯的問題,並刪除它,而是發表您的解決方案作爲一個答案,並標記它是正確的。請參閱[我能回答自己的問題嗎?](http://meta.stackexchange.com/questions/17463/can-i-answer-my-own-questions-even-those-where-i-knew-the-answer - 詢問) – jewelsea

+0

一個bug。。我不確定是否允許我這樣做。 根據要求更改。 – djpalme

回答

1

我能找到唯一的解決方法是通過一個濫用JSplitPane的(setDivider(0),並設置任一側作爲調用setVisible(假)):示例代碼如下。

CustomFXPanel pn1 = new CustomFXPanel(); //JPanel containing JavaFX Chart 
CustomPanel pn2 = new CustomPanel();  //Extends JPanel 
JSplitPane spContent; 
… 
/** 
* Constructor 
*/ 
public MyJFrame(){ 
    init(); //Set up JFrame Size, title etc. 
    spContent.setDividerSize(0);  //Hide the Divider 
    spContent.setLeftComponent(pn1);  
    spContent.setLeftComponent(pn2); 
    pn1.setVisible(true);    //By default show pn1 
    pn2.setVisible(false);    //Hide this Panel. JSplitPane will adjust itself (either left or right will take up all space). 

    setLayout(new GridLayout(1,1)); //Show the Content all across the Frame 
    add(spContent); 
}  

/** 
* Changes Mode of Application 
* @param showFX: True = show fxChart, False = show other Panel 
*/ 
public void changePanel(boolean showFX){ 
    if(showFX){ //Show FX Panel 
     pn1.setVisible(true); //spContent will adjust itself 
     pn2.setVisible(false); 
    }else{ //Show normal Panel 
     pn1.setVisible(false); 
     pn2.setVisible(true); 
    } 
} 

如果您有任何人找到更優雅的解決方案,請隨時寫下您的答案。 我在這裏寫了我自己的解決方法,因爲我認爲,我不是唯一一個有此問題的人。

我希望我可以幫忙。 乾杯