Solved

Help with saveAsMenuItem.addActionListener

Posted on 2004-04-04
12
288 Views
Last Modified: 2010-08-05
Someone here said it would be better to use the type of code below, other then ActionCommand:

saveAsMenuItem.addActionListener(new java.awt.event.ActionListener() {
      public void actionPerformed(ActionEvent e) {
        saveAsMenuItem_actionPerformed(e);
      }
    });

Could someone give me an example of how this would be used from a menu through to a SaveAs JFileChooser.
0
Comment
Question by:Drop_of_Rain
  • 5
  • 4
  • 2
  • +1
12 Comments
 
LVL 92

Assisted Solution

by:objects
objects earned 150 total points
ID: 10753682
as above and your saveAsMenuItem_actionPerformed method would perform the action:

private void saveAsMenuItem_actionPerformed(ActionEvent e)
{
   // perform action here
}
0
 
LVL 14

Assisted Solution

by:kennethxu
kennethxu earned 100 total points
ID: 10753720
>> Could someone give me an example of how this would be used from a menu through to a SaveAs JFileChooser.
http://sprocket.telab.artic.edu/ed/MotionSoftware/SourceDocs/TuningUtilityFrame_8java-source.html
0
 
LVL 92

Assisted Solution

by:objects
objects earned 150 total points
ID: 10753818
Heres some background on choosers, and how to popup thew chooser:

http://java.sun.com/docs/books/tutorial/uiswing/components/filechooser.html

private void saveAsMenuItem_actionPerformed(ActionEvent e)
{
  int return = chosser.showSaveDialog(this);
  if (return == JFileChooser.APPROVE_OPTION)
  {
      File file = chooser.getSelectedFile();
      // do the save
  }
}
0
 
LVL 15

Assisted Solution

by:Javatm
Javatm earned 250 total points
ID: 10753864
Hi :

Here is my share :


  // Here is your Action Listener . . .

  saveAsMenuItem.addActionListener(new ActionListener() {
  public void actionPerformed(ActionEvent e) {

  // Call the method save ...
  save();
  }
  });



  // Here is your save method . . .
 
  private void save()
  {
  if (fileName==null) {

  JFileChooser y = new JFileChooser();

  y.setDialogTitle("Save . . .");
  y.setFileSelectionMode(JFileChooser.FILES_ONLY);
  y.setCurrentDirectory(new File("C:/"));

  int result = y.showSaveDialog(null);

  if (result == JFileChooser.CANCEL_OPTION)
  return;

  fileName = y.getSelectedFile();

  if (fileName == null || fileName.getName().equals(""))
  {
  JOptionPane.showMessageDialog( this,"Invalid file name . . .",
  "Warning . . .",JOptionPane.WARNING_MESSAGE );
  }
  else
  {
  try
  {

      // Do your thing here . . .  

  }
  pw.close();
  }
  catch (Exception e)
  {
  JOptionPane.showMessageDialog(this,"Error saving file . . .",
  "Warning . . .", JOptionPane.ERROR_MESSAGE);
  }  
  }

Hope that helps . . .
Javatm
  }
0
 
LVL 15

Accepted Solution

by:
Javatm earned 250 total points
ID: 10753872
I made some changes, here you go :

Dont forget to import :
import java.io.*;


  // Here is your Action Listener . . .

  saveAsMenuItem.addActionListener(new ActionListener() {
  public void actionPerformed(ActionEvent e) {

  // Call the method save ...
  save();
  }
  });


  // Here is your save method . . .
 
  private void save()
  {
  if (fileName==null) {

  JFileChooser y = new JFileChooser();

  y.setDialogTitle("Save . . .");
  y.setFileSelectionMode(JFileChooser.FILES_ONLY);
  y.setCurrentDirectory(new File("C:/"));

  int result = y.showSaveDialog(null);

  if (result == JFileChooser.CANCEL_OPTION)
  return;

  File fileName = y.getSelectedFile();

  if (fileName == null || fileName.getName().equals(""))
  {
  JOptionPane.showMessageDialog( this,"Invalid file name . . .",
  "Warning . . .",JOptionPane.WARNING_MESSAGE );
  }
  else
  {
  try
  {

      // Do your thing here . . .  

  }
  pw.close();
  }
  catch (Exception e)
  {
  JOptionPane.showMessageDialog(this,"Error saving file . . .",
  "Warning . . .", JOptionPane.ERROR_MESSAGE);
  }  
  }
  }

Hope that helps . . .
Javatm
0
 

Author Comment

by:Drop_of_Rain
ID: 10754003
I have an accessory named AudioAccessory to add to the file chooser, how would that be done here, would it just be added at:

y.setDialogTitle("Save . . .");
AudioAccessory aa = new AudioAccessory();
y.setAccessory(aa);
y.setFileSelectionMode(JFileChooser.FILES_ONLY);
y.setCurrentDirectory(new File("C:/"));
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 92

Expert Comment

by:objects
ID: 10754024
yes
0
 
LVL 92

Expert Comment

by:objects
ID: 10754035
you would only need to do that initialisation of the chooser once though, not every time you wanted to use it.
0
 
LVL 15

Expert Comment

by:Javatm
ID: 10754051
> I have an accessory named AudioAccessory to add to the file chooser

Yes, thats correct :)
0
 

Author Comment

by:Drop_of_Rain
ID: 10754795
I doubled the points to make it fair on you guys.

Thanks everyone for all the support.
0
 
LVL 92

Expert Comment

by:objects
ID: 10754825
0
 
LVL 15

Expert Comment

by:Javatm
ID: 10754906
http://www.freewebs.com/cube-j

Always there to help . . .
Friend : Javatm
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
The viewer will learn how to implement Singleton Design Pattern in Java.

896 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now