2012-12-07 75 views
0

我想知道是否有一種方法或方法可以讓我顯示當前的JEditorPane。例如,我有一個JFrame,我可以創建幾個選項卡。每當創建一個選項卡時,都會創建一個新的JEditorPane對象,並且該選項卡中顯示該窗格的內容。我實現了一個ChangeListener,只要我打開一個新的,關閉一個或在選項卡之間導航,它就會使我獲得當前選項卡的索引。我想要做的是無論何時打開新的選項卡或導航到我想要獲取駐留在此選項卡上的當前JEditorPane對象。有什麼方法可以實現這一目標?獲取正在顯示的當前JEditorPane

對不起,如果這個問題有點含糊。

在此先感謝。

+0

將它存儲在一個變量..? – Brian

+0

@brian並不是我想要達到的目標。例如,如果我導航到不同的選項卡,則存儲JEditorPane對象的變量將存儲打開的最新選項卡中的一個,而不是該選項卡僅被導航到的那個變量。不知道這是否有道理。我想要的變量來存儲任何打開或導航到任何選項卡的JEditorPane。基本上任何當前正在顯示的選項卡。 –

+1

將它存儲在列表中? –

回答

1

做到這將是繼承JPanel和您的自定義JPanel添加到標籤面板,而不是最好的辦法:

public class EditorPanel extends JPanel { 

    private JEditorPane editorPane; 

    // ... 

    public EditorPanel() { 
     // ... 
     editorPane = new JEditorPane(...); 
     super.add(editorPane); 
     // ... 
    } 

    // ... 

    public JEditorPane getEditorPane() { 
     return editorPane; 
    } 
} 

添加一個新的標籤:

JTabbedPane tabbedPane = ... ; 
tabbedPane.addTab(name, icon, new EditorPanel()); 

然後當你需要使用選項卡窗格訪問它:

Component comp = tabbedPane.getComponentAt(i); 
if (comp instanceof EditorPanel) { 
    JEditorPane editorPane = ((EditorPanel) comp).getEditorPane(); 
} 

這是一個bett呃可以選擇維護一個單獨的列表並嘗試將其與標籤窗格的索引一起維護。

+0

謝謝,這是一個相當不錯的解決方案。我會嘗試這兩種解決方案,看看哪一個適合我現在更好的代碼。不過謝謝。 –