Solved

Will the file description work

Posted on 2004-04-02
3
191 Views
Last Modified: 2010-03-31


// 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);


}
}
0
Comment
Question by:Drop_of_Rain
  • 2
3 Comments
 
LVL 15

Accepted Solution

by:
Javatm earned 75 total points
ID: 10739061
Its great, good work so what's the problem ?
0
 

Author Comment

by:Drop_of_Rain
ID: 10739108
There isn't one I was just double checking!
0
 
LVL 15

Expert Comment

by:Javatm
ID: 10739142
http://www.freewebs.com/cube-j

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

Suggested Solutions

For customizing the look of your lightweight component and making it look opaque like it was made of plastic.  This tip assumes your component to be of rectangular shape and completely opaque.   (CODE)
Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
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 theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.

707 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

17 Experts available now in Live!

Get 1:1 Help Now