• 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

init() not entered first

 
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
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();
}
}
}
 
Ranch Hand
Posts: 96
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
McPatrick,
I spent some time trying to figure out your MyInsetsButton problem. You are correct that the reason you cannot initialize theInsets inside of init() is that init() calls getInsets() before theInsets is initialized. Your statement
<blockquote>"More amazingly the getInsets method is even looked at twice before entering the init() method."</blockquote>
is incorrect but I understand where you are coming from. We learn that init() always executes first, and it does. Right when init() begins in your program it calls getInsets() to setup the display(for some unknown reason, it calls it twice), then it runs the statement System.out.println("inside init()"); This is why you are thinking that init() is executing after getInsets() but really it is not, only your print statement is.

I hope this helps you, it sure helped me =)

Noah Carroll
 
Anonymous
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Noah,
thanks for replying.
You are saying that init() is entered first after all. I'd like to believe this but I don't see any proof for it. The first line of code inside init() is the print statement. You're saying that method GetInsets is called first from within init(). How does this happen then? Not because of any code then? Can you explain this any further?
Appreciate it!
Patrick
 
Sheriff
Posts: 9099
12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Maybe (?) because java.awt.Insets already has a getInsets() method. It seems to initialize fine for me defining theInsets inside init, but class Switcher doesn't seem to work right.
<pre>
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()");
Insets theInsets = new Insets(1,1,1,1);
System.out.println("after Insets");
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)
{
Insets theInsets=null;
System.out.println("inside Switcher");
theInsets = new Insets(theInsets.top + 2, theInsets.left + 2,
theInsets.bottom + 2, theInsets.right + 2);
invalidate();
validate();
}
}
*/
}
</pre>
 
F is for finger. Can you stick your finger in your nose? Doesn't that feel nice? Now try this tiny ad:
Thread Boost feature
https://coderanch.com/t/674455/Thread-Boost-feature
    Bookmark Topic Watch Topic
  • New Topic