MenuPieces and mnemonics

Hello,
I am using a Method that crawls my menu and sets the mnemonics. This does not work if i use it directly after assembling the menue with some MenuePieces. The problem is that the MenuItems genereted by MenuPieces return an empty String when calling getText.

When I call my method in an seperate Thread that waits for 5 Seconds before working I do get the desired Text. Is there any way do get a callback when the MenuPieces are ready to use?

Best regards,
schumacb

There is a MenuPieceListener which is called after new items were created. I’m not sure if the listener helps, but it is certainly worth a try.

I’m not sure why getText returns an empty string, sounds like a bug to me. Does this happen for all items, or just a subset?

Thanks for your reply.

I have tried to add a MenuPieceListener to the RootMenuPiece and to the CLayoutChoiceMenuPiece.
The first one is called the time when I add the CLayoutChoiceMenuPiece to it, but this seems to be to early.
And the MenuItems getText return “”.

The second one is never called. Seems like the CLayoutChoiceMenuPiece has all its components set in the constructor.

It happens for all MenuItems of the CLayoutChoiceMenuPiece.

Ah, it is not a bug, it is a feature!

Since the text depends on the current language, the text needs to be set dynamically. To be precise: the text is set shortly before the items become visible (and may be removed once the items are invisible again). The link between visibility and text exists to prevent a memory leak. Otherwise invisible items would continue to monitor the TextManager (internal map containing all the text that is shown), and old items would never be garbage collected.

I admit I did not think of mnemonics when implementing this feature.

There is however a workaround, this might introduce a memory leak, but only if you continue to create more and more MenuPieces:


import java.awt.Component;

import javax.swing.JMenuItem;

import bibliothek.gui.dock.common.CControl;
import bibliothek.gui.dock.common.menu.CLayoutChoiceMenuPiece;
import bibliothek.gui.dock.facile.menu.RootMenuPiece;

public class Dock67 {
	public static void main( String[] args ){
		boolean textIsEmpty = false;
		
		CControl control = new CControl();
		CLayoutChoiceMenuPiece menu = new CLayoutChoiceMenuPiece( control, false );
		
		if( !textIsEmpty ){
			RootMenuPiece root = new RootMenuPiece( "Menu", true, menu ){
				@Override
				public void unbind(){
					// ignore, never allow a menu to be treated as if it were invisible
				}
			};	

			// tell this menu that it is visible
			root.bind();
		}
		
		for( Component c : menu.items()){
			JMenuItem m = (JMenuItem)c;
			System.out.println( "text: " + m.getText() );
		}
	}
}

Thank you for your reply.

With that information i could solve the problem for me.
Instead of statically bind the menu I have overridden the bind method and called my method to set the mnemonics for this menu from there.


    ...
    @Override
    public void bind()
    {
        super.bind();
        setMnemonics(getMenu());
    }
    ...

That’s an even better solution than mine.