0
//create and display a label containing icon and a string
//JLabel and ImageIcon

import java.awt.*;
import java.applet.*;
import javax.swing.*;
import javax.swing.JLabel.*;  

public class JLabelDemo extends JApplet {

    public void init() {
        try {
            SwingUtilities.invoke(
                new Runnable() {
                    public void run() {
                        makeGUI();
                    }
                }
            );
        }
        catch(Exception e) {
            System.out.println("Cannot happen due to exception " + e);
        }
    }

    private void makeGUI() {
        //create an icon
        ImageIcon ii=new ImageIcon("The Big Trip.png");
        //create a label
        JLabel jl=new JLabel("India",ii,JLabel.CENTER);
        add(jl);//add label to content pane
    }

}
/*<applet code="JLabelDemo" height=250 width=150>*/

This code is compiled using:

javac JLabelDemo.java 

But running through cmd using the following is not working (is not displaying any applet)!!

appletviewer JLabelDemo.java 
frasertweedale
  • 5,424
  • 3
  • 26
  • 38
Surya
  • 1
  • 2
  • 2
    1) your code doesn't compile. 2) I'm sure you don't write your code left justified so don't expect us to read it that way either. Post properly formatted code that compiles. – camickr Apr 22 '16 at 20:33
  • 1
    Did you mean to call SwingUtilities.invokeAndWait? There is no invoke method. – ManoDestra Apr 22 '16 at 20:35
  • 1
    FYI: [Java Plugin support deprecated](http://www.gizmodo.com.au/2016/01/rest-in-hell-java-plug-in/) and [Moving to a Plugin-Free Web](https://blogs.oracle.com/java-platform-group/entry/moving_to_a_plugin_free) – MadProgrammer Apr 22 '16 at 21:08
  • `ImageIcon ii=new ImageIcon("The Big Trip.png");` Note, an applet within a sandbox cannot load an image by `File` (which is what is happening in that code statement). An applet must access resources by URL, and if it is sand-boxed, the URL must be from the same server as the applet. And to hark back to the advice of @MadProgrammer: Why code an applet? If it is due to the teacher specifying it, please refer them to [Why CS teachers should **stop** teaching Java applets](http://programmers.blogoverflow.com/2013/05/why-cs-teachers-should-stop-teaching-java-applets/). – Andrew Thompson Apr 23 '16 at 01:37
  • As an aside, change `System.out.println("Cannot happen due to exception "+e);` to `e.printStackTrace(); // shorter & more informative!` and be sure the [Java Console](http://www.java.com/en/download/help/javaconsole.xml) is configured to show. If there is no output at the default level, raise the level and try it again. – Andrew Thompson Apr 23 '16 at 01:39
  • For that exact code, what is the output from `javac JLabelDemo.java`? Please copy/paste the information from the command line. Do you know how to do that? – Andrew Thompson Apr 23 '16 at 01:48
  • improve code formatting – frasertweedale Apr 25 '16 at 02:03

2 Answers2

0

Use SwingUtilities.invokeAndWait(runnable obj) instead of SwingUtilities.invoke(runnable obj)

Rahul Patel
  • 307
  • 1
  • 9
  • 27
0

You need to call SwingUtilities.invokeAndWait instead. And you also need to close your applet tag, for it to work in appletviewer. And you need to add your content to the content pane, rather than using add, as that merely adds to the container.

JLabelDemo.java

//create and display a label containing icon and a string
//JLabel and ImageIcon

import java.awt.*;
import java.applet.*;
import javax.swing.*;

public class JLabelDemo extends JApplet {
    public void init() {
        try {
            SwingUtilities.invokeAndWait(
                new Runnable() {
                    public void run() {
                         this.makeGUI();
                    }
                }
            );
        } catch (Exception e) {
            System.out.println("Cannot happen due to exception "+e);
        }
    }

    private void makeGUI(){
        //create an icon
        ImageIcon ii = new ImageIcon("The Big Trip.png");

        //create a label
        JLabel jl = new JLabel("India", ii, JLabel.CENTER);
        //add label to content pane
        this.getContentPane().add(jl);
    }
}

HTML:

<applet code="JLabelDemo" width="150" height="250"></applet>
ManoDestra
  • 6,325
  • 6
  • 26
  • 50