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.getPro
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(fals
stopButton.setEnabled(fals
textArea.setFont(new Font("Serif", Font.PLAIN, 12));
textArea.setLineWrap(true)
textArea.setWrapStyleWord(
jspane.setVerticalScrollBa
jspane.setPreferredSize(ne
jspane.getViewport().add(t
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(fileDescriptionLabe
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.addActionListen
public void actionPerformed(ActionEven
if (currentClip != null) {
currentClip.stop();
currentClip.play();
}
}
});
stopButton.addActionListen
public void actionPerformed(ActionEven
if (currentClip != null) {
currentClip.stop();
}
}
});
}
public void propertyChange(PropertyCha
String pname = e.getPropertyName();
if (JFileChooser.SELECTED_FIL
// 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()
f.getName().toLowerCase().
f.getName().toLowerCase().
f.getName().toLowerCase().
) {
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(fals
stopButton.setEnabled(fals
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(ActionEven
// 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(t
jFileChooser1.setEnabled(t
if (JFileChooser.APPROVE_OPTI
currFileName = jFileChooser1.getSelectedF
if (currFileName.endsWith(".d
currFileName.endsWith(".rt
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.showMessageDia
"Saved", 1);
}
catch (IOException e) {
//System.out.println("Erro
}
}
jFileChooser1.setVisible(f
jFileChooser1.setEnabled(f
}
}
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER