0

I have an open source application that use JTabbedPane to show its contents. Look at this screenshot:

http://i46.tinypic.com/4rrs6c.jpg

The main content has a JSplitPane where at the left side is TreeMenu object and at right side is JTabbedPane object.

When I clicked a menu from the left panel, it will display the menu contents in JTabbedPane by using JTabbedPane.addTab() method:

tabbedPane.addTab(menu.getTitle(), menu.getAPanel());

I need to know how to do this: when I clicked a menu that has an associated tab already opened, it will not add a new tab, instead it should give focus to the already opened tab.

For example, you can see in the screenshot, I had already opened Cost Type tab. Now, if I click the Cost Type menu again, it should bring focus to the Cost Type tab, instead of adding new Cost Type tab.

The menu itself has an unique id field (menu.getId()). However, every time I clicked the menu, the menu.getAPanel() always returned a new instance of APanel. That means if I clicked Cost Type menu two times, the APanel returned from menu.getAPanel() from first click and second click will never be a same instance.

Anyone can help?

UPDATE:
Alright, it seems this question is confusing other people and honestly, I'm not a native english speaker, that's why I had a hard time to explain my problem and I had found the solution anyway (thanks to MadProgrammer), so I'm planning to delete this question as I think it will not benefit the others anymore, but to honor MadProgrammer, I will keep this question open for two days to give him a chance submitting his answer and get his deserved reputation.

Only for a little curious one:
This case has connection with my previous question. In original source code, when the menu is clicked, it actually displays a pop-up window. I'm trying to change the flow to: when the menu is clicked, the pop-up window is hidden and the pop-up window's content panel (APanel) is fetched and transferred into JTabbedPane as tab's component.

Glorfindel
  • 21,988
  • 13
  • 81
  • 109
null
  • 8,669
  • 16
  • 68
  • 98
  • 1
    *"two tabs or more can have same title/icon"* Sounds very confusing to the user. My advice, change that or explain this extraordinary presentation in a way that makes sense. – Andrew Thompson Oct 18 '12 at 08:24
  • 2
    You could maintain a active map which maps some kind of unique key for each menu item to its active tab. If the key doesn't exist, then the tab does not exist. – MadProgrammer Oct 18 '12 at 08:28
  • @Andrew: is it the "/" ? In my country, "/" can mean "or". Anyway, I fixed it, don't know if it's still confusing for you or others. – null Oct 18 '12 at 09:08
  • @MadProgrammer: hi, we meet again :). So is that the best solution? Is there other alternative solution that doesn't depend on other object? – null Oct 18 '12 at 09:13
  • You need some mechanism that unquily identifies each tab back to the menu item that created it. Given the fact that you say it you can rely on the title or icon, you need some other key, the menu item seems the logical choice. Is it the best? Can't say, it's what I can think of at the time – MadProgrammer Oct 18 '12 at 09:25
  • don't quite understand the problem here: you simply need some means to uniquely identify a tab. There are tons of options that have nothing to do with Swing, just plain basic (f.i. you could simply use a Map yourFunctionality/tabInstance). Or use the component's name property, or a clientProperty ... – kleopatra Oct 18 '12 at 10:07
  • @MadProgrammer: alright, I've made it using HashMap. I put the menu id as the key and menu.getAPanel() as the value. If you want to get increased reputation, you better hurry made an answer now :) – null Oct 18 '12 at 12:23
  • @all: sorry for all, I don't know what's the correct title for this case. It's really hard to explain. If you can recommend a good title ^^, I would really appreciate it. – null Oct 18 '12 at 12:27

1 Answers1

5

Your menu already has all the information you need. Here's a quick mock:

final JTabbedPane tabbedPane = new JTabbedPane();
final JComboBox items = new JComboBox(new Object[] {
        new MyMenu("one"), new MyMenu("other"), new MyMenu("got it?")
});
Action action = new AbstractAction("showInTab") {

    @Override
    public void actionPerformed(ActionEvent e) {
        MyMenu selected = (MyMenu) items.getSelectedItem();
        int index = tabbedPane.indexOfComponent(selected.getMenuPanel());
        if (index < 0) {
            tabbedPane.addTab(selected.getName(), selected.getMenuPanel());
        } else {
            tabbedPane.setSelectedIndex(index);
        }
    }
};
items.setSelectedIndex(-1);
items.setAction(action);

Edit

These statements (in the question):

it will display the menu contents in JTabbedPane by using tabbedPane.addTab(menu.getTitle(), menu.getAPanel());

and in the comment:

That's why in my very original question (before I edited many times), I stated that I can't use indexOfComponent directly

are contradicting each other, as the following assertion holds:

int old = tabbedPane.getTabCount(); 
tabbedPane.addTab(somename, myTabContent)
assertEquals(old, tabbedPane.indexOfComponent(myTabContent));

Clean up one or the other ;-)

kleopatra
  • 51,061
  • 28
  • 99
  • 211
  • Thanks for your contribution. Actually, it's correct but it's not entirely correct too because the problem is a bit complex (if you read my 2nd previous question, you might understand. This is continuation of my work progress^^). That's why in my very original question (before I edited many times), I stated that I can't use indexOfComponent directly. In short, every time I clicked menu, even if the menu has already been clicked one, the returned panel from getMenuPanel() is always not a same instance. – null Oct 18 '12 at 13:07
  • then loop through all tabs, check if the panel is a child: it must be somewhere if already added ;-) – kleopatra Oct 18 '12 at 13:10
  • correction: "That's why in my very original question (before I edited many times), I stated that I can't use APanel as key and thus I can't use indexOfComponent directly to find and bringing focus on the existing tab". Well, If you're still interested to solve this problem, I can show you the full length code or I can show you the Map solution (for the latter, I prefer to let MadProgrammer does it first, because the idea is from him). – null Oct 18 '12 at 13:26