Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Help with saveAsMenuItem.addActionListener

Posted on 2004-04-04
12
Medium Priority
?
299 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Industry Leaders: 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

What’s Wrong with Your Cloud Strategy ?

Even as many CIOs are embracing a cloud-first strategy, the reality is that moving to the cloud is a lengthy process and the end-state is likely to be a blend of multiple clouds—public and private. Learn why multicloud solutions matter in this webinar by Nimble Storage.

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…
Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
Suggested Courses

650 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