Plugin - Adding Chat Toolbar Buttons - Example is wrong?

I’ve been trying to get my plugin to properly add buttons to ToolBar in chat windows. I followed the example code shown in the dev guide:

/**
     * Adds a button to each Chat Room that is opened.
     */
    private void addChatRoomButton(){
        // Retrieve ChatManager from the SparkManager
        ChatManager chatManager = SparkManager.getChatManager();         // Create a new ChatRoomButton.
        final ChatRoomButton button = new ChatRoomButton("Push Me");         // Add to a new ChatRoom when the ChatRoom opens.
        chatManager.addChatRoomListener(new ChatRoomListenerAdapter() {
            public void chatRoomOpened(ChatRoom room) {
                room.getToolBar().addChatRoomButton(button);
            }             public void chatRoomLeft(ChatRoom room) {
                room.getToolBar().removeChatRoomButton(button);
            }
        });
    }

And yes, if you run that, it does add the example button to the first chat room that you open… so it looks like the code works.

But then if you open another chat room, it will add the button to the new chat room, but it will no longer show up on the first chat room’s toolbar. Adding further chat rooms does the same thing. The button will always appear on the most recently added chat room, but none of the others.

What’s going on here? If this method isn’t the correct way to add buttons to the toolbar (and keep them there), then what is?

I’m using Spark 2.5.8 and Java 1.5.

Oh, also, I had one more question in that example…

The second part, where it removes the button on chatRoomLeft… is that necessary? Do you need to explicitly remove your buttons? Won’t they just be disposed of when the jpanel tab they’re on is closed?

I’m not totally sure I understand the difference between chatRoomLeft and chatRoomClosed.

Does “left” mean that you are no longer in the chat room, but the room’s tab is still open with contents displayed (like on a network disconnect) and “closed” is when the tab is actually closed? This all seems a bit vague in the documentation.

Ok, I think I understand what’s going on here…

Each time the “chatRoomOpened” method is called, it adds the button to the new chat room, which then sets the toolbar on that tab as that component’s parent. The problem is, because it’s a “final” variable (so that the inner class can reference it), the method is always referring to the same instance of the button. A component can’t have multiple parents, so each time it’s assigned to a new tab, it nukes the reference to the previous tab.

So, my question still stands…

What is the right way to be adding buttons to tabs?

(While still maintaining a reference to the button so that methods like “chatRoomLeft” can still do stuff to it.)

Ok, really? No one has any opinion on this?

Come on, somebody. Even if only to say, “Uh, you’re obviously doing it wrong, this is so easy, what is your problem?” or linking to some ultra-simple tutorial that shows the right way to do this? Anything!

Also, can someone just confirm that the example from the dev kit is in fact not the right way to do it?