Results 1 to 14 of 14
  1. #1
    Asvin is offline Member
    Join Date
    Nov 2010
    Posts
    66
    Rep Power
    0

    Default Please help me ASAP!

    Hey guys.. I was looking to study from this code example the guys over at oracle.com made:

    Java Code:
    /*
     * Copyright (c) 1995, 2008, Oracle and/or its affiliates. All rights reserved.
     *
     * Redistribution and use in source and binary forms, with or without
     * modification, are permitted provided that the following conditions
     * are met:
     *
     *   - Redistributions of source code must retain the above copyright
     *     notice, this list of conditions and the following disclaimer.
     *
     *   - Redistributions in binary form must reproduce the above copyright
     *     notice, this list of conditions and the following disclaimer in the
     *     documentation and/or other materials provided with the distribution.
     *
     *   - Neither the name of Oracle or the names of its
     *     contributors may be used to endorse or promote products derived
     *     from this software without specific prior written permission.
     *
     * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
     * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
     * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT OWNER OR
     * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
     * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
     * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
     * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
     * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
     * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
     * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     */ 
    
    package components;
    
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    
    /**
     * @author ges
     * @author kwalrath
     */
    /* MenuLayoutDemo.java requires no other files. */
    
    public class MenuLayoutDemo {
        public JMenuBar createMenuBar() {
            JMenuBar menuBar = new JMenuBar();
            menuBar.setLayout(new BoxLayout(menuBar, BoxLayout.PAGE_AXIS));
            menuBar.add(createMenu("Menu 1"));
            menuBar.add(createMenu("Menu 2"));
            menuBar.add(createMenu("Menu 3"));
    
            menuBar.setBorder(BorderFactory.createMatteBorder(0,0,0,1,
                                                              Color.BLACK));
            return menuBar;
        }
    
        // used by createMenuBar
        public JMenu createMenu(String title) {
            JMenu m = new HorizontalMenu(title);
            m.add("Menu item #1 in " + title);
            m.add("Menu item #2 in " + title);
            m.add("Menu item #3 in " + title);
    
            JMenu submenu = new HorizontalMenu("Submenu");
            submenu.add("Submenu item #1");
            submenu.add("Submenu item #2");
            m.add(submenu);
    
            return m;
        }
    
        /**
         * Create the GUI and show it.  For thread safety,
         * this method should be invoked from the
         * event-dispatching thread.
         */
        private static void createAndShowGUI() {
            //Create and set up the window.
            JFrame frame = new JFrame("MenuLayoutDemo");
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    
            //Create and set up the content pane.
            MenuLayoutDemo demo = new MenuLayoutDemo();
            Container contentPane = frame.getContentPane();
            contentPane.setBackground(Color.WHITE); //contrasting bg
            contentPane.add(demo.createMenuBar(),
                            BorderLayout.LINE_START);
    
            //Display the window.
            frame.setSize(300, 150);
            frame.setVisible(true);
        }
    
        public static void main(String[] args) {
            //Schedule a job for the event-dispatching thread:
            //creating and showing this application's GUI.
            javax.swing.SwingUtilities.invokeLater(new Runnable() {
                public void run() {
                    createAndShowGUI();
                }
            });
        }
    
        class HorizontalMenu extends JMenu {
            HorizontalMenu(String label) {
                super(label);
                JPopupMenu pm = getPopupMenu();
                pm.setLayout(new BoxLayout(pm, BoxLayout.LINE_AXIS));
            }
    
            public Dimension getMinimumSize() {
                return getPreferredSize();
            }
    
            public Dimension getMaximumSize() {
                return getPreferredSize();
            }
    
            public void setPopupMenuVisible(boolean b) {
                boolean isVisible = isPopupMenuVisible();
                if (b != isVisible) {
                    if ((b==true) && isShowing()) {
                        //Set location of popupMenu (pulldown or pullright).
                        //Perhaps this should be dictated by L&F.
                        int x = 0;
                        int y = 0;
                        Container parent = getParent();
                        if (parent instanceof JPopupMenu) {
                            x = 0;
                            y = getHeight();
                        } else {
                            x = getWidth();
                            y = 0;
                        }
                        getPopupMenu().show(this, x, y);
                    } else {
                        getPopupMenu().setVisible(false);
                    }
                }
            }
        }
    }
    I do get it to compile, however it gives me a runtime error.. I have attached the error message as a file.. Please take a look and help me out.. Thank you in advance!
    Attached Thumbnails Attached Thumbnails Please help me ASAP!-helpimage.jpg  

  2. #2
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,949
    Rep Power
    19

    Default

    Can you copy and paste the error...that's unreadable.

    Also identify what line it occurred on.

  3. #3
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,450
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by Tolls View Post
    that's unreadable.
    Can't you read that? Man, you need glasses. That's a stack trace and the last line reads "Press any key to continue ..."; duh, easy ...

    kind regards,

    Jos aka Hawkeye ;-)
    cenosillicaphobia: the fear for an empty beer glass

  4. #4
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,321
    Rep Power
    25

    Default

    To copy the contents of the command prompt window:
    Click on Icon in upper left corner
    Select Edit
    Select 'Select All' - The selection will show
    Click in upper left again
    Select Edit and click 'Copy'

    Paste here.

  5. #5
    Asvin is offline Member
    Join Date
    Nov 2010
    Posts
    66
    Rep Power
    0

    Default

    Oh shikes.. sorry for that attachment.. didn't know the forum would shrink it down so much.. Sorry!

    Thanks for teaching me something new! :)

    Java Code:
    Exception in thread "main" java.lang.NoClassDefFoundError: MenuLayoutDemo (wrong
     name: components/MenuLayoutDemo)
            at java.lang.ClassLoader.defineClass1(Native Method)
            at java.lang.ClassLoader.defineClassCond(ClassLoader.java:632)
            at java.lang.ClassLoader.defineClass(ClassLoader.java:616)
            at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:14
    1)
            at java.net.URLClassLoader.defineClass(URLClassLoader.java:283)
            at java.net.URLClassLoader.access$000(URLClassLoader.java:58)
            at java.net.URLClassLoader$1.run(URLClassLoader.java:197)
            at java.security.AccessController.doPrivileged(Native Method)
            at java.net.URLClassLoader.findClass(URLClassLoader.java:190)
            at java.lang.ClassLoader.loadClass(ClassLoader.java:307)
            at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301)
            at java.lang.ClassLoader.loadClass(ClassLoader.java:248)
    Could not find the main class: MenuLayoutDemo.  Program will exit.
    Press any key to continue . . .

  6. #6
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,321
    Rep Power
    25

    Default

    Where is the command line you used to execute the program?
    The program is in a package, so you need to consider that when executing the program.

    In the folder containing the components folder, enter:
    java components.MenuLayoutDemo

  7. #7
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,450
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by Asvin View Post
    Java Code:
    Exception in thread "main" java.lang.NoClassDefFoundError: MenuLayoutDemo (wrong
     name: components/MenuLayoutDemo)
    The class is in a package so you should've run it as components.MenuLayoutDemo. Also that .class file should be stored in its correct directory.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  8. #8
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,949
    Rep Power
    19

    Default

    Quote Originally Posted by JosAH View Post
    Can't you read that? Man, you need glasses. That's a stack trace and the last line reads "Press any key to continue ..."; duh, easy ...

    kind regards,

    Jos aka Hawkeye ;-)
    Just call me Mr Magoo.

  9. #9
    Asvin is offline Member
    Join Date
    Nov 2010
    Posts
    66
    Rep Power
    0

    Default

    I ran it as components.MenuLayoutDemo and it still gave me the same error.. The class file and java file are in the same folder!

  10. #10
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,949
    Rep Power
    19

    Default

    And are you running the command from the folder above that one?
    Not the components folder.

  11. #11
    nitishsp is offline Member
    Join Date
    May 2011
    Posts
    3
    Rep Power
    0

    Default

    remove "package components;" from the code.

  12. #12
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,450
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by Asvin View Post
    I ran it as components.MenuLayoutDemo and it still gave me the same error.. The class file and java file are in the same folder!
    Read some tutorials about packages, where to store them and how to run them; it wouldn't surprise me if all that information is mentioned in your book.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  13. #13
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,450
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by nitishsp View Post
    remove "package components;" from the code.
    That would be a stupid nearsighted hack.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  14. #14
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,321
    Rep Power
    25

    Default

    it still gave me the same error
    When you get an error, please copy the FULL text of the console window and paste it here.
    We can only guess what you're doing wrong, the full text would show us what happened.
    Also do a dir command to show what is in the current directory.

Similar Threads

  1. Please need help asap
    By Tamtome in forum New To Java
    Replies: 9
    Last Post: 02-04-2011, 08:19 PM
  2. Need help ASAP, please
    By KAM0002 in forum New To Java
    Replies: 6
    Last Post: 12-08-2010, 04:58 PM
  3. Need help asap please!
    By mbm4ever in forum Java Applets
    Replies: 4
    Last Post: 08-12-2010, 03:57 PM
  4. Need some help ASAP
    By varma in forum New To Java
    Replies: 11
    Last Post: 01-08-2010, 12:15 AM
  5. HELP PLEASE! Need reply ASAP
    By SteroidalPsycho in forum New To Java
    Replies: 3
    Last Post: 10-21-2009, 08:28 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
  •