944,098 Members | Top Members by Rank

Ad:
  • Java Discussion Thread
  • Unsolved
  • Views: 680
  • Java RSS
Nov 1st, 2009
0

MenuBar in swing not working :(

Expand Post »
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.

Java Syntax (Toggle Plain Text)
  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. }
Reputation Points: 10
Solved Threads: 1
Newbie Poster
sush27 is offline Offline
13 posts
since Nov 2009
Nov 1st, 2009
0
Re: MenuBar in swing not working :(
Here is working sample based on code provided by you
Java Syntax (Toggle Plain Text)
  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. }
Moderator
Featured Poster
Reputation Points: 2786
Solved Threads: 874
Code tags enforcer
peter_budo is offline Offline
6,659 posts
since Dec 2004
Nov 2nd, 2009
0
Re: MenuBar in swing not working :(
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.
Java Syntax (Toggle Plain Text)
  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??
Reputation Points: 10
Solved Threads: 1
Newbie Poster
sush27 is offline Offline
13 posts
since Nov 2009
Nov 2nd, 2009
-1
Re: MenuBar in swing not working :(
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.
Moderator
Featured Poster
Reputation Points: 2786
Solved Threads: 874
Code tags enforcer
peter_budo is offline Offline
6,659 posts
since Dec 2004

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Java Forum Timeline: Scan web pages for attributes.
Next Thread in Java Forum Timeline: JAR works bue EXE doesn't





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC