Results 1 to 15 of 15

Thread: Menu class

  1. #1
    Kligham is offline Member
    Join Date
    Sep 2009
    Posts
    30
    Rep Power
    0

    Default Menu class

    Hallo,

    I have these two classes atm:

    Java Code:
    package test;
    
    /*
     * Auteur
     */
    
    import javax.swing.JFrame;
    
    public class Main extends JFrame{
    
        public static void main(String[] args) {
    
        JFrame frame = new JFrame();
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        [COLOR="red"][B]frame.setJMenuBar(new Menu());[/B][/COLOR]
    
        frame.pack();
        frame.setSize(500,500);
        frame.setVisible(true);
    
        }
    
    }
    and:

    Java Code:
    package test;
    
    import javax.swing.*;
    import java.awt.event.*;
    [COLOR="red"][B]import java.awt.event.InputEvent.CTRL_DOWN_MASK;[/B][/COLOR]
    
    public class Menu{
    
        public Menu (){
    
            JMenuBar menuBar = new JMenuBar();
    
            JMenu consultatie = new JMenu("Consultatie");
            menuBar.add(consultatie);
            JMenuItem nieuweClientItem= new JMenuItem("Nieuwe CliŽnt");
            [B][COLOR="Red"]nieuweClientItem.setAccelerator(KeyStroke.getKeyStroke("C", CTRL_DOWN_MASK));[/COLOR][/B]
            consultatie.add(nieuweClientItem);
    
            JMenu boekhouding = new JMenu("Boekhouding");
            menuBar.add(boekhouding);
    
        }
    }
    Netbeans gives me 3 error signs, I underlined them in my code. What am I doing wrong?
    Last edited by Kligham; 09-26-2009 at 10:40 PM.

  2. #2
    travishein's Avatar
    travishein is offline Senior Member
    Join Date
    Sep 2009
    Location
    Canada
    Posts
    684
    Rep Power
    5

    Default

    the first error, need ot use JMenu I think, instead of Menu, when working with JFrame.

    In general swing things have a "J*" version of their AWT counterparts.

    e.g.

    JFrame
    JMenu
    JMenItem
    JButton

  3. #3
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default

    Perhaps you could say what the error messages are if you don't understand them?

    Generally they try to be self explanatory (but don't always succeed!).

    A couple of things to watch out for: (1) You don't import a constant like InputEvent.CTRL_DOWN_MASK. Instead you import the class (whatever.InputEvent) and then use the static int value as InputEvent.CTRL_DOWN_MASK in your code.

    (2) Have the API documentation open all the time! When you use a method the parameters must match what is required exactly. A char is not a String. The same goes for your own methods: a MenuBar is not a Menu.

  4. #4
    Kligham is offline Member
    Join Date
    Sep 2009
    Posts
    30
    Rep Power
    0

    Default

    Hi,

    I'll try to keep point 1 in mind. I changed it in my programe, and it doesn't give me an error anymore.

    However, there is still one problem. I can't get my JMenubar displayed. I don't know if it is possible to add my menuBar from my Menu.java class to my JFrame wicht is in my Main.java class?

    Main.java class:

    Java Code:
    package test;
    
    /*
     * Auteur 
     */
    
    import javax.swing.JFrame;
    
    public class Main extends JFrame{
    
        public static void main(String[] args) {
    
        JFrame frame = new JFrame();
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        [COLOR="Red"][B]frame.setJMenuBar(menuBar);[/B][/COLOR]
    
        frame.pack();
        frame.setSize(500,500);
        frame.setVisible(true);
    
        }
    
    }
    Menu.java:

    Java Code:
    package test;
    
    import javax.swing.*;
    import java.awt.event.*;
    import java.awt.event.InputEvent.*;
    
    public class Menu{
    
        public Menu (){
    
            JMenuBar menuBar = new JMenuBar();
    
            JMenu consultatie = new JMenu("Consultatie");
            menuBar.add(consultatie);
            JMenuItem nieuweClientItem= new JMenuItem("Nieuwe CliŽnt");
            nieuweClientItem.setAccelerator(KeyStroke.getKeyStroke('C', InputEvent.CTRL_DOWN_MASK));
            consultatie.add(nieuweClientItem);
    
            JMenu boekhouding = new JMenu("Boekhouding");
            menuBar.add(boekhouding);
    
        }
    }
    The error that netbeans gives me is shown on the pic that I added.

    + Can anyone explain me what KeyEvent.VK_T does? I see it at alot of websites, but the never explain what it does.
    Attached Thumbnails Attached Thumbnails Menu class-menu-error.jpg  
    Last edited by Kligham; 09-27-2009 at 12:20 PM.

  5. #5
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default

    KeyEvent.VK_T is just an int. (for what it's worth, it's the value 84). It is used in a number of places where you want to refer to a key. For instance the KeyEvent class offers a getKeyCode() method. So you might have a method that gets invoked every time a key is pressed. This event would be sent an instance of KeyEvent at the time of the event and your method would call getKeyCode() to see what key was pressed. (eg if getKeyCode() returned KeyEvent.VK_T, ie 84, you would know that the "T" key had been pressed.

    The best place for usage examples would be How to Write a Key Listener in Sun's Tutorial. The whole section on Swing is well worth reading.

    In your example you seem to be using the Menu class to actually construct the menu bar that your frame will use. (The tutorial gives examples of constructing menu bars as well!) This is possible, but the Menu class should expose a "getter" method to return the menu bar which it constructs elsewhere (for instance in its constructor as you do now.)

    Here's your code (but I changed the package name to what I think you mean) with a "getter" added:

    Java Code:
    package test;
    
    import javax.swing.JFrame;
    
    public class Main extends JFrame{
        public static void main(String[] args) {
            JFrame frame = new JFrame();
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                // construct a Menu object...
            Menu menu = new Menu();
                // ...and obtain its menu bar
            frame.setJMenuBar(menu.getMenuBar());
    	
            frame.pack();
            frame.setSize(500,500);
            frame.setVisible(true);
        }
    }
    
    package test;
    
    import java.awt.event.InputEvent;
    
    import javax.swing.JMenu;
    import javax.swing.JMenuBar;
    import javax.swing.JMenuItem;
    import javax.swing.KeyStroke;
    
    public class Menu {
    
        private JMenuBar menuBar;
    
            /** Constructs the menu bar. */
        public Menu() {
            menuBar = new JMenuBar();
    
            JMenu consultatie = new JMenu("Consultatie");
            menuBar.add(consultatie);
            JMenuItem nieuweClientItem= new JMenuItem("Nieuwe CliŽnt");
            nieuweClientItem.setAccelerator(KeyStroke.getKeyStroke(
                    'C', InputEvent.CTRL_DOWN_MASK));
            consultatie.add(nieuweClientItem);
    
            JMenu boekhouding = new JMenu("Boekhouding");
            menuBar.add(boekhouding);
        }
    
            /** Returns the menu bar corresponding to this menu. */
        public JMenuBar getMenuBar() {return menuBar;}
    }
    Last edited by pbrockway2; 09-27-2009 at 03:27 AM.

  6. #6
    Kligham is offline Member
    Join Date
    Sep 2009
    Posts
    30
    Rep Power
    0

    Default

    Hi,

    Thanks for the help mate. I've got a better insight on constructors as well. I works as it should be.

  7. #7
    camickr is offline Senior Member
    Join Date
    Jul 2009
    Posts
    1,226
    Rep Power
    6

    Default

    Would just like to add that the AWT package already has a class called "Menu". So you should rename yours to avoid confusion.

  8. #8
    Kligham is offline Member
    Join Date
    Sep 2009
    Posts
    30
    Rep Power
    0

    Default

    Thx, i'll rename it.

  9. #9
    r035198x is offline Senior Member
    Join Date
    Aug 2009
    Posts
    2,388
    Rep Power
    7

    Default

    Quote Originally Posted by camickr View Post
    Would just like to add that the AWT package already has a class called "Menu". So you should rename yours to avoid confusion.
    While we are at it ....
    Your Main class extends JFrame for no apparent reason. Your Main class isn't a JFrame at all it is?

  10. #10
    Kligham is offline Member
    Join Date
    Sep 2009
    Posts
    30
    Rep Power
    0

    Default

    Thx to let me know. I removed it, and it still works. I added that because I thought you needed to do that if you are creating a JFame, but apparently not.

  11. #11
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,450
    Rep Power
    18

    Default

    You also ought to look at the tutorials for Swing (possibly even run through the straight Java ones as well). Especially this bit for the Swing stuff, since you may as well write this stuff properly from the getgo.

  12. #12
    Kligham is offline Member
    Join Date
    Sep 2009
    Posts
    30
    Rep Power
    0

    Default

    What do you mean with getgo? + I don't fully understand the articles about the threads. Can you maybe point out about what is wrong with the program I wrote (in function of those treads) and what I should look out for?

  13. #13
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,450
    Rep Power
    18

    Default

    From the getgo...from the start.
    I knew I shouldn't have typed that as I was posting it. Oh well.

    The last link (about threads) shows how you ought to launch a Swing app:

    Java Code:
    SwingUtilities.invokeLater(new Runnable() {
        public void run() {
            createAndShowGUI();
        }
    });
    This would be in your main() method and the createAndShowGUI() method would be a static method containing all the code you currently have in main(). It's all about how the thread that the Swing GUI works under (the event dispatcher thread), works.

  14. #14
    Kligham is offline Member
    Join Date
    Sep 2009
    Posts
    30
    Rep Power
    0

    Default

    Quote Originally Posted by Tolls View Post
    From the getgo...from the start.
    I knew I shouldn't have typed that as I was posting it. Oh well.

    The last link (about threads) shows how you ought to launch a Swing app:

    Java Code:
    SwingUtilities.invokeLater(new Runnable() {
        public void run() {
            createAndShowGUI();
        }
    });
    This would be in your main() method and the createAndShowGUI() method would be a static method containing all the code you currently have in main(). It's all about how the thread that the Swing GUI works under (the event dispatcher thread), works.
    So you mean that I need to type it this way then?

    Java Code:
    package test;
    
    import javax.swing.JFrame;
    
    public class Main extends JFrame{
    
        public void createAndShowGUI(){
    
            JFrame frame = new JFrame();
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                // construct a Menu object...
            Menu menu = new Menu();
                // ...and obtain its menu bar
            frame.setJMenuBar(menu.getMenuBar());
    	
            frame.pack();
            frame.setSize(500,500);
            frame.setVisible(true);
    
    
        }
    
        public static void main(String[] args) {
                
            SwingUtilities.invokeLater(new Runnable() {
                public void run() {
                    createAndShowGUI();
        }
    });
    
    }
    }
    And do I only have to do this in my main class or everywhere I would have a part of GUI-stuff that needs to be created if some event has been triggered?

  15. #15
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,450
    Rep Power
    18

    Default

    Yep. That's what you'll find on all the Swing tutorials, as far as I can remember.
    It's purely a Swing thing so that all GUI stuff is run on the Event thread. The event thread is also where all events occur, so you don't have to worry about that after this.

    There is a similar thing, though, when you are doing some heavy non-GUI processing as the result of an event (say a database search) where you would create a worker thread, but that's probably more than you need to know at the moment. (It's a bit further on on that Swing threading tutorial).

Similar Threads

  1. Replies: 0
    Last Post: 07-07-2008, 04:47 PM
  2. How to use SWT menu and menu event
    By Java Tip in forum SWT
    Replies: 0
    Last Post: 07-07-2008, 04:46 PM
  3. React to menu action and checkbox menu
    By Java Tip in forum javax.swing
    Replies: 0
    Last Post: 06-27-2008, 07:50 PM
  4. Replies: 7
    Last Post: 05-09-2008, 07:54 AM
  5. Help with menu class
    By mathias in forum New To Java
    Replies: 1
    Last Post: 08-07-2007, 06:45 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
  •