簡體   English   中英

從JavaFX的階段的實例訪問UI組件

[英]Accessing UI Components from an INSTANCE of a stage in JavaFX

我正在使用Scene Builder和Netbeans IDE在javaFX中編寫notetaker軟件。

我希望用戶能夠打開無限數量的(記筆記)窗口(就像在記事本之類的應用程序中一樣),然后單擊“全部保存”按鈕,然后從每個窗口的文本區域讀取。

我當前的問題是訪問這些文本區域。

項目中有兩個FXML文件,其中一個是注釋的通用模板(但是用戶選擇要打開多少文件)。

幫助將不勝感激!

到目前為止,我已經能夠毫無問題地啟動這些無限記錄窗口。 我有兩個控制器類,在一個類中有一個getText()方法與FXMLTemplate相對應(一個用於注釋本身,而不是與主屏幕相對)。

另一個類包含與啟動(無限)注釋相關的代碼,將每個新階段存儲在稱為“ arrayOfStages”的全局數組列表中。 我還擔心階段的arraylist是否包含(引用到)這些階段本身的實例,而不是對靜態模板類的相同引用的副本)。 是這樣嗎 如果有人可以為我澄清這一點,那就太好了!

我已經嘗試過getText()路由和訪問textarea路由,但到目前為止都失敗了(即,引用控制器類的特定實例與從舞台實例訪問UI元素(存儲在數組中))。 到目前為止,我都失敗了:(

讓我知道代碼是否會有所幫助,我將在此處發布。 衷心感謝您能提供的任何幫助!

:)

因此,這是負責啟動理論上無限數量的notetaker窗口的部分:

 public void newNote() {//intuntitlednotenumber //hold an global array of Stage objects (the note windows themselves), to access those stages try{ Parent root = FXMLLoader.load(getClass().getResource("Template.fxml")); Scene scene = new Scene(root); Stage aStage = new Stage(); aStage.setScene(scene); arrayOfStages.add(aStage); aStage.show(); } catch(Exception e) { } //array of "new notes" ie stages } } 

這是我嘗試保存這些內容的部分(假設實例本身存儲在數組中,我需要幫助參考位於舞台上的錨定窗格(希望如此……但我不確定)我需要幫助的部分在這里:

 for(int i = 0; i < arrayOfStages.size(); i++) { try{ AnchorPane frame = arrayOfStages.get(i).getScene().getRoot().get....//<--- I'm not really sure what to do here, or if this technique will really work...ie. get me the INSTANCE of the anchorpane from the INSTANCE of the stage, and in turn the textarea Node n = frame.getChildren().get(0); TextArea a = (TextArea) n; System.out.println(a.getText()); //this print statement is for testing purposes, later on, I will store the extracted text in a textfile } catch(Exception e){System.out.println(e);} 

我不太確定該從哪里去。 在這一點上,我已經嘗試了很多荒謬的事情,但我再也無法保持彼此直截了當了:

這種方法是正確的還是在實例化方面存在缺陷? 如何使用數組中的舞台實例來引用錨定窗格?

如果有人感興趣,以下是解決方案:

Node node1 = arrayOfStages.get(i).getScene().getRoot();
AnchorPane ap = (AnchorPane) node1;
Node n =...

其余與問題中的代碼相同。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM