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 - /home/vontux/Swing2/src/Editor.java:55: unreported exception java.io.IOException; must be caught or declared to be thrown
Here is the portion of code that throws the compiler error: - 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
And here is the method that is being called from the Actionlistener: - 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);
-
}
-
-
}
And here is the program in its entirety: - /*
-
* 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);
-
}
-
-
}
-
-
}
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.
4 3619
First of all either delete the "throws IOException " line,
or rewrite catch as
both place IOException is not the syntax.Your errors will get resolved,hopefully. - /home/vontux/Swing2/src/Editor.java:55: unreported exception java.io.IOException; must be caught or declared to be thrown
- 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);
-
}
-
-
}
.
dont try to add any throws after actionListener.Because you are overriding the function,so you cannot change the signature according to your need.That is not petmitted.
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.
Thank you, your deletion and modification suggestions solved the problem. Just out of curiosity, how do the "throws java.io.IOException" statements override the actionListener?
Thank you, your deletion and modification suggestions solved the problem. Just out of curiosity, how do the "throws java.io.IOException" statements override the actionListener?
The throws clause does not override anything. When writing code that can throw an exception which you want to handle, either put that code in a try block and catch the exception or declare that block of code as throws...whatever exception. Do not use both.
it does not override actionListener.
here you are overriding actionListener when you are using it in your code.
actionListener is defined in an interface which you access by writing -
implements actionListener
-
at appropriate places.
Thank you, your deletion and modification suggestions solved the problem. Just out of curiosity, how do the "throws java.io.IOException" statements override the actionListener?
Sign in to post your reply or Sign up for a free account.
Similar topics
by: ptaz |
last post by:
Hi I'm trying to run a web page but I get the following error. Ca
anyone please tell me a solution to this.
Thanks
Ptaz
HTTP Status 500 -
type Exception report
|
by: Gary.Hu |
last post by:
I was trying to catch the Arithmetic exception, unsuccessfully.
try{
int a = 0, b = 9;
b = b / a;
}catch(...){
cout << "arithmetic exception was catched!" << endl;
}
After ran the program,...
|
by: Steph |
last post by:
I am working on a Windows Forms App. I have attached a standard
UnhandledExceptionEventHandler to the current domain to catch
unexpected errors. In my application, when I trigger an unhandled...
|
by: Pardhasaradhy |
last post by:
Hi,
Please see the following error and revert back as
early as possible. I am getting this once I request for
the asp.net page.
Server Error in '/tanishq' Application.
Timed out waiting for...
|
by: Marco Viana |
last post by:
Hi,
I'm developing an ASP.NET application with Visual Studio .NET 2003 in
a Win XP Professional, .NET Framework 1.1 and IIS 5.1 computer with
all the lattest patches.
When testing a page...
|
by: Zorro |
last post by:
The simplicity of stack unraveling of C++ is not without defective
consequences. The following article points to C++ examples showing the
defects. An engineer aware of defects can avoid...
|
by: shana07 |
last post by:
I have unreported exceptions as below:
unreported exception java.lang.Exception; must be caught or declared to be thrown
Euclid euc = new Euclid();
...
|
by: George2 |
last post by:
Hello everyone,
How do you understand the Bjarne's comments about exception
specification? Especially, "not required to be checked across
compilation-unit" and "violations will not be caught...
|
by: Ioannis Vranos |
last post by:
Perhaps a mechanism can be introduced in the C++0x/1x standard,
something simple like defining a function as:
void somefunc(void) throw()
{
// ...
}
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
by: ryjfgjl |
last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
|
by: ryjfgjl |
last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
|
by: emmanuelkatto |
last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud.
Please let me know.
Thanks!
Emmanuel
|
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...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
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,...
|
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...
|
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...
| |