Hello,
I am posting reguarding some modifications I made to a text editor program that I wrote in my java class at my school today. The file I was modifying is a very simple java text editor with a "JMenuBar" object, with "JMenu" objects, such as "File" with "JMenuItem" objects such as "Save". The Modifications that I am attempting to make to the program are designed to give actual functionality to the "JMenuItems ", starting with the "Save" object. I am implementing the functionality with ActionListener classes. I have run into an error that I am unsure how to correct while I was attempting to set up the FileWriter code I was planning to use to implement "Save". When I attempt to compile, I get the following error
Here is the portion of code that throws the compiler error:
And here is the method that is being called from the Actionlistener:
And here is the program in its entirety:
Please ignore the comments reguarding my academic class, they are meant for my Professor and other students reading my code. Any help that could be provided on this problem would be greatly appreciated, because I am quite honestly stumped, as whenever I try to apply the "throws IOException" statement directly to the actionListener, there is a compiler error, I thought by applying it to the method, I would be eliminating the problem. So thanks in advance to anyone who helps me out here.
I am posting reguarding some modifications I made to a text editor program that I wrote in my java class at my school today. The file I was modifying is a very simple java text editor with a "JMenuBar" object, with "JMenu" objects, such as "File" with "JMenuItem" objects such as "Save". The Modifications that I am attempting to make to the program are designed to give actual functionality to the "JMenuItems ", starting with the "Save" object. I am implementing the functionality with ActionListener classes. I have run into an error that I am unsure how to correct while I was attempting to set up the FileWriter code I was planning to use to implement "Save". When I attempt to compile, I get the following error
Code:
/home/vontux/Swing2/src/Editor.java:55: unreported exception java.io.IOException; must be caught or declared to be thrown
Code:
mi_File_Save.addActionListener( new ActionListener() { public void actionPerformed(ActionEvent e) { //System.exit(0); //final JFileChooser fc = new JFileChooser(); int returnVal = fc.showSaveDialog(Editor.this); SaveOutput(returnVal);//this line right here is what kicks out the error //String nfile; //final JFileChooser fc = new JFileChooser(); // int returnVal = fc.showOpenDialog(e); } });//not part of class examples
Code:
private void SaveOutput(int returnVal) throws IOException { try { String nfile; if(returnVal == JFileChooser.APPROVE_OPTION) { nfile = fc.getSelectedFile().toString(); String text = jtaNotes.getText(); FileWriter fw = new FileWriter(nfile); fw.close(); } } catch(IOException SIOxcp) { JOptionPane.showMessageDialog(null,"Error","an IOException was Thrown",JOptionPane.ERROR_MESSAGE); } }
Code:
/* * Editor.java * * Created on February 13, 2007, 1:05 PM * * To change this template, choose Tools | Template Manager * and open the template in the editor. */ /** * * @author vontux */ import java.awt.*; import java.awt.event.*; import javax.swing.*; import java.io.*;//not part of class example code public class Editor extends JFrame { private JTextArea jtaNotes = new JTextArea(24,60);//it will only be able to scroll private JScrollPane jspNotes = new JScrollPane(jtaNotes);//pass component to the scroll area you want to be scrollable final JFileChooser fc = new JFileChooser();//not part of class code /** Creates a new instance of Editor */ public Editor() throws java.io.IOException { setTitle("MyEditor"); //adding the menus JMenuBar jmb = new JMenuBar();//where the JMenu's go JMenu m_file = new JMenu("File"); JMenu m_Edit = new JMenu("Edit"); JMenu m_Search = new JMenu("Search"); JMenu m_Help = new JMenu("Help"); JMenuItem mi_File_Exit = new JMenuItem("Exit"); JMenuItem mi_File_Save = new JMenuItem("Save"); JMenuItem mi_File_SaveAs = new JMenuItem("Save As"); JMenuItem mi_File_Open = new JMenuItem("Open"); mi_File_Exit.addActionListener( new ActionListener() { public void actionPerformed(ActionEvent e) { System.exit(0); } }); mi_File_Save.addActionListener( new ActionListener() { public void actionPerformed(ActionEvent e) { //System.exit(0); //final JFileChooser fc = new JFileChooser(); int returnVal = fc.showSaveDialog(Editor.this); SaveOutput(returnVal);//this line right here is what kicks out the error //String nfile; //final JFileChooser fc = new JFileChooser(); // int returnVal = fc.showOpenDialog(e); } });//not part of class examples m_file.add(mi_File_Open);//not part of class examples m_file.add(mi_File_Save);//not part of class examples m_file.add(mi_File_SaveAs);//not part of class examples m_file.add(mi_File_Exit);//adds a menu item to the specified JMenu object jmb.add(m_file);//these add the Menu's to the JMenu jmb.add(m_Edit); jmb.add( m_Search); jmb.add(m_Help); setJMenuBar( jmb );//adding the JMenuBar object initializing it I suppose setBackground(Color.GREEN);//sets background of the form jtaNotes.setBackground(Color.GREEN);//sets bg color of the jtaNotes Object jtaNotes.setFont( new Font("Times New Roman", Font.BOLD, 14)); getContentPane().add(jspNotes, BorderLayout.CENTER);//getContentPane() is a container which gives access to other methods //setSize(300,400);//sets a default size for the window //pack();//autoadjusts based on the content addWindowListener(new WindowAdapter() { public void windowClosing(WindowEvent e) { System.exit(0);//allows you to close the jvm process by clicking the red x as opposed to simply hiding it } }); pack();//autoadjusts based on the size of contents of it, in this case the size of the JScrollArea show(); } public static void main(String[] args) throws java.io.IOException { try { new Editor(); } catch(IOException IOxcp) { JOptionPane.showMessageDialog(null,"Error","an IOException was Thrown",JOptionPane.ERROR_MESSAGE); } } private void SaveOutput(int returnVal) throws IOException { try { String nfile; if(returnVal == JFileChooser.APPROVE_OPTION) { nfile = fc.getSelectedFile().toString(); String text = jtaNotes.getText(); FileWriter fw = new FileWriter(nfile); fw.close(); } } catch(IOException SIOxcp) { JOptionPane.showMessageDialog(null,"Error","an IOException was Thrown",JOptionPane.ERROR_MESSAGE); } } }
Comment