Skip to Main Content

Java SE (Java Platform, Standard Edition)

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Actions make Menu items disappear

843806Nov 10 2008 — edited Nov 11 2008
Hi everybody,
I've tried something new, hooking up Actions to JMenuItems, but it doesn't seem to be working. I have a menu with items on it, and each of those items have a different action attached. The actions themselves work fine, but when the actions are attached to the JMenuItems, the JMenuItems' text disappears. Could anyone please help me figure out why this is?

I have a SSCCE that demonstrates the problem here:
import java.awt.event.ActionEvent;

import javax.swing.AbstractAction;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;

public class Test {
	public static void main(String[] args) {
		try {
			JFrame frame = new JFrame();
			frame.setSize( 200, 200 );
			frame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
			
			JMenuBar bar = new JMenuBar();
			JMenu menu = new JMenu( "Test Menu" );
			
			JMenuItem item1 = new JMenuItem( "test" );
			item1.setAction( new Click() );
			menu.add( item1 );
			
			JMenuItem item2 = new JMenuItem( "test2" );
			item2.setAction( new Click() );
			menu.add( item2 );
			
			bar.add( menu );
			frame.setJMenuBar( bar );
			frame.setVisible( true );
		} catch( Exception e ) {
			e.printStackTrace();
			System.exit( -1 );
		}
	}
}

class Click extends AbstractAction {
	public void actionPerformed( ActionEvent e ) {
		System.out.println( "click" );
	}
}
Is there something strange about JMenuItems, like how you can't hook an actionListener directly to a JMenu? or is there just something silly I'm missing? I'd really appreciate any help.

Thanks,
Jezzica85

Edited by: jezzica85 on Nov 10, 2008 2:38 PM
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Dec 9 2008
Added on Nov 10 2008
6 comments
222 views