Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: Can I set the contentPane of JF to be JApplet?

  1. #1
    Banned
    Join Date
    May 2010
    Location
    North Central Illinois
    Posts
    1,631
    My Mood
    Sleepy
    Thanks
    390
    Thanked 112 Times in 110 Posts

    Default Can I set the contentPane of JF to be JApplet?

    Could I do something like this:


     
       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.
    Last edited by javapenguin; May 23rd, 2012 at 09:54 PM.


  2. #2
    Super Moderator pbrockway2's Avatar
    Join Date
    Jan 2012
    Posts
    990
    Thanks
    7
    Thanked 207 Times in 183 Posts

    Default 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.

Similar Threads

  1. NEED HELP! Beginner JApplet Problems
    By bam0792 in forum AWT / Java Swing
    Replies: 4
    Last Post: February 20th, 2012, 02:52 PM
  2. Animation in JApplet
    By Aurin in forum What's Wrong With My Code?
    Replies: 3
    Last Post: January 14th, 2012, 10:18 AM
  3. Components I add to JApplet won't appear!
    By austin.rose94 in forum What's Wrong With My Code?
    Replies: 3
    Last Post: January 6th, 2012, 11:47 AM
  4. Sockets in JApplet
    By shrey.haria in forum Java Networking
    Replies: 4
    Last Post: September 5th, 2011, 10:45 AM
  5. JApplet containing JButton and a JLabel
    By JuneM in forum AWT / Java Swing
    Replies: 3
    Last Post: March 26th, 2010, 09:32 AM