Solved

Will the file description work

Posted on 2004-04-02
3
203 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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
print map entry 34 72
ejb mdb examples 1 6
Is Applet the way to go for my drag and drop system? 8 25
Notify sent to other threads in Java 9 33
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…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.

808 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