Results 1 to 3 of 3
  1. #1
    minime12358 is offline Member
    Join Date
    Jul 2010
    Posts
    21
    Rep Power
    0

    Default Adding a menu from a seperate class to a JFrame

    Currently, I am making a simple JFrame with a JMenuBar and a JPanel. I have the JFrame in one class that is the driver, and the JMenuBar and JPanel each in a separate class. The problem I am having is that even though I add it to the frame, it is not visible. Here is the basis of what I am using:

    public class Drivertest
    {
    public static void main(String[] args) throws Exception
    {
    JFrame frame = new JFrame("Timer");
    frame.setUndecorated(true);
    frame.getRootPane().setWindowDecorationStyle(JRoot Pane.NONE);
    frame.setJMenuBar(new menuBar());
    int width = 300;
    int height = 150;
    Toolkit toolkit = Toolkit.getDefaultToolkit ();
    Dimension dim = toolkit.getScreenSize();
    frame.setSize(width,height);
    frame.setLocation(dim.width-width, dim.height-height);
    TimerPanel through = new TimerPanel();
    through.changeTime(0,10);
    through.Reset();
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOS E);
    frame.setContentPane(through);
    frame.setVisible(true);


    }
    }
    And then, my menu looks like this: (I cut out all of the components of it)
    public class menuBar extends JMenuBar
    {
    JMenuBar menuBar;
    JMenu menu, submenu;
    JMenuItem menuItem;
    JRadioButtonMenuItem rbMenuItem;
    JCheckBoxMenuItem cbMenuItem;
    public menuBar()
    {
    JMenuBar menuBar;
    JMenu menu, submenu;
    JMenuItem menuItem;
    JRadioButtonMenuItem rbMenuItem;
    JCheckBoxMenuItem cbMenuItem;

    //Create the menu bar.
    menuBar = new JMenuBar();

    //Build the first menu.
    menu = new JMenu("A Menu");
    ...
    //The Rest
    }
    }
    The thing that bugs me is that when I have everything in the driver class(The one with the frame) it works fine, but when I put it in a seperate class(which I need to do), it doesnt work.

  2. #2
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,446
    Rep Power
    25

    Default

    Don't create another menubar in the extended menubar class. Use the 'this' reference for adding to it. The extended class is what is added to the JFrame.

  3. #3
    minime12358 is offline Member
    Join Date
    Jul 2010
    Posts
    21
    Rep Power
    0

Similar Threads

  1. Replies: 2
    Last Post: 04-04-2010, 08:57 PM
  2. Manipulating components of a seperate GUI Class
    By ribbs2521 in forum New To Java
    Replies: 1
    Last Post: 10-20-2009, 10:53 PM
  3. [SOLVED] Adding JLabel to JFrame
    By mlfatty in forum AWT / Swing
    Replies: 3
    Last Post: 03-04-2009, 11:33 PM
  4. Adding popup Menu on animation
    By Peggy in forum Java Applets
    Replies: 2
    Last Post: 12-10-2008, 08:33 AM
  5. Replies: 6
    Last Post: 07-28-2008, 06:04 PM

Tags for this Thread

Posting Permissions

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