Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 5 of 5

Thread: Trouble with Java menubar in java frame.

  1. #1
    Junior Member
    Join Date
    Apr 2013
    Posts
    6
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Trouble with Java menubar in java frame.

    Hey, I've got some trouble with my code. It should be two tabs in the frame, with the titles "File" and "Open". But I can only see "File". I rewrote the code and launched it, and I could see both, then I changed size of the frame, and they were gone again. Sometimes I can see neither one, it's really weird. Here's the code, take a look at it and see if you can see any mistakes. And I'm using Dr Java and Eclipse, neither of them works.

    import javax.swing.*;
    public class Meny{
    public static void main(String[] cmdLn){
    JFrame f = new JFrame("Meny");
    f.setVisible(true);
    f.setSize(300,300);
    f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    JMenuBar mb = new JMenuBar();
    f.setJMenuBar(mb);

    JMenu file = new JMenu("File");
    mb.add(file);
    JMenuItem exit = new JMenuItem();
    file.add(exit);

    JMenu open = new JMenu("Open");
    mb.add(open);

    }

    }


    I can't see what I've done wrong. Help would be appreciated.
    Edit
    All of a sudden I can see the two tabs, but I can't see the "exit" option in the tab "File". It's really weird.


  2. #2
    Junior Member
    Join Date
    Apr 2013
    Posts
    25
    Thanks
    0
    Thanked 3 Times in 3 Posts

    Default Re: Trouble with Java menubar in java frame.

    Hi

    You should provide label for exit menu item. i.e JMenuItem exit = new JMenuItem("Exit");

  3. The Following User Says Thank You to hope_08 For This Useful Post:

    Jullix993 (April 16th, 2013)

  4. #3
    Junior Member
    Join Date
    Apr 2013
    Posts
    6
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Trouble with Java menubar in java frame.

    I'm not sure what you mean, JLabel?

    Anyway, it's working better now, but I can't understand why it's so messy.

    Do you think it has anything to do with the size? At first, when I start the program, the tabs aren't there, but when i grab the edge and reducing/increasing the frame's size, they show up. Strange.

  5. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Trouble with Java menubar in java frame.

    Try waiting to call setVisible() until after everything is ready to be seen. The code adds to the GUI after it is called.

    Please edit your post and wrap your code with code tags:
    [code=java]
    <YOUR CODE HERE>
    [/code]
    to get highlighting and preserve formatting.
    If you don't understand my answer, don't ignore it, ask a question.

  6. The Following User Says Thank You to Norm For This Useful Post:

    Jullix993 (April 16th, 2013)

  7. #5
    Junior Member
    Join Date
    Apr 2013
    Posts
    6
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Trouble with Java menubar in java frame.

    Thank you very much, I will remember that.

Similar Threads

  1. Printing two rectangles on same Frame in Java
    By harshendushridhar in forum What's Wrong With My Code?
    Replies: 11
    Last Post: April 6th, 2013, 01:39 PM
  2. Save java frame as pdf/image.
    By Austin777 in forum Java Theory & Questions
    Replies: 6
    Last Post: October 16th, 2012, 08:01 AM
  3. MenuBar in Java?
    By xTommy24 in forum AWT / Java Swing
    Replies: 2
    Last Post: October 12th, 2011, 03:27 PM
  4. Java Frame Buffer Line drawing
    By tcmei in forum What's Wrong With My Code?
    Replies: 3
    Last Post: May 17th, 2011, 06:55 AM
  5. IE browser in Java swing/AWT Frame
    By gafaec in forum AWT / Java Swing
    Replies: 0
    Last Post: April 10th, 2011, 05:16 AM