MenuBar in swing not working :(

Please support our Java advertiser: Programming Forums - DaniWeb Sister Site
Reply

Join Date: Nov 2009
Posts: 10
Reputation: sush27 is an unknown quantity at this point 
Solved Threads: 1
sush27 sush27 is offline Offline
Newbie Poster

MenuBar in swing not working :(

 
0
  #1
34 Days Ago
hi...
i am new to this community, lately i was looking for some help in my code and this website came to my rescue in many topic.
i am just implementing the menubar in swing, can somebody help me in my code.

  1. import java.lang.*;
  2. import java.awt.*;
  3. import java.io.*;
  4. import java.awt.event.*;
  5. import javax.swing.*;
  6. import javax.swing.event.*;
  7. import java.text.*;
  8. import java.util.*;
  9.  
  10. public class FrameExample extends JFrame {
  11.  
  12. private JMenuBar menuBar;
  13. private JMenu fileMenu;
  14. private JMenu optionMenu;
  15. private JMenuItem newFileItem;
  16. private JMenuItem openFileItem;
  17. private JMenuItem exitItem;
  18.  
  19. public FrameExample()
  20. {
  21. menuBar = new JMenuBar();
  22. setJMenuBar(menuBar);
  23. MyMenuHandler handler = new MyMenuHandler();
  24.  
  25. newFileItem=new JMenuItem();
  26. newFileItem.setText("New");
  27. newFileItem.setToolTipText("Click to open new file");
  28. newFileItem.setActionCommand("new");
  29. newFileItem.addActionListener(handler(this));
  30.  
  31. openFileItem=new JMenuItem();
  32. openFileItem.setText("Open");
  33. openFileItem.setToolTipText("Click to open existing file");
  34. openFileItem.setActionCommand("open");
  35. openFileItem.addActionListener(handler(this));
  36.  
  37. exitItem=new JMenuItem();
  38. exitItem.setText("Exit");
  39. exitItem.setToolTipText("Click to Exit");
  40. exitItem.setActionCommand("exit");
  41. exitItem.addActionListener(handler(this));
  42.  
  43. fileMenu=new JMenu();
  44. fileMenu.add(newFileItem);
  45. fileMenu.add(openFileItem);
  46. fileMenu.add(exitItem);
  47.  
  48. addWindowListener(new MyWindowAdapter());
  49. setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
  50. }
  51.  
  52. public static void main(String[] args)
  53. {
  54. FrameExample frame=new FrameExample();
  55. frame.setSize(200,200);
  56. frame.setTitle("A Frame");
  57. frame.setVisible(true);
  58.  
  59. }
  60.  
  61. class MyMenuHandler extends ActionListener{
  62. FrameExample f;
  63.  
  64.  
  65. public void actionPerformed(ActionEvent ae)
  66. {
  67. String arg=(String)ae.getActionCommand();
  68.  
  69. if(arg.equals("new"))
  70. {
  71. JOptionPane.showMessageDialog(FrameExample.this, "New File Chosen", JOptionPane.CANCEL_OPTION);
  72. }
  73. }
  74.  
  75. }
  76. }
  77. class MyWindowAdapter extends WindowAdapter{
  78.  
  79. public void windowClosing(WindowEvent we)
  80. {
  81. int result = JOptionPane.showConfirmDialog( FrameExample.this, "Do you really Want to exit?", "Confirmation", JOptionPane.YES_NO_OPTION );
  82. if( result == JOptionPane.YES_OPTION )
  83. {
  84. System.exit( 0 );
  85. }
  86. }
  87.  
  88. }
Reply With Quote Quick reply to this message  
Join Date: Dec 2004
Posts: 4,208
Reputation: peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of 
Solved Threads: 486
Moderator
Featured Poster
peter_budo's Avatar
peter_budo peter_budo is online now Online
Code tags enforcer
 
0
  #2
34 Days Ago
Here is working sample based on code provided by you
  1. import java.lang.*;
  2. import java.awt.*;
  3. import java.io.*;
  4. import java.awt.event.*;
  5. import javax.swing.*;
  6. import javax.swing.event.*;
  7. import java.text.*;
  8. import java.util.*;
  9.  
  10. public class FrameExample{
  11.  
  12. private JFrame frame;
  13. private JMenuBar menuBar;
  14. private JMenu fileMenu;
  15. private JMenu optionMenu;
  16. private JMenuItem newFileItem;
  17. private JMenuItem openFileItem;
  18. private JMenuItem exitItem;
  19.  
  20. public FrameExample()
  21. {
  22. frame = new JFrame();
  23. menuBar = new JMenuBar();
  24. frame.setJMenuBar(menuBar);
  25.  
  26. newFileItem=new JMenuItem();
  27. newFileItem.setText("New");
  28. newFileItem.setToolTipText("Click to open new file");
  29. newFileItem.setActionCommand("new");
  30. newFileItem.addActionListener(new ActionListener(){
  31. public void actionPerformed(ActionEvent ae){
  32. JOptionPane.showMessageDialog(frame, "New File Chosen","New File", JOptionPane.CANCEL_OPTION);
  33. /*Be carefull with declaration you used only 3 arguments with this method
  34.   *where there is option only for 2, 4, 5 and 8 arguments.
  35.   */
  36. }
  37. });
  38.  
  39. openFileItem=new JMenuItem();
  40. openFileItem.setText("Open");
  41. openFileItem.setToolTipText("Click to open existing file");
  42. openFileItem.setActionCommand("open");
  43. openFileItem.addActionListener(new ActionListener(){
  44. public void actionPerformed(ActionEvent ae){
  45. //action here
  46. }
  47. });
  48.  
  49. exitItem=new JMenuItem();
  50. exitItem.setText("Exit");
  51. exitItem.setToolTipText("Click to Exit");
  52. exitItem.setActionCommand("exit");
  53. exitItem.addActionListener(new ActionListener(){
  54. public void actionPerformed(ActionEvent ae){
  55. //action here
  56. }
  57. });
  58.  
  59. fileMenu=new JMenu("File");//missing menu name it would be not visible even if added to JMenuBar
  60. fileMenu.add(newFileItem);
  61. fileMenu.add(openFileItem);
  62. fileMenu.add(exitItem);
  63. menuBar.add(fileMenu);//never added to JMenuBar
  64. frame.setJMenuBar(menuBar);//JMenuBar never added to frame
  65.  
  66. frame.addWindowListener(new WindowAdapter(){
  67. public void windowClosing(WindowEvent we){
  68. int result = JOptionPane.showConfirmDialog( frame, "Do you really Want to exit?", "Confirmation", JOptionPane.YES_NO_OPTION );
  69. if( result == JOptionPane.YES_OPTION )
  70. {
  71. System.exit( 0 );
  72. }
  73. }
  74. });
  75. frame.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
  76. frame.setSize(400,400);
  77. frame.setTitle("A Frame");
  78. frame.setVisible(true);
  79. }
  80.  
  81. public static void main(String[] args)
  82. {
  83. SwingUtilities.invokeLater(new Runnable(){
  84. public void run(){
  85. new FrameExample();
  86. }
  87. });
  88. }
  89. }
Learn to see in another's calamity the ills which you should avoid.
Publilius Syrus
(~100 BC)

LJC - London Java Community, Graduate & Undergraduate Software Development Community, JAVAWUG (Java Web User Group), The London Android Group
Reply With Quote Quick reply to this message  
Join Date: Nov 2009
Posts: 10
Reputation: sush27 is an unknown quantity at this point 
Solved Threads: 1
sush27 sush27 is offline Offline
Newbie Poster
 
0
  #3
34 Days Ago
thanks peter_budo

yup its working now, but can you tell whats the problem in creating a new handler(MyMenuHandler in my code) to handle Action Event of menu items.
  1. openFileItem.addActionListener(new ActionListener(){
  2. public void actionPerformed(ActionEvent ae){
  3. //action here
  4. }
  5. });
this kinda code lookss little clumsy.
is there any way getting around this??
Reply With Quote Quick reply to this message  
Join Date: Dec 2004
Posts: 4,208
Reputation: peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of 
Solved Threads: 486
Moderator
Featured Poster
peter_budo's Avatar
peter_budo peter_budo is online now Online
Code tags enforcer
 
-1
  #4
34 Days Ago
There are two ways to handle generated events:
A) You will create single general actionPerformed() method which can be encapsulated in event handler as you tried and then with you of loop and if statement try to figure out which event was triggered. This approach makes your code short as you do not have to repeat some lines over and over. Unfortunately with number of events attached to this approach you are running risk of making mistake in event recognition and spending long time checking out if the logic inside is working as you expect.

B) Do it the way as showed in my code by providing actionPerformed() to every object that need it. By doing so you code will be longer as some code will be repeated. However when some action doesn't work as you intended you just go to specific section of code and check out "what the hell is going on!"

Consider what is better for you, as you can see I prefer option B for its easy debugging.
Learn to see in another's calamity the ills which you should avoid.
Publilius Syrus
(~100 BC)

LJC - London Java Community, Graduate & Undergraduate Software Development Community, JAVAWUG (Java Web User Group), The London Android Group
Reply With Quote Quick reply to this message  
Reply

Message:



Other Threads in the Java Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC