Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 JMenuBar won't show up on my JFrame
Index -> Programming, Java -> Java Help
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
Bo0sT




PostPosted: Sat Apr 25, 2009 12:17 pm   Post subject: JMenuBar won't show up on my JFrame

So I made a subclass of JFrame and i'm using it as the entry point to my program. So far all I have on it is a JMenuBar and a subclass of Canvas. The JMenuBar has one JMenu on it and the JMenu has four JMenuItems. So I added the JMenuBar using setJMenuBar(JMenuBar) and everything worked nicely. Then I changed the look and feel to getSystemLookAndFeelClassName() and now only the Canvas shows up but the JMenuBar doesn't. I'm thinking it's because the Canvas is from java.awt and the JMenuBar is javax.swing, so the look and feel effects them diffrently, i'm not sure if i'm right but if I am how could I work around this? If i'm not then what's wrong and how do I fix it?
Sponsor
Sponsor
Sponsor
sponsor
Dark




PostPosted: Sun Apr 26, 2009 10:26 am   Post subject: RE:JMenuBar won\'t show up on my JFrame

Hard to say because you don't have a code snippet posted...

but have you tried .setVisible(true); for the JFrame? that could help.

I've never used Canvas but post your GUI code and I might be able to help.
Bo0sT




PostPosted: Sun Apr 26, 2009 5:36 pm   Post subject: Re: JMenuBar won't show up on my JFrame

I made my description so long so that I wouldn't have to post a code snippet. I'll remake a sample though, so that my code isn't on the internet and I don't get accused of plagurism.

It's basically what I explained
Java:

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

import java.awt.BorderLayout;
import java.awt.Canvas;
import java.awt.Color;
import java.awt.Graphics;

public class CharacterMaker extends JFrame
{
   JMenuBar menuBar;
   JMenu fileMenu;
   JMenuItem newMenu;
   JMenuItem saveMenu;
   JMenuItem openMenu;

   Viewer viewer;

   public static void main(String[] args)
   {
      new CharacterMaker();
   }

   public CharacterMaker()
   {
      super("Character Maker");

      // This code is supose to apply system look and feel to my program, in my case it is Windows XP.
      try {UIManager.setLookAndFeel
           (UIManager.getSystemLookAndFeelClassName());}
      catch (Exception e) {System.err.println("Error: " + e);}

      setSize(400,200);
      setDefaultCloseOperation(EXIT_ON_CLOSE);
      setVisible(true);
      setLayout(new BorderLayout());

      menuBar = new JMenuBar();
      fileMenu = new JMenu("File");
      newMenu = new JMenuItem("New");
      saveMenu = new JMenuItem("Save");
      openMenu = new JMenuItem("Open");

      viewer = new Viewer();

      fileMenu.add(newMenu);
      fileMenu.add(saveMenu);
      fileMenu.add(openMenu);
      menuBar.add(fileMenu);

      setJMenuBar(menuBar);
      getContentPane().add(viewer, BorderLayout.CENTER);
   }
}

class Viewer extends Canvas
{
   public Viewer()
   {
      setSize(200,200);
   }

   public void paint(Graphics g)
   {
      g.setColor(Color.white);
      g.fillRect(0,0,200,200);
      g.setColor(Color.black);
      g.drawRect(0,0,200,200);
   }
}


ok here is a sample of my code, this is a watered down version of it, of just what is giving me problems, there are more bells and whistles on the original. Also, while I was writing this I found that if i resize the screen, everything shows up like it's supose to. I think I can use that to solve my problem but if anyone has anything better plz link me back
Dark




PostPosted: Tue Apr 28, 2009 2:31 pm   Post subject: RE:JMenuBar won\'t show up on my JFrame

Put
Java:
setVisible(true);


at the end of your constructor public CharacterMaker(), right under

Java:
getContentPane().add(viewer, BorderLayout.CENTER);


Always have your setVisible(true); as the last line of code in constructor methods such as this one, so after the compiler executes your code, it will set your JFrame to visible.

Otherwise, in your case for example, it will be set to visible but then add the menu after.

So put setVisible at the end. Hope that helps
Display posts from previous:   
   Index -> Programming, Java -> Java Help
View previous topic Tell A FriendPrintable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 1  [ 4 Posts ]
Jump to:   


Style:  
Search: