Solved

Help with saveAsMenuItem.addActionListener

Posted on 2004-04-04
12
287 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
What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

 
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

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

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…
Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
Viewers learn about the “while” loop and how to utilize it correctly in Java. Additionally, viewers begin exploring how to include conditional statements within a while loop and avoid an endless loop. Define While Loop: Basic Example: Explanatio…
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

743 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