This week's book giveaway is in the JavaScript forum.
We're giving away four copies of Svelte and Sapper in Action and have Mark Volkmann on-line!
See this thread for details.
Win a copy of Svelte and Sapper in Action this week in the JavaScript forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Bear Bibeault
  • Junilu Lacar
Sheriffs:
  • Jeanne Boyarsky
  • Tim Cooke
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • salvin francis
  • Frits Walraven
Bartenders:
  • Scott Selikoff
  • Piet Souris
  • Carey Brown

applet init() not entered first..help!!

 
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
I have posted this same question on the beginner as well as the imtermediate but haven't received any replies so I guess this is an advanced topic.Hope someone here can help me on this one !
Please look at the following code. It looks like a fairly simple applet but has some behavior that I find strange. As far as i know you should normally be able to make use of an initializer OR initialize a declared field in the init() method. The thing with the applet below though is that when I initialize Insets theInsets within the init() method, the applet won't run. It will when I make use of an initializer. The reason it doesn't run in the first case is because the getInsets method is looked at first and then it can't return theInsets since the value is still null (un-initialized). You can see this happen when you run it and look at the output,since I put some System.out.println lines inthere. More amazingly the getInsets method is even looked at twice before entering the init() method.
I wonder if there is someone that can explain this behavior...It sure is much appreciated !
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
public class MyInsetsButton extends JApplet
{
JButton theButton;
Container theContainer;
//Insets theInsets ;
Insets theInsets = new Insets(1,1,1,1);
public void init()
{
System.out.println("inside init()");
//theInsets = new Insets(1,1,1,1);
theButton = new JButton("Press Me");
theButton.addActionListener(new Switcher());
theContainer = this.getContentPane();
theContainer.add(theButton);
}
public Insets getInsets()
{
System.out.println("inside getInsets");
return theInsets;
}
public class Switcher implements ActionListener
{
public void actionPerformed(ActionEvent ae)
{
System.out.println("inside Switcher");
theInsets = new Insets(theInsets.top + 2, theInsets.left + 2,
theInsets.bottom + 2, theInsets.right + 2);
invalidate();
validate();
}
}
}
Thanx in advance !
McPatrick
 
Ranch Hand
Posts: 96
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
McPatrick,
I answered your question in the beginner forum. I hope it helps you out.
Noah
 
So I left, I came home, and I ate some pie. And then I read this tiny ad:
Thread Boost feature
https://coderanch.com/t/674455/Thread-Boost-feature
    Bookmark Topic Watch Topic
  • New Topic