Link to home
Start Free TrialLog in
Avatar of Drop_of_Rain
Drop_of_Rain

asked on

Will the file description work



// AudioAccessory.java
// An accessory for JFileChooser that lets you play music clips.  Only the
// simple .au, .aiff and .wav formats available through the Applet sound
// classes can be played.
//
import javax.swing.*;
import java.awt.*;
import java.net.*;
import java.beans.*;
import java.io.*;
import java.applet.*;
import java.awt.event.*;

public class AudioAccessory extends JPanel implements PropertyChangeListener,
ActionListener {

  AudioClip currentClip;
  String currentName="";
  JLabel fileLabel;
  JButton playButton, stopButton;
  JTextArea textArea;
  JLabel fileDescriptionLabel;
  JFileChooser jFileChooser1 = new JFileChooser(System.getProperty("user.dir"));
 
  public AudioAccessory() {
    // Set up the accessory.  The file chooser will give us a reasonable size.
fileLabel = new JLabel("Audio Clip");
playButton = new JButton("Play");
stopButton = new JButton("Stop");
fileDescriptionLabel = new JLabel("File Description");
textArea = new JTextArea(
"");
JScrollPane jspane = new JScrollPane(textArea);

playButton.setEnabled(false);
stopButton.setEnabled(false);
textArea.setFont(new Font("Serif", Font.PLAIN, 12));
textArea.setLineWrap(true);
textArea.setWrapStyleWord(true);
jspane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
jspane.setPreferredSize(new Dimension(150, 60));
jspane.getViewport().add(textArea);

JPanel p0 = new JPanel(new BorderLayout());
JPanel p1 = new JPanel(new BorderLayout());
JPanel p2 = new JPanel(new BorderLayout());
JPanel p3 = new JPanel(new BorderLayout());
JPanel p4 = new JPanel(new BorderLayout());

p0.add(fileLabel, BorderLayout.CENTER);
p1.add(playButton, BorderLayout.CENTER);
p2.add(stopButton, BorderLayout.CENTER);
p3.add(fileDescriptionLabel, BorderLayout.CENTER);
p4.add(jspane, BorderLayout.CENTER);

p4.add(p3, BorderLayout.NORTH);
p3.add(p2, BorderLayout.NORTH);
p2.add(p1, BorderLayout.NORTH);
p1.add(p0, BorderLayout.NORTH);

setLayout(new FlowLayout());
add(p4);

    playButton.addActionListener(new ActionListener() {
      public void actionPerformed(ActionEvent e) {
        if (currentClip != null) {
          currentClip.stop();
          currentClip.play();
        }
      }
    });
    stopButton.addActionListener(new ActionListener() {
      public void actionPerformed(ActionEvent e) {
        if (currentClip != null) {
          currentClip.stop();
        }
      }
    });
  }

  public void propertyChange(PropertyChangeEvent e) {
    String pname = e.getPropertyName();
    if (JFileChooser.SELECTED_FILE_CHANGED_PROPERTY.equals(pname)) {
      // Ok, the user selected a file in the chooser
      File f = (File)e.getNewValue();

          // Make reasonably sure it's an audio file
      if ((f != null) && 
          (f.getName().toLowerCase().endsWith(".au") ||
           f.getName().toLowerCase().endsWith(".wav") ||
           f.getName().toLowerCase().endsWith(".aif") ||
           f.getName().toLowerCase().endsWith(".aiff"))
         ) {
        setCurrentClip(f);
      }
      else {
        setCurrentClip(null);
      }
    }
  }


  public void setCurrentClip(File f) {
    if (currentClip != null) { currentClip.stop(); }
    // Make sure we have a real file, otherwise, disable the buttons
    if ((f == null) || (f.getName() == null)) {
      fileLabel.setText("no audio selected");
      playButton.setEnabled(false);
      stopButton.setEnabled(false);
      return;
    }

    // Ok, seems the audio file is real, so load it and enable the buttons
    String name = f.getName();
    if (name.equals(currentName)) {
      // Same clip they just loaded...make sure the player is enabled
      fileLabel.setText(name);
      playButton.setEnabled(true);
      stopButton.setEnabled(true);
      return;
    }
    currentName = name;
    try {
      URL u = new URL("file:///" + f.getAbsolutePath());
      currentClip = Applet.newAudioClip(u);
    }
    catch (Exception e) {
      e.printStackTrace();
      currentClip = null;
      fileLabel.setText("Error loading clip.");
    }
    fileLabel.setText(name);
    playButton.setEnabled(true);
    stopButton.setEnabled(true);
  }

  public void actionPerformed(ActionEvent ae) {
    // Be a little cavalier here...we're assuming the dialog was just
    // approved or cancelled so we should stop any playing clip
    if (currentClip != null) { currentClip.stop(); }
  }
void saveasfile() {

String currFileName;

jFileChooser1.setVisible(true);

jFileChooser1.setEnabled(true);

if (JFileChooser.APPROVE_OPTION == jFileChooser1.showSaveDialog(this)) {

currFileName = jFileChooser1.getSelectedFile().getPath();

if (currFileName.endsWith(".doc") == false && 

currFileName.endsWith(".rtf") == false) {

currFileName = currFileName + ".doc";

}

try { File file = new File(currFileName);

FileWriter out = new FileWriter(file);

String text = textArea.getText(); // GET YOUR TEXT FROM TEXT AREA..

out.write(text);

out.close();

JOptionPane.showMessageDialog(null, "The report has been saved to the file..",

"Saved", 1);

}

catch (IOException e) {

//System.out.println("Error saving " + currFileName);

}

}

jFileChooser1.setVisible(false);

jFileChooser1.setEnabled(false);


}
}
ASKER CERTIFIED SOLUTION
Avatar of Javatm
Javatm
Flag of Singapore image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Drop_of_Rain
Drop_of_Rain

ASKER

There isn't one I was just double checking!
http://www.freewebs.com/cube-j

Always glad to help . . .
Friend : Javatm