473,405 Members | 2,187 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,405 software developers and data experts.

how to make actionPerformed method throw exceptions?

Hi,
I am trying to design a JMenuItem which creates textfiles.
See line 39 .I have declared a Jmenuitem named file.
the problem is here:-the actionPerformed() connected to the JmenuItem cannot throw IOException.I made use of a userinputdialogbox to ask the user the name of file and
then called createNewFile() but it declares an exception and the actionPerformedPerformed cannot throw IOException. what to do?
You can take a look on the source code of my program:

Expand|Select|Wrap|Line Numbers
  1. import java.awt.event.ActionEvent;
  2. import java.awt.event.ActionListener;
  3. import java.awt.event.KeyEvent;
  4. import java.io.*;
  5. import javax.swing.JFrame;
  6. import javax.swing.JMenu;
  7. import javax.swing.JMenuBar;
  8. import javax.swing.JMenuItem;
  9. import javax.swing.JRadioButtonMenuItem;
  10. import javax.swing.JOptionPane;
  11. class MenuActionListener implements ActionListener {
  12.     public void actionPerformed(ActionEvent actionEvent) {
  13.         JFrame fr= new JFrame();
  14.         JOptionPane.showMessageDialog(fr,"A DialogBOX");
  15.     }
  16. }
  17.  
  18. public class MenuAction {
  19.     public static void main( String args[])throws IOException {
  20.         String name;
  21.         ActionListener menuListener = new MenuActionListener();
  22.         MenuAction mes=new MenuAction();
  23.         JFrame frame = new JFrame("MenuSample Example");
  24.         frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  25.         JMenuBar menuBar = new JMenuBar();
  26.  
  27.         JMenu ab = new JMenu("A menu");
  28.         ab.setMnemonic(KeyEvent.VK_A);
  29.         menuBar.add(ab);
  30.  
  31.         JMenu mn= new JMenu("New");ab.addSeparator();
  32.         mn.setMnemonic(KeyEvent.VK_N);
  33.  
  34.         JMenuItem men = new JMenuItem("Dialog Box");
  35.         men.addActionListener(menuListener);
  36.         mn.add(men);
  37.         ab.add(mn);
  38.  
  39.         JMenuItem file = new JMenuItem("File");
  40.         file.setMnemonic(KeyEvent.VK_F);
  41.         file.addActionListener(new ActionListener(){
  42.                 public void actionPerformed(ActionEvent actionEvent) {
  43.                     JFrame framea=new JFrame();
  44.                     int messageType = JOptionPane.INFORMATION_MESSAGE;
  45. String stra = new JOptionPane.showInputDialog(framea,"File Name?","File dialog",messageType);                    
  46. File f = new File("E:\\"+stra+".txt");
  47.                     f.createNewFile();}
  48.                });
  49.  
  50.         mn.add(file);
  51.  
  52.         JRadioButtonMenuItem it = new JRadioButtonMenuItem("Radio button");
  53.         it.setSelected(true);
  54.  
  55.         it.setMnemonic(KeyEvent.VK_R);
  56.         it.addActionListener(menuListener);
  57.         ab.add(it);
  58.  
  59.         frame.setJMenuBar(menuBar);
  60.         frame.setSize(350, 250);
  61.         frame.setVisible(true);
  62.     }
  63. }
I wrote this program when I was learning menus so I have added a submenu too in my menu.Can anybody help me in this case? Is there any alternative option?
Jun 19 '11 #1
0 1553

Sign in to post your reply or Sign up for a free account.

Similar topics

4
by: Boogie El Aceitoso | last post by:
Hi, I have an exception class for winapi errors (returns a formated error mesage, usign ::GetLastError() et al, with what()). It looks like this: class WinapiException : public...
4
by: Jurko Gospodnetić | last post by:
Hi all. I was wondering. Can the standard basic_string<> c_str() member function throw any exceptions? Is it perhaps implementation dependent? I tried checking the standard and as far as I...
1
by: Bruno van Dooren | last post by:
Hi, i am using some STL containers in a library of mine. how can i see which exceptions can occur when doing something with those containers (adding an elemnt for example) i have looked in...
8
by: cat | last post by:
I had a long and heated discussion with other developers on my team on when it makes sense to throw an exception and when to use an alternate solution. The .NET documentation recommends that an...
6
by: Henryk | last post by:
I did a lot of delphi GUI programming recently. In my experience most of the time you just want to throw a standard exception with a descriptive message. Then all calling functions can handle...
5
by: Gary Wessle | last post by:
Hi if I have a method like this bool myClass::myMothod(myType& mt, herType ht) throw ( SomeExp ); how can I use it in a conditional statement if ( try {
5
by: Olaf Rabbachin | last post by:
Hi folks, I have a (VB.Net-) DLL that I'm using from MS Access 2003. Everything's pretty fine except for one thing - When I throw or pass on exceptions from within the .Net-DLL, all I get is Err...
2
by: lukasz | last post by:
Hey is there a way to make certain method be called whenever any other method in my program is called (such global method hook), usable e.g. for logging or debugging?
1
by: killy971 | last post by:
I am using the following syntax, with Xalan, to process xml transformations in java: StreamSource xml = new StreamSource(xmlFile); StreamSource xsl = new StreamSource(xslFile); StreamResult out...
5
billiy321
by: billiy321 | last post by:
Hi I am trying to send a mail in C# without having to use an SMTP server(using SMTP service instead) with " cdoSendUsingPickup " configuration. But when i enter a invalid recipient adderss say...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.