3 Replies - 1017 Views - Last Post: 13 July 2012 - 07:34 AM Rate Topic: -----

#1 teamsilvamma  Icon User is offline

  • D.I.C Head

Reputation: -2
  • View blog
  • Posts: 153
  • Joined: 17-January 12

Java Applet showing blank

Posted 10 July 2012 - 08:31 AM

Hey guys,

I was building a simple form in Java using Applet and it was showing fine, then I introduced the setLayout to Flowout and it all went blank. I thought maybe I shouldn't do that, but I set the layout back to how I had it and it still drawing a blank applet. Can you take a quick glance at this code please. Thanks

package applets;

import java.awt.*; 
import java.applet.*;

public class applet3 extends Applet  
{ 
  	private static final long serialVersionUID = 1L;
  	
//creating the fields and storing variables     
	  
  	  Button sendBtn, resetBtn; 
      Label nameLbl, addressLbl, cityLbl, stateLbl, zipLbl;
      Label radiolblTitle, optionTitle, choiceTitle;
      TextField nameTxt, addressTxt, cityTxt, stateTxt, zipTxt; 
      CheckboxGroup radioGroup; 
      Checkbox radio1, radio2; 
      Checkbox option1, option2, option3, option4; 
      Choice choice;
 

     public void form() 
     { 
        //assigning properties to the fields
    	setLayout(new FlowLayout()); 
        sendBtn = new Button("Send"); 
        resetBtn = new Button("Reset");
        
        nameLbl = new Label("Name");
        addressLbl = new Label("Address");
        cityLbl = new Label("City");
        stateLbl = new Label("State");
        zipLbl = new Label("Zip");
        radiolblTitle = new Label("Is this a new case?");
        optionTitle = new Label("Service required. Choose all that apply");
        choiceTitle = new Label("Age");
        
        nameTxt = new TextField("",50); 
        addressTxt = new TextField("",100);
        cityTxt = new TextField("",20);
        stateTxt = new TextField("",2);
        zipTxt = new TextField("",5);
        
        radioGroup = new CheckboxGroup();  
        radio1 = new Checkbox("Yes", radioGroup, false); 
        radio2 = new Checkbox("No", radioGroup, false); 
            
        option1 = new Checkbox("Criminal Law", false);
        option2 = new Checkbox("Civil Law", false);
        option3 = new Checkbox("Business Law", false);
        option4 = new Checkbox("Divorce Law", false);
        
        choice = new Choice();
        choice.addItem("Choice 1");
        choice.addItem("Choice 2");
        choice.addItem("Choice 3");
        

        //placing the fields on the applet
        sendBtn.setBounds(20,580,100,30);
        resetBtn.setBounds(180,580,100,30);
          
        nameLbl.setBounds(10,70,140,30);
        addressLbl.setBounds(10,105,140,30);
        cityLbl.setBounds(10,140,40,30);
        stateLbl.setBounds(200,140,40,30);
        zipLbl.setBounds(290,140,40,30);
        radiolblTitle.setBounds(10,200,120,30);
        optionTitle.setBounds(10,270,250,30);
        choiceTitle.setBounds(10,350,250,30);
          
        nameTxt.setBounds(60,70,350,25); 
        addressTxt.setBounds(60,105,350,25); 
        cityTxt.setBounds(60,140,130,25); 
        stateTxt.setBounds(240,140,40,25);
        zipTxt.setBounds(330,140,80,25); 
          
        radio1.setBounds(40,230,50,30); 
        radio2.setBounds(100,230,50,30); 
        
        option1.setBounds(40,300,100,30);
        option2.setBounds(40,335,100,30);
        option3.setBounds(180,300,100,30);
        option4.setBounds(180,335,100,30);
        
        choice.setBounds(40,370,100,30);
        
        //activating the fields
        add(sendBtn); 
        add(resetBtn);
      
        add(nameTxt);
        add(addressTxt);
        add(cityTxt);
        add(stateTxt);
        add(zipTxt);
      
        add(nameLbl);
        add(addressLbl);
        add(cityLbl);
        add(stateLbl);
        add(zipLbl);
        add(radiolblTitle);
        add(optionTitle);
        add(choiceTitle);
      
        add(radio1); 
        add(radio2); 
        
        add(option1);
        add(option2);
        add(option3);
        add(option4);
        
        add(choice);
     }//end form

}//end class




Is This A Good Question/Topic? 0
  • +

Replies To: Java Applet showing blank

#2 macosxnerd101  Icon User is offline

  • Self-Trained Economist
  • member icon




Reputation: 10447
  • View blog
  • Posts: 38,690
  • Joined: 27-December 08

Re: Java Applet showing blank

Posted 10 July 2012 - 08:44 AM

*Moved to Java*

You need an init() method for an Applet, and you should invoke your form() method there. Or just rename form() to init(). Applets have milestone methods- init(), start(), stop(), destroy(). The init() one is the one you are always interested in.

Also, you should avoid AWT in favor of the more modern Swing Components like JApplet, JButton, etc.
Was This Post Helpful? 1
  • +
  • -

#3 g00se  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 2675
  • View blog
  • Posts: 11,305
  • Joined: 20-September 08

Re: Java Applet showing blank

Posted 10 July 2012 - 02:47 PM

You can't normally use setBounds and expect your choices to be honoured unless you use a null layout. Null layouts should be avoided except for very special cases, and yours isn't special. Learn how to use layout managers

http://docs.oracle.c...yout/index.html
Was This Post Helpful? 1
  • +
  • -

#4 teamsilvamma  Icon User is offline

  • D.I.C Head

Reputation: -2
  • View blog
  • Posts: 153
  • Joined: 17-January 12

Re: Java Applet showing blank

Posted 13 July 2012 - 07:34 AM

Thanks fellas, it worked great!
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1