Results 1 to 6 of 6
  1. #1
    Bill86 is offline Member
    Join Date
    Dec 2009
    Posts
    5
    Rep Power
    0

    Default Applet in JFrame, JMenu problem

    I'm having a problem with the below code, being displayed properly on windows vista. However it works on windows xp for me.

    Java Code:
    public GUI(Applet app) {
    	super("Title");
    	JPanel panel = new JPanel();
    	panel.setLayout(new BorderLayout());
    	
    	JMenuBar jmb = new JMenuBar();
    	
    	// -- File --
    	JMenu file = new JMenu("File");
    		JMenuItem open   = new JMenuItem("Open...");
    		
    		open.setActionCommand("file-open");
    		open.addActionListener(this);
    		
    		JMenuItem reload = new JMenuItem("Reload");
    		
    		reload.setActionCommand("file-reload");
    		reload.addActionListener(this);
    		
    		JMenuItem exit         = new JMenuItem("Exit");
    		
    		exit.setActionCommand("file-exit");
    		exit.addActionListener(this);
    		
    		file.add(open);
    		file.add(reload);
    		
    		file.add(new JSeparator());
    		
    		file.add(exit);
    	// --
    	
    	jmb.add(file);
    	
    	getContentPane().add(jmb);
    	setJMenuBar(jmb);
    	getContentPane().add(app, BorderLayout.CENTER);
    	setSize(519, 394);
    	setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    	setResizable(false);
    	setVisible(true);
    }

    What happens is:

    On vista, the JMenuItem's are displayed under the applet. Instead of on top of the applet.

    Why is this?

    Thanks,
    Bill86

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    It seems funny that you're adding an Applet to to a JFrame, but perhaps your problem is more from mixing AWT and Swing components than from trying shove the applet square peg into the desktop round hole.

    So why this design?

  3. #3
    Bill86 is offline Member
    Join Date
    Dec 2009
    Posts
    5
    Rep Power
    0

    Default

    Quote Originally Posted by Fubarable View Post
    It seems funny that you're adding an Applet to to a JFrame, but perhaps your problem is more from mixing AWT and Swing components than from trying shove the applet square peg into the desktop round hole.

    So why this design?
    The applet is from a different java program. That I'm displaying on the JFrame.

    What should I use instead? To prevent "mixing AWT and Swing components".
    Last edited by Bill86; 12-27-2009 at 03:43 PM.

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    You would be better off if the "different Java program" (here called the "GUI class") produced a JPanel instead of an Applet. This way, should you desire to display it as an applet, you would create a class that extends JApplet, have it hold an instance of your GUI class (the one that now produces a JPanel) and place the JPanel into the JApplet's contentPane. Likewise if you wanted to display it as a JFrame, create a class that displays a JFrame, have it hold an instance of the GUI class and then place the GUI's JPanel into the JFrame's contentPane.

  5. #5
    Bill86 is offline Member
    Join Date
    Dec 2009
    Posts
    5
    Rep Power
    0

    Default

    Like this?


    "GUI.class" that now extends JPanel, instead of JFrame.

    Java Code:
            // Applet app = (Applet) Class.forName("abc", false, loader).newInstance();
    	public GUI(Applet app) {
    		setLayout(new BorderLayout());
    		add(app, BorderLayout.CENTER);
    	}

    The below class extends JFrame / implements ActionListener.

    Java Code:
            // 'panel' = an instance of the "GUI.class".
    	public GUIJ(JPanel panel) {
    		super("Title");
    		
    		JMenuBar jmb = new JMenuBar();
    		
    		// -- File --
    		JMenu file = new JMenu("File");
    			JMenuItem open   = new JMenuItem("Open...");
    			
    			open.setActionCommand("file-open");
    			open.addActionListener(this);
    			
    			JMenuItem reload = new JMenuItem("Reload");
    			
    			reload.setActionCommand("file-reload");
    			reload.addActionListener(this);
    			
    			JMenuItem exit         = new JMenuItem("Exit");
    			
    			exit.setActionCommand("file-exit");
    			exit.addActionListener(this);
    			
                            file.add(open);
                            file.add(reload);
    
    			file.add(new JSeparator());
    			
    			file.add(exit);
    		// -- End --
    			
    		jmb.add(file);
    		getContentPane().add(jmb);
    		setJMenuBar(jmb);
    		getContentPane().add(panel);
    		
    		setResizable(false);
    		setSize(519, 394);
    		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		setVisible(true);
    	}
    It works just the same on xp, I'll test it on vista when I can.

    Edit: Still doesn't work on vista. Am I doing something wrong still?
    Last edited by Bill86; 12-28-2009 at 05:13 AM.

  6. #6
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by Bill86 View Post
    Like this?
    "GUI.class" that now extends JPanel, instead of JFrame.
    .....

    It works just the same on xp, I'll test it on vista when I can.

    Edit: Still doesn't work on vista. Am I doing something wrong still?
    The biggest problem is the Applet as this is still an AWT component. So if possible this is the class that should produce a JPanel.

Similar Threads

  1. link between two JFrame using JMenu
    By JavaEbony in forum NetBeans
    Replies: 8
    Last Post: 03-09-2011, 12:19 PM
  2. Embed java applet in JFrame
    By Bill89 in forum New To Java
    Replies: 0
    Last Post: 12-09-2009, 02:05 PM
  3. javax.media.Player and JMenu problem
    By nowy in forum AWT / Swing
    Replies: 2
    Last Post: 11-10-2009, 12:49 PM
  4. Closing Popup JFrame in Applet
    By Arsenic in forum Java Applets
    Replies: 3
    Last Post: 04-10-2009, 09:37 PM
  5. JFrame vs Applet
    By baltimore in forum AWT / Swing
    Replies: 1
    Last Post: 08-06-2007, 04:24 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •