Reputation: 3191
I was wondering if there is a way or a method that allows me to get the current JEditorPane being displayed. For example I have a JFrame where I can create several tabs. Whenever a tab is created a new JEditorPane object is created and the content of that pane are displayed in the tab. I've implemented a ChangeListener that currently just gets me the index of the current tab whenever I open a new one, close one or navigate between tabs. What I want to do is whenever a new tab is opened or navigated to I want to get the current JEditorPane object that resides at this tab. Is there any way in which I can achieve that?
Sorry if the question is a bit vague.
Thanks in advance.
Upvotes: 0
Views: 158
Reputation: 17319
The best way to do this would be to subclass JPanel
and add your custom JPanel
to the tabbed pane instead:
public class EditorPanel extends JPanel {
private JEditorPane editorPane;
// ...
public EditorPanel() {
// ...
editorPane = new JEditorPane( ... );
super.add(editorPane);
// ...
}
// ...
public JEditorPane getEditorPane() {
return editorPane;
}
}
Adding a new tab:
JTabbedPane tabbedPane = ... ;
tabbedPane.addTab(name, icon, new EditorPanel());
And then when you need to access it using the tabbed pane:
Component comp = tabbedPane.getComponentAt(i);
if (comp instanceof EditorPanel) {
JEditorPane editorPane = ((EditorPanel) comp).getEditorPane();
}
This is a better alternative to maintaining a separate list and trying to maintain it alongside the tabbed pane's indices.
Upvotes: 1