?
Solved

Help with saveAsMenuItem.addActionListener

Posted on 2004-04-04
12
Medium Priority
?
302 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 600 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 400 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 600 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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 15

Assisted Solution

by:Javatm
Javatm earned 1000 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 1000 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
 
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

Vote for the Most Valuable Expert

It’s time to recognize experts that go above and beyond with helpful solutions and engagement on site. Choose from the top experts in the Hall of Fame or on the right rail of your favorite topic page. Look for the blue “Nominate” button on their profile to vote.

Question has a verified solution.

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

For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…
Suggested Courses
Course of the Month14 days, 16 hours left to enroll

839 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