0

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.

Kakalokia
  • 3,191
  • 3
  • 24
  • 42
  • @brian Not really what I'm trying to achieve. For example if i navigate to a different tab, the variable where I store the JEditorPane object is storing the one of the newest tab that is opened, not the tab is is just been navigated to. Don't know if that makes any sense. I want to the variable to store the JEditorPane of any tab that is opened or navigated to. Basically any tab that is currently being displayed. – Kakalokia Dec 07 '12 at 18:29
  • @AliAlamiri Ah, I see, I'll write an answer for that. – Brian Dec 07 '12 at 18:35
  • @GilbertLeBlanc nice idea, just did that and it mostly works. There is just a small bug that I'll fix and hopefully everything will work fine. Thank you, I don't even know why I overlooked using a List – Kakalokia Dec 07 '12 at 18:42

1 Answers1

1

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.

Brian
  • 17,079
  • 6
  • 43
  • 66
  • Thank you, this is quite a nice solution. I'll try both solutions and see which one fits the code I have now better. But thanks anyway. – Kakalokia Dec 07 '12 at 18:55