Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » WindowBuilder » JFrame and JMenuBar(problems using a JFrame with separated JMenuBar)
JFrame and JMenuBar [message #1014623] Tue, 26 February 2013 02:12 Go to next message
Andreas Walter is currently offline Andreas Walter
Messages: 3
Registered: February 2013
Junior Member
Hi all,

I've created a simple JFrame class which should get it's menu bar from another class.
Something like this:

// ...
public class MclViewerFrame extends JFrame {
// ...
private MainMenuBar mainMenuBar = null;
// ...
this.mainMenuBar = new MainMenuBar(this);
// WindowBuilder doesn't work with this line
this.setJMenuBar(this.mainMenuBar);

And the MainMenuBar class looks like this:

// ...
public class MainMenuBar extends JMenuBar {
// ...

When I compile this stuff, it works as it should. But when I start the design view of the WindowBuilder, I get the error message
java.lang.ClassCastException: javax.swing.JPanel cannot be cast to javax.swing.JMenuBar

Deleting the line "this.setJMenuBar(this.mainMenuBar);" makes the WindowBuilder work, but of course without the menu bar.

Where's my flaw? Should I build the menu bar with a factory class or something like that?
Thanks for comments!
Re: JFrame and JMenuBar [message #1014755 is a reply to message #1014623] Tue, 26 February 2013 06:58 Go to previous messageGo to next message
Eric Clayberg is currently offline Eric Clayberg
Messages: 847
Registered: July 2009
Location: Boston, MA
Senior Member
No idea without being able to reproduce the problem.

I would recommend posting a complete example and the complete exception stack trace.
Re: JFrame and JMenuBar [message #1014980 is a reply to message #1014623] Wed, 27 February 2013 01:29 Go to previous messageGo to next message
Andreas Walter is currently offline Andreas Walter
Messages: 3
Registered: February 2013
Junior Member
I've made it a little simpler...

Class TestMenuBar:
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowEvent;

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

public class TestMenuBar extends JMenuBar {

	private static final long serialVersionUID = 1L;
	private String strFile = "File";
	private String strClose = "Close";
	
	private TestFrame parent = null;
	
	private JMenu menuFile = new JMenu(this.strFile);
	
	public TestMenuBar(TestFrame parent) {
		this.parent = parent;
		setMenuFile();
	}
	
	private void setMenuFile() {
		
		this.menuFile.add(setMenuItemFileClose());
		this.add(this.menuFile);
	}
	
	private JMenuItem setMenuItemFileClose() {
		
		// exit
		JMenuItem menu = new JMenuItem();
		menu.setText(this.strClose);
		menu.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				parent.processWindowEvent(new WindowEvent(parent, WindowEvent.WINDOW_CLOSING));
			}
		});
		return menu;
	}
}

Class TestFrame:
import java.awt.Dimension;

public class TestFrame extends JFrame{

	private TestMenuBar testMenuBar = null;
	public static final String TITLE = "Test WindowBuilder";
	
	public TestFrame() {
		
		init();
	}
	
	private void init() {
		
		setTitle(TITLE);
		
		testMenuBar = new TestMenuBar(this);
		setJMenuBar(testMenuBar);
		
		setMinimumSize(new Dimension(800, 600));
		setVisible(true);
		setExtendedState(getExtendedState() | MAXIMIZED_BOTH);
	}
	
	protected void processWindowEvent(WindowEvent e) {
		
		if (e.getID() == WindowEvent.WINDOW_CLOSING) {

			// close
			this.setVisible(false);
			this.dispose();
			System.exit(0);
		}
		super.processWindowEvent(e);
	}
}


Class Main:
public class Main {
	
	private static TestFrame testFrame = null;

	/**
	 * @param args
	 */
	public static void main(String[] args) {

		testFrame = new TestFrame();

	}

}


And here's the full context stack trace:
java.lang.ClassCastException: javax.swing.JPanel cannot be cast to javax.swing.JMenuBar
	at org.eclipse.wb.internal.swing.model.component.menu.JMenuBarInfo.refresh_afterCreate(JMenuBarInfo.java:100)
	at org.eclipse.wb.core.model.ObjectInfo.refresh_afterCreate(ObjectInfo.java:621)
	at org.eclipse.wb.core.model.JavaInfo.refresh_afterCreate(JavaInfo.java:1217)
	at org.eclipse.wb.core.model.AbstractComponentInfo.refresh_afterCreate(AbstractComponentInfo.java:238)
	at org.eclipse.wb.core.model.ObjectInfo.refreshCreate0(ObjectInfo.java:552)
	at org.eclipse.wb.core.model.ObjectInfo.access$0(ObjectInfo.java:546)
	at org.eclipse.wb.core.model.ObjectInfo$5$1.run(ObjectInfo.java:486)
	at org.eclipse.wb.internal.core.utils.execution.ExecutionUtils.runDesignTime(ExecutionUtils.java:139)
	at org.eclipse.wb.core.model.ObjectInfo$5.run(ObjectInfo.java:484)
	at org.eclipse.wb.internal.swing.utils.SwingUtils$2.run(SwingUtils.java:76)
	at java.awt.event.InvocationEvent.dispatch(Unknown Source)
	at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
	at java.awt.EventQueue.access$000(Unknown Source)
	at java.awt.EventQueue$1.run(Unknown Source)
	at java.awt.EventQueue$1.run(Unknown Source)
	at java.security.AccessController.doPrivileged(Native Method)
	at java.security.AccessControlContext$1.doIntersectionPrivilege(Unknown Source)
	at java.awt.EventQueue.dispatchEvent(Unknown Source)
	at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
	at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
	at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
	at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
	at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
	at java.awt.EventDispatchThread.run(Unknown Source)


This code compiles fine - a simple frame with a menubar is shown.
Switching to the TestFrame design view of the WindowBuilder produces the error above, but deleting the line "setJMenuBar(testMenuBar);" makes it work (as I've written in my first post).

Hope, this helps you to help me Wink
Re: JFrame and JMenuBar [message #1015024 is a reply to message #1014980] Wed, 27 February 2013 03:40 Go to previous messageGo to next message
Eric Clayberg is currently offline Eric Clayberg
Messages: 847
Registered: July 2009
Location: Boston, MA
Senior Member
All custom components used within WB must be valid Java Beans. Your TestMenuBar class is missing the required default zero-arg constructor. Add that and it works fine.
Re: JFrame and JMenuBar [message #1015058 is a reply to message #1015024] Wed, 27 February 2013 05:27 Go to previous message
Andreas Walter is currently offline Andreas Walter
Messages: 3
Registered: February 2013
Junior Member
You're right... it worked fine.
Thanks a lot!
Previous Topic:Game Project for University
Next Topic:Welcome
Goto Forum:
  


Current Time: Fri Aug 01 12:17:38 EDT 2014

Powered by FUDForum. Page generated in 0.01615 seconds