Solved

Help with saveAsMenuItem.addActionListener

Posted on 2004-04-04
12
290 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
Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

 
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
 
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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
How to parse custom JSON to POJO java 4 83
maven module vs maven project 3 41
ejb on wildfly 5 30
String array comparison 4 38
Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
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 about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:

765 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