Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

A little strange with the syntax of applets  RSS feed

 
Oleg Korsakov
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello )
One thing from the Class JApplet SDK 5.0 that the setLayout method is not static:
public void setLayout(LayoutManager manager)

Why does the calling setLayout (also add) happen without creating object by new operator? setLayout is not static and even if it is static we need to write JApplet.setLayout, is it?
the example below:

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.net.*;

public class WelcomeApplet extends JApplet
{
public void init()
{
* setLayout(new BorderLayout());

JLabel label = new JLabel(getParameter("greeting"), SwingConstants.CENTER);
label.setFont(new Font("Serif", Font.BOLD, 18));
* add(label, BorderLayout.CENTER);

JPanel panel = new JPanel();

JButton oneButton = new JButton("First Button");
oneButton.addActionListener(makeURLActionListener(
"http://www.javaranch.com"));
panel.add(oneButton);

JButton twoButton = new JButton("Second Button");
twoButton.addActionListener(makeURLActionListener(
"mailto:xxx@yyy.com"));
panel.add(twoButton);

* add(panel, BorderLayout.SOUTH);
}

private ActionListener makeURLActionListener(final String u)
{
return new
ActionListener()
{
public void actionPerformed(ActionEvent event)
{
try
{
getAppletContext().showDocument(new URL(u));
}
catch(MalformedURLException e)
{
e.printStackTrace();
}
}
};
}
}
Excuse me for long example - I saw it in a book...
Thanks.
 
Ulf Dittmer
Rancher
Posts: 42972
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Actually, there is an object involved, in all the cases you marked with a "*". It's the implicit object known as "this". So, "setLayout" is really "this.setLayout", and it refers to the JApplet instance itself.

This is a feature of Java, and has nothing to do with applets in particular.
[ February 27, 2007: Message edited by: Ulf Dittmer ]
 
Oleg Korsakov
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, thank you...
One remark only: when the creation of that class instance happened?
( I guessed that only after creation implicit reference this appears )
 
Ulf Dittmer
Rancher
Posts: 42972
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The class is created when the browser is trying to show a page containing an applet (well, that much is obvious). The important thing is that none of the methods in the class will be called until the object is fully initialized, so the reference to "this" is always valid (and optional, as you have found out).
 
Oleg Korsakov
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
My thanks to you. I didn't know that class in applet is created implisitly.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!