Why I can NOT have a class inside a method?

S

Shawn

Hi,

I have a method. Inside it, I need a ActionListener. The code is like:

public class DataMgr
{
private JFileChooser chooser = new JFileChooser();

public void saveData()
{
chooser.addActionListener(new SaveFileListener(chooser));
if ( chooser.showSaveDialog(new JPanel()) ==
JFileChooser.APPROVE_OPTION) //user clicked Save button
...//some code


final class SaveFileListener implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
...//some code
}
} //end of class SaveFileListener

} //end of method saveData
} //end of class DataMgr

Let me explain my code. I need to save something using JFileChooser to
pops up a gui which lets the user to select the path and input file
name. Unfortunately, if the user selects an existing file, the program
just QUIETLY overwrite it without any warning. So I googled and found
that I have to implement my own listener (e.g. SaveFileListener) to
achieve such a feature. I am surprised Sun doesn't provide such a
feature since it is so obviously needed.

Because the class SaveFileListener is only needed inside the method
saveData(), none of other methods need or care its existence, I hope to
put the class inside the method scope to make the method self-contained.
Then I run into the problem:
first, compiler says that modifier can only be abstract or final. I
changed private to final.
Then, compiler says the following line cannot find the type
SaveFileListener.
chooser.addActionListener(new SaveFileListener(chooser));

My plan is correct or not? How can I achieve my plan? Thank you very much.

I assume if I put the class SaveFileListener outside the method scope
saveData(), things will be fine. But I think that way unrelated code
will clutter the programmer's brain.

Thank you very much.
 
S

Steve Wampler

Shawn said:
Hi,

I have a method. Inside it, I need a ActionListener. The code is like: ....
Let me explain my code. I need to save something using JFileChooser to
pops up a gui which lets the user to select the path and input file
name. Unfortunately, if the user selects an existing file, the program
just QUIETLY overwrite it without any warning. So I googled and found
that I have to implement my own listener (e.g. SaveFileListener) to
achieve such a feature. I am surprised Sun doesn't provide such a
feature since it is so obviously needed.

Read about anonymous inner classes.
 
K

Knute Johnson

Shawn said:
Hi,

I have a method. Inside it, I need a ActionListener. The code is like:

public class DataMgr
{

Should be class SaveFileListener extends ActionListener or
ActionListener saveFileListener = new ActionListener() {
in any case it must be defined before you use it in your code
 
O

Oliver Wong

Shawn said:
public void saveData()
{
chooser.addActionListener(new SaveFileListener(chooser));
if ( chooser.showSaveDialog(new JPanel()) == JFileChooser.APPROVE_OPTION)
//user clicked Save button
...//some code


final class SaveFileListener implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
...//some code
}
} //end of class SaveFileListener

} //end of method saveData [...]
Then, compiler says the following line cannot find the type
SaveFileListener.
chooser.addActionListener(new SaveFileListener(chooser));

Did you try putting the class declaration before its usage?

- Oliver
 
S

Shawn

Shawn said:
Hi,

I have a method. Inside it, I need a ActionListener. The code is like:

public class DataMgr
{
private JFileChooser chooser = new JFileChooser();

public void saveData()
{
chooser.addActionListener(new SaveFileListener(chooser));
if ( chooser.showSaveDialog(new JPanel()) ==
JFileChooser.APPROVE_OPTION) //user clicked Save button
...//some code


final class SaveFileListener implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
...//some code
}
} //end of class SaveFileListener

} //end of method saveData
} //end of class DataMgr

Let me explain my code. I need to save something using JFileChooser to
pops up a gui which lets the user to select the path and input file
name. Unfortunately, if the user selects an existing file, the program
just QUIETLY overwrite it without any warning. So I googled and found
that I have to implement my own listener (e.g. SaveFileListener) to
achieve such a feature. I am surprised Sun doesn't provide such a
feature since it is so obviously needed.

Because the class SaveFileListener is only needed inside the method
saveData(), none of other methods need or care its existence, I hope to
put the class inside the method scope to make the method self-contained.
Then I run into the problem:
first, compiler says that modifier can only be abstract or final. I
changed private to final.
Then, compiler says the following line cannot find the type
SaveFileListener.
chooser.addActionListener(new SaveFileListener(chooser));

My plan is correct or not? How can I achieve my plan? Thank you very much.

I assume if I put the class SaveFileListener outside the method scope
saveData(), things will be fine. But I think that way unrelated code
will clutter the programmer's brain.

Thank you very much.

Thank you all. Yes, I put the class definition in the end (right before
the end of the method scope } ). Now, if I put the inner class in the
beginning part of the method, then when I use it, no error at all.

Thank you all for your help.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,535
Members
45,007
Latest member
obedient dusk

Latest Threads

Top