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 4 of 4

Thread: Why doesn't my Applet show up in my browser?

  1. #1
    Junior Member
    Join Date
    Jan 2012
    Posts
    4
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Question Why doesn't my Applet show up in my browser?

    Hi, I'm new-ish to java so please excuse any mistakes I make. I've been making a dice game That when you press space, the dice "rolls" and a random number is generated. I've been trying to add the class to an html as an applet. I created a Application, and then attempted to turn it into a applet. Why doesn't the applet show up in my browser?
    Application Code:
    import java.awt.FlowLayout;
    import java.awt.BorderLayout;
    import java.awt.Container;
    import java.awt.Dimension;
    import javax.swing.*;
    import java.awt.event.*;
     
    public class DiceGame extends JFrame
           implements KeyListener,
           ActionListener
    {
    	private JLabel label1;
    	private JLabel label2;
    	private JLabel label3;
        JTextArea displayArea;
        JTextField typingArea;
        static final String newline = System.getProperty("line.separator");
     
        public static void main( String args[] )
    	{
            try {
                 UIManager.setLookAndFeel("javax.swing.plaf.metal.MetalLookAndFeel");
            } catch (UnsupportedLookAndFeelException ex) {
                ex.printStackTrace();
            } catch (IllegalAccessException ex) {
                ex.printStackTrace();
            } catch (InstantiationException ex) {
                ex.printStackTrace();
            } catch (ClassNotFoundException ex) {
                ex.printStackTrace();
            }
            UIManager.put("swing.boldMetal", Boolean.FALSE);
            javax.swing.SwingUtilities.invokeLater(new Runnable() {
                public void run() {
                    createAndShowGUI();
                }
            });
    	}
     
        public static void createAndShowGUI() {
        DiceGame frame = new DiceGame("Dice Game");
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            frame.addComponentsToPane();
            frame.pack();
            frame.setVisible(true);
    	}
     
    	    private void addComponentsToPane() {
     
    	        JButton button = new JButton("Clear");
    	        button.addActionListener(this);
    	        displayArea = new JTextArea();
    	        displayArea.setEditable(true);
    	        displayArea.addKeyListener(this);
    	        JScrollPane scrollPane = new JScrollPane(displayArea);
    	        scrollPane.setPreferredSize(new Dimension(375, 125));
    	        getContentPane().add(scrollPane, BorderLayout.CENTER);
    	        getContentPane().add(button, BorderLayout.PAGE_END);
        }
     
    	public DiceGame(String name)
    	{
            super( name );
    		setLayout( new FlowLayout() );
    		Icon DiceGame = new ImageIcon( getClass().getResource( "dice.gif" ) );
    		label2 = new JLabel();
    		label2.setText( "" );
    		label2.setIcon( DiceGame );
    		label2.setToolTipText( "DiceGame" );
    		label2.setHorizontalTextPosition( SwingConstants.LEFT );
    		label2.setVerticalTextPosition( SwingConstants.BOTTOM );
    		add( label2 );
    	}
    	    public void keyTyped(KeyEvent e) {}
    	    public void keyPressed(KeyEvent e) {
    		    if(e.getKeyChar() == e.VK_SPACE) {
    		       int d = (int)(Math.random() * 10) % 6 + 1;
    		       displayInfo(d);
    	        }
    		    displayArea.requestFocusInWindow();
    	    }
    	    public void keyReleased(KeyEvent e) {}
     
    	    public void actionPerformed(ActionEvent e) {
    		     displayArea.setText("");
    		     displayArea.requestFocusInWindow();
            }
     
            private void displayInfo(int g){
    			displayArea.setText("");
    		    String keyString = Integer.toString(g);
                displayArea.append(keyString + newline);
                displayArea.setCaretPosition(displayArea.getDocument().getLength());
        }
    }
    Applet Code:
    import java.awt.FlowLayout;
    import java.awt.BorderLayout;
    import java.awt.Container;
    import java.awt.Dimension;
    import javax.swing.*;
    import java.awt.event.*;
    import java.applet.*;
     
    public class DiceGameApplet extends JApplet
           implements KeyListener,
           ActionListener
    {
    	private JLabel label2 = new JLabel();
        JTextArea displayArea = new JTextArea();
        int width, height;
        JButton button = new JButton("Clear");
        JScrollPane scrollPane = new JScrollPane(displayArea);
        Icon DiceGame = new ImageIcon( getClass().getResource( "dice.gif" ) );
     
        static final String newline = System.getProperty("line.separator");
     
    	    public void init() {
            width = getSize().width;
            height = getSize().height;
    	    button.addActionListener(this);
    	    displayArea.setEditable(true);
    	    displayArea.addKeyListener(this);
    	    scrollPane.setPreferredSize(new Dimension(375, 125));
    	    getContentPane().add(scrollPane, BorderLayout.CENTER);
    	    getContentPane().add(button, BorderLayout.PAGE_END);
    		setLayout( new FlowLayout() );
    		label2.setText( "" );
    		label2.setIcon( DiceGame );
    		label2.setToolTipText( "DiceGame" );
    		label2.setHorizontalTextPosition( SwingConstants.LEFT );
    		label2.setVerticalTextPosition( SwingConstants.BOTTOM );
    		add( label2 );
        }
     
    	    public void keyTyped(KeyEvent e) {}
    	    public void keyPressed(KeyEvent e) {
    		    if(e.getKeyChar() == e.VK_SPACE) {
    		       int d = (int)(Math.random() * 10) % 6 + 1;
    		       displayInfo(d);
    	        }
    		    displayArea.requestFocusInWindow();
    	    }
    	    public void keyReleased(KeyEvent e) {}
     
    	    public void actionPerformed(ActionEvent e) {
    		     displayArea.setText("");
    		     displayArea.requestFocusInWindow();
            }
     
            private void displayInfo(int g){
    			displayArea.setText("");
    		    String keyString = Integer.toString(g);
                displayArea.append(keyString + newline);
                displayArea.setCaretPosition(displayArea.getDocument().getLength());
        }
    }


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Why doesn't my Applet show up in my browser?

    Why doesn't the applet show up in my browser?
    Can you show the HTML you are using to load the applet into the browser?
    Also look at the browser's java console to see if there are any error messages.

  3. The Following User Says Thank You to Norm For This Useful Post:

    SendBorg (January 29th, 2012)

  4. #3
    Junior Member
    Join Date
    Jan 2012
    Posts
    4
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Why doesn't my Applet show up in my browser?

    Quote Originally Posted by Norm View Post
    Can you show the HTML you are using to load the applet into the browser?
    Also look at the browser's java console to see if there are any error messages.
    HTML Code:
    <HTML>
    <HEAD>
    <TITLE>Dice Game</TITLE>
    </HEAD>
    <BODY>
    <CENTER>
    <APPLET CODE="DiceGameApplet.class" WIDTH=300 HEIGHT=300></APPLET>
    </BODY>
    </HTML>
    Where can I find the java console? (I'm on firefox.)
    EDIT: nvm, The Size was to small for it to be shown >.<. Thanks anyway!
    Last edited by SendBorg; January 29th, 2012 at 08:29 AM.

  5. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Why doesn't my Applet show up in my browser?

    Glad you solved it.

Similar Threads

  1. How to run Applet with Ms Sql connectivity in browser
    By mayanksmart4 in forum Java Applets
    Replies: 1
    Last Post: November 22nd, 2011, 09:57 AM
  2. HELP! My applet won't run on a web browser....
    By coolidge in forum Java Applets
    Replies: 4
    Last Post: October 21st, 2011, 08:52 AM
  3. I am trying to show database contents in the browser through servlets
    By abhiM in forum What's Wrong With My Code?
    Replies: 2
    Last Post: August 12th, 2011, 02:08 AM
  4. Applet doesnt show
    By chonch in forum What's Wrong With My Code?
    Replies: 5
    Last Post: March 24th, 2011, 03:22 PM
  5. Replies: 10
    Last Post: January 12th, 2011, 05:48 AM