Can I set the contentPane of JF to be JApplet?
Could I do something like this:
Code java:
public class SneakyShortCut extends javax.swing.JFrame
{
private javax.swing.JApplet ja;
private javax.swing.JButton b1, b2;
public SneakyShortCut()
{
super("JFrame with a JApplet as content pane");
ja = new javax.swing.JApplet();
b1 = new javax.swing.JButton("Button 1");
b1.addActionListener(
new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent e)
{
java.net.URI uri = null;
try
{
uri = new java.net.URI("C:/Users/Wenguin/My Documents/Sleep Away.mp3");
}
catch(java.net.URISyntaxException urise)
{
javax.swing.JOptionPane.showMessageDialog(null, "Error!", "Could not find file!", javax.swing.JOptionPane.ERROR_MESSAGE);
}
java.net.URL temp = null;
try
{
temp = uri.toURL();
}
catch(java.net.MalformedURLException murle)
{
javax.swing.JOptionPane.showMessageDialog(null, "Error!", "Could not find file!", javax.swing.JOptionPane.ERROR_MESSAGE);
}
ja.play(temp);
}});
b2 = new javax.swing.JButton("Button 2");
ja.setContentPane(new javax.swing.JPanel());
ja.getContentPane().setLayout(new java.awt.GridLayout(2,1));
ja.getContentPane().add(b1);
ja.getContentPane().add(b2);
setContentPane(ja);
setVisible(true);
setDefaultCloseOperation(EXIT_ON_CLOSE);
}
public static void main(String[] args)
{
new SneakyShortCut();
}
}
If I can do this, without major complications, then I can use the JApplet to work as a media player.
It appears I can do it, though when I try the URI.toURL() it won't accept it and later throws a NullPointerException as the variable uri throws the MalformedURLException.
Re: Can I set the contentPane of JF to be JApplet?
Applets (J or otherwise) require some sort of context in which to run. This is typically supplied by the JRE associated with the browser within whose page they run. Or by the code within the appletviewer application. I can't see any good coming from the hopeful invokation of the JApplet constructor without that context...
If all you are trying to do is play a sound clip, notice that Applet has a useful static method - newAudioClip() - which, because it's static doesn't require you to construct any applet instance. It returns an AudioClip instance which, in turn provides methods to play the clip.
-----
It's not clear what problems you are having with the url. Is it throwing URISyntaxException? or somethng else? Perhaps the URI you are using should specify the file: protocol.
It could be that the MalformedURLEception occurs simply because the uri is null in which case it is the first exception that has to be addressed.