Results 1 to 15 of 15
Thread: Menu class
- 09-26-2009, 10:36 PM #1
Member
- Join Date
- Sep 2009
- Posts
- 30
- Rep Power
- 0
Menu class
Hallo,
I have these two classes atm:
and: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); } }
Netbeans gives me 3 error signs, I underlined them in my code. What am I doing wrong?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); } }Last edited by Kligham; 09-26-2009 at 10:40 PM.
- 09-27-2009, 12:09 AM #2
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
- 09-27-2009, 12:10 AM #3
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,547
- Rep Power
- 11
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.
- 09-27-2009, 01:49 AM #4
Member
- Join Date
- Sep 2009
- Posts
- 30
- Rep Power
- 0
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:
Menu.java: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); } }
The error that netbeans gives me is shown on the pic that I added.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); } }
+ Can anyone explain me what KeyEvent.VK_T does? I see it at alot of websites, but the never explain what it does.Last edited by Kligham; 09-27-2009 at 12:20 PM.
- 09-27-2009, 03:24 AM #5
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,547
- Rep Power
- 11
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.
- 09-27-2009, 01:07 PM #6
Member
- Join Date
- Sep 2009
- Posts
- 30
- Rep Power
- 0
Hi,
Thanks for the help mate. I've got a better insight on constructors as well. I works as it should be.
- 09-27-2009, 04:39 PM #7
Senior Member
- Join Date
- Jul 2009
- Posts
- 1,143
- Rep Power
- 5
Would just like to add that the AWT package already has a class called "Menu". So you should rename yours to avoid confusion.
- 09-27-2009, 07:33 PM #8
Member
- Join Date
- Sep 2009
- Posts
- 30
- Rep Power
- 0
Thx, i'll rename it.
- 09-28-2009, 07:48 AM #9
Senior Member
- Join Date
- Aug 2009
- Posts
- 2,388
- Rep Power
- 6
- 09-28-2009, 04:01 PM #10
Member
- Join Date
- Sep 2009
- Posts
- 30
- Rep Power
- 0
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.
- 09-28-2009, 04:26 PM #11
Moderator
- Join Date
- Apr 2009
- Posts
- 10,479
- Rep Power
- 16
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.
- 09-28-2009, 05:34 PM #12
Member
- Join Date
- Sep 2009
- Posts
- 30
- Rep Power
- 0
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?
- 09-28-2009, 05:46 PM #13
Moderator
- Join Date
- Apr 2009
- Posts
- 10,479
- Rep Power
- 16
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:
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.Java Code:SwingUtilities.invokeLater(new Runnable() { public void run() { createAndShowGUI(); } });
- 09-28-2009, 06:02 PM #14
Member
- Join Date
- Sep 2009
- Posts
- 30
- Rep Power
- 0
So you mean that I need to type it this way then?
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?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(); } }); } }
- 09-28-2009, 06:08 PM #15
Moderator
- Join Date
- Apr 2009
- Posts
- 10,479
- Rep Power
- 16
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
-
Fill a menu dynamically when menu is shown
By Java Tip in forum SWTReplies: 0Last Post: 07-07-2008, 04:47 PM -
How to use SWT menu and menu event
By Java Tip in forum SWTReplies: 0Last Post: 07-07-2008, 04:46 PM -
React to menu action and checkbox menu
By Java Tip in forum javax.swingReplies: 0Last Post: 06-27-2008, 07:50 PM -
how to create Popup Menu with Sub Menu while right-clicking the JTree Node??
By Kabiraa in forum AWT / SwingReplies: 7Last Post: 05-09-2008, 07:54 AM -
Help with menu class
By mathias in forum New To JavaReplies: 1Last Post: 08-07-2007, 06:45 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks