can not handle swing button event when run form applet

This is my applet

public class TestApplet extends Applet{
 public void init(){

 }

 public void start(){
  Swsmall b = new Swsmall();


 }
}

This is my Swsmall file

public Swsmall() {
  JFrame frame = new JFrame ("Stand alone");
  JButton jl = new JButton("Exits properly");

  frame.getContentPane().add(jl);
  frame.setSize(180,80);
  frame.setVisible(true);



        jl.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
             System.exit(0);}});
            }

this my jsp file

<body>
    <applet code="TestApplet.class" width="400" height="400"></applet>
  </body>

I am able to run applet successfully but I can't get any responce on button click event

When I run same application on java console it works perfect

Answers


frame.dispose();

Code for creating the JFrame should be placed in the init() method. You should also be using the invokeAndWait() method.

Read the section from the Swing tuorial on How to Make Applets for more information and a working example.


Calling System.exit(0) from Java applet will not destroy an applet. Try calling something else from action listener (i.e. System.out.println("something"); would print in Java applet console), and you'll see that it's called correctly, but in this case it probably doesn't work as you expected it to work.


Need Your Help

Java beginner regarding repetition

java

I am a complete beginner and I'm trying to follow the logic behind this code snippet.

EF4.1 Code First: Stored Procedure with output parameter

entity-framework stored-procedures parameters ef-code-first

I use Entity Framework 4.1 Code First. I want to call a stored procedure that has an output parameter and retrieve the value of that output parameter in addition to the strongly typed result set. I...

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.