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 userinputdialog box to ask the user the name of file and
then called createNewFile() but it declares an exception and the actionPerformed Performed cannot throw IOException. what to do?
You can take a look on the source code of my program:
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?
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 userinputdialog box to ask the user the name of file and
then called createNewFile() but it declares an exception and the actionPerformed Performed cannot throw IOException. what to do?
You can take a look on the source code of my program:
Code:
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.io.*;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JRadioButtonMenuItem;
import javax.swing.JOptionPane;
class MenuActionListener implements ActionListener {
public void actionPerformed(ActionEvent actionEvent) {
JFrame fr= new JFrame();
JOptionPane.showMessageDialog(fr,"A DialogBOX");
}
}
public class MenuAction {
public static void main( String args[])throws IOException {
String name;
ActionListener menuListener = new MenuActionListener();
MenuAction mes=new MenuAction();
JFrame frame = new JFrame("MenuSample Example");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JMenuBar menuBar = new JMenuBar();
JMenu ab = new JMenu("A menu");
ab.setMnemonic(KeyEvent.VK_A);
menuBar.add(ab);
JMenu mn= new JMenu("New");ab.addSeparator();
mn.setMnemonic(KeyEvent.VK_N);
JMenuItem men = new JMenuItem("Dialog Box");
men.addActionListener(menuListener);
mn.add(men);
ab.add(mn);
JMenuItem file = new JMenuItem("File");
file.setMnemonic(KeyEvent.VK_F);
file.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent actionEvent) {
JFrame framea=new JFrame();
int messageType = JOptionPane.INFORMATION_MESSAGE;
String stra = new JOptionPane.showInputDialog(framea,"File Name?","File dialog",messageType);
File f = new File("E:\\"+stra+".txt");
f.createNewFile();}
});
mn.add(file);
JRadioButtonMenuItem it = new JRadioButtonMenuItem("Radio button");
it.setSelected(true);
it.setMnemonic(KeyEvent.VK_R);
it.addActionListener(menuListener);
ab.add(it);
frame.setJMenuBar(menuBar);
frame.setSize(350, 250);
frame.setVisible(true);
}
}