?
Solved

Will the file description work

Posted on 2004-04-02
3
Medium Priority
?
219 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
[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
  • 2
3 Comments
 
LVL 15

Accepted Solution

by:
Javatm earned 225 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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

After being asked a question last year, I went into one of my moods where I did some research and code just for the fun and learning of it all.  Subsequently, from this journey, I put together this article on "Range Searching Using Visual Basic.NET …
Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
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…
This video teaches viewers about errors in exception handling.
Suggested Courses
Course of the Month13 days, 11 hours left to enroll

800 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