| | |
MenuBar in swing not working :(
Please support our Java advertiser: Programming Forums - DaniWeb Sister Site
![]() |
•
•
Join Date: Nov 2009
Posts: 10
Reputation:
Solved Threads: 1
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.
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)
import java.lang.*; import java.awt.*; import java.io.*; import java.awt.event.*; import javax.swing.*; import javax.swing.event.*; import java.text.*; import java.util.*; public class FrameExample extends JFrame { private JMenuBar menuBar; private JMenu fileMenu; private JMenu optionMenu; private JMenuItem newFileItem; private JMenuItem openFileItem; private JMenuItem exitItem; public FrameExample() { menuBar = new JMenuBar(); setJMenuBar(menuBar); MyMenuHandler handler = new MyMenuHandler(); newFileItem=new JMenuItem(); newFileItem.setText("New"); newFileItem.setToolTipText("Click to open new file"); newFileItem.setActionCommand("new"); newFileItem.addActionListener(handler(this)); openFileItem=new JMenuItem(); openFileItem.setText("Open"); openFileItem.setToolTipText("Click to open existing file"); openFileItem.setActionCommand("open"); openFileItem.addActionListener(handler(this)); exitItem=new JMenuItem(); exitItem.setText("Exit"); exitItem.setToolTipText("Click to Exit"); exitItem.setActionCommand("exit"); exitItem.addActionListener(handler(this)); fileMenu=new JMenu(); fileMenu.add(newFileItem); fileMenu.add(openFileItem); fileMenu.add(exitItem); addWindowListener(new MyWindowAdapter()); setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE); } public static void main(String[] args) { FrameExample frame=new FrameExample(); frame.setSize(200,200); frame.setTitle("A Frame"); frame.setVisible(true); } class MyMenuHandler extends ActionListener{ FrameExample f; public void actionPerformed(ActionEvent ae) { String arg=(String)ae.getActionCommand(); if(arg.equals("new")) { JOptionPane.showMessageDialog(FrameExample.this, "New File Chosen", JOptionPane.CANCEL_OPTION); } } } } class MyWindowAdapter extends WindowAdapter{ public void windowClosing(WindowEvent we) { int result = JOptionPane.showConfirmDialog( FrameExample.this, "Do you really Want to exit?", "Confirmation", JOptionPane.YES_NO_OPTION ); if( result == JOptionPane.YES_OPTION ) { System.exit( 0 ); } } }
0
#2 34 Days Ago
Here is working sample based on code provided by you
Java Syntax (Toggle Plain Text)
import java.lang.*; import java.awt.*; import java.io.*; import java.awt.event.*; import javax.swing.*; import javax.swing.event.*; import java.text.*; import java.util.*; public class FrameExample{ private JFrame frame; private JMenuBar menuBar; private JMenu fileMenu; private JMenu optionMenu; private JMenuItem newFileItem; private JMenuItem openFileItem; private JMenuItem exitItem; public FrameExample() { frame = new JFrame(); menuBar = new JMenuBar(); frame.setJMenuBar(menuBar); newFileItem=new JMenuItem(); newFileItem.setText("New"); newFileItem.setToolTipText("Click to open new file"); newFileItem.setActionCommand("new"); newFileItem.addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent ae){ JOptionPane.showMessageDialog(frame, "New File Chosen","New File", JOptionPane.CANCEL_OPTION); /*Be carefull with declaration you used only 3 arguments with this method *where there is option only for 2, 4, 5 and 8 arguments. */ } }); openFileItem=new JMenuItem(); openFileItem.setText("Open"); openFileItem.setToolTipText("Click to open existing file"); openFileItem.setActionCommand("open"); openFileItem.addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent ae){ //action here } }); exitItem=new JMenuItem(); exitItem.setText("Exit"); exitItem.setToolTipText("Click to Exit"); exitItem.setActionCommand("exit"); exitItem.addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent ae){ //action here } }); fileMenu=new JMenu("File");//missing menu name it would be not visible even if added to JMenuBar fileMenu.add(newFileItem); fileMenu.add(openFileItem); fileMenu.add(exitItem); menuBar.add(fileMenu);//never added to JMenuBar frame.setJMenuBar(menuBar);//JMenuBar never added to frame frame.addWindowListener(new WindowAdapter(){ public void windowClosing(WindowEvent we){ int result = JOptionPane.showConfirmDialog( frame, "Do you really Want to exit?", "Confirmation", JOptionPane.YES_NO_OPTION ); if( result == JOptionPane.YES_OPTION ) { System.exit( 0 ); } } }); frame.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE); frame.setSize(400,400); frame.setTitle("A Frame"); frame.setVisible(true); } public static void main(String[] args) { SwingUtilities.invokeLater(new Runnable(){ public void run(){ new FrameExample(); } }); } }
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
Publilius Syrus
(~100 BC)
LJC - London Java Community, Graduate & Undergraduate Software Development Community, JAVAWUG (Java Web User Group), The London Android Group
•
•
Join Date: Nov 2009
Posts: 10
Reputation:
Solved Threads: 1
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.
this kinda code lookss little clumsy.
is there any way getting around this??

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)
openFileItem.addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent ae){ //action here } });
is there any way getting around this??
-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.
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
Publilius Syrus
(~100 BC)
LJC - London Java Community, Graduate & Undergraduate Software Development Community, JAVAWUG (Java Web User Group), The London Android Group
![]() |
Other Threads in the Java Forum
- Previous Thread: Scan web pages for attributes.
- Next Thread: JAR works bue EXE doesn't
| Thread Tools | Search this Thread |
actuate android api applet application applications array arrays automation balls bank binary bluetooth business chat class classes clear client code codesnippet collections component database defaultmethod development dice dragging ebook eclipse error event exception formatingtextintooltipjava fractal froglogic game givemetehcodez graphics gui hql html ide image infinite input integer intersect invokingapacheantprogrammatically j2me java javaprojects jni jpanel julia linux list loop looping map method methods mobile mysql netbeans newbie numbers openjavafx parameter php print problem program programming project recursion repositories scanner screen scrollbar server set size sms sort sorting sql sqlserver state storm string sun superclass swing swt text-file threads time tree windows






