Programming Forums

Programming Forums (http://www.programmingforums.org/forumindex.php)
-   Java (http://www.programmingforums.org/forum17.html)
-   -   Shouldnt this work? (http://www.programmingforums.org/showthread.php?t=13403)

crawforddavid2006 Jun 21st, 2007 5:50 AM

Shouldnt this work?
 
ok, i'm confused why this doesnt work
:

  1.  
  2. import java.awt.*;
  3. import javax.swing.*;
  4.  
  5. public class GuiExample extends JApplet
  6. {
  7.     Button okButton;
  8.  
  9.     TextField nameField;
  10.  
  11.     CheckboxGroup radioGroup;
  12.  
  13.     Checkbox radio1;
  14.     Checkbox radio2;
  15.  
  16.     Checkbox option;
  17.  
  18.     public void init()
  19.     {
  20.         //setLayout(null);
  21.  
  22.         okButton = new Button("A Button");
  23.  
  24.         nameField = new TextField("A TextField", 100);
  25.  
  26.         radioGroup = new CheckboxGroup();
  27.  
  28.         radio1 = new Checkbox("Radio 1", radioGroup, false);
  29.  
  30.         radio2 = new Checkbox("Radio 2", radioGroup, true);
  31.  
  32.         option = new Checkbox("Option", false);
  33.  
  34.         okButton.setBounds(20,20,100,30);
  35.         nameField.setBounds(20,70,100,40);
  36.         radio1.setBounds(20,120,100,30);
  37.         radio2.setBounds(140,120,100,30);
  38.         option.setBounds(20,170,100,30);
  39.  
  40.         add(okButton);
  41.         add(nameField);
  42.         add(radio1);
  43.         add(radio2);
  44.         add(option);
  45.     }
  46. }


it compiles, but when i run it in blueJ it give me this "applet not initialized"

Tsar_of_Cows Jun 21st, 2007 10:45 AM

It's been a while sicne I extended a class, but I seem to remember you tended to need certain methods or you'd get an error like that.

Are you sure you have all the required methods for it to work? (I've never sued applets so I wouldn't know)

Tsar_of_Cows Jun 21st, 2007 11:10 AM

Ah, I just realised exactly what's wrong!

IIRC, the fist thing you have to do when extending a class is call the super class in the contruction method.

Try this:

:

  1. import java.awt.*;
  2. import javax.swing.*;
  3.  
  4. public class GuiExample extends JApplet
  5. {
  6.     Button okButton;
  7.  
  8.     TextField nameField;
  9.  
  10.     CheckboxGroup radioGroup;
  11.  
  12.     Checkbox radio1;
  13.     Checkbox radio2;
  14.  
  15.     Checkbox option;
  16.  
  17.     //Constructor method
  18.     public GuiExample()
  19.     {
  20.  
  21.     super();
  22.  
  23.     init();
  24.  
  25.   }
  26.  
  27.     public void init()
  28.     {
  29.         //setLayout(null);
  30.  
  31.         okButton = new Button("A Button");
  32.  
  33.         nameField = new TextField("A TextField", 100);
  34.  
  35.         radioGroup = new CheckboxGroup();
  36.  
  37.         radio1 = new Checkbox("Radio 1", radioGroup, false);
  38.  
  39.         radio2 = new Checkbox("Radio 2", radioGroup, true);
  40.  
  41.         option = new Checkbox("Option", false);
  42.  
  43.         okButton.setBounds(20,20,100,30);
  44.         nameField.setBounds(20,70,100,40);
  45.         radio1.setBounds(20,120,100,30);
  46.         radio2.setBounds(140,120,100,30);
  47.         option.setBounds(20,170,100,30);
  48.  
  49.         add(okButton);
  50.         add(nameField);
  51.         add(radio1);
  52.         add(radio2);
  53.         add(option);
  54.     }
  55. }


ReggaetonKing Jun 21st, 2007 4:40 PM

@Tsar_of_Cows: When a GuiExample object is created, the super class, JApplet's constructor will automatically be called right before the GuiExample's constructor. It's all OOP.

crawforddavid2006 Jun 21st, 2007 5:51 PM

Now it says "do not use GuiExample.add() use GuiExample.getContentPane().add() instead." then it gives me the original error

titaniumdecoy Jun 21st, 2007 6:39 PM

Your code (crawforddavid2006) works fine for me (although the window layout leaves much to be desired). There may be some special way to initialize an applet in BlueJ before it can run; however, I suggest you find a real IDE/compiler instead.

Tsar_of_Cows Jun 22nd, 2007 6:44 PM

Quote:

Originally Posted by reggaeton_king (Post 129519)
@Tsar_of_Cows: When a GuiExample object is created, the super class, JApplet's constructor will automatically be called right before the GuiExample's constructor. It's all OOP.

Is that specific to Applets?

titaniumdecoy Jun 22nd, 2007 7:54 PM

No; it's specific to the Java language.

Harakim Jun 23rd, 2007 4:56 AM

It's not even really specific to Java. When you create an object, if you do not explicity call the super constructor, then the superclass' default constructor will be called... sort of. That way, all of the super class' memory space gets initialized at the very least. It wouldn't really make sense if it didn't. If you don't get it, don't worry. I didn't really get it until I wrote an entire program, which you are planning on doing now anyway, right?

Cheers.



The applet works for me. Here is my html code (pretty much taken from the example)

:

<HTML>
<HEAD>
<TITLE> Query Output </TITLE>
</HEAD>
<BODY>

Output from query
select NAME, PRICE from COFFEES
<APPLET CODE="GuiExample.class" WIDTH=250 HEIGHT=200>
</APPLET>
</BODY>
<script language="JavaScript" src="/js/omi/jsc/s_code_remote.js"></script></HTML>


I used JDK 6.1 on Vista Business to test this.


All times are GMT -5. The time now is 2:40 AM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC