Can't display components on applet


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 2 of 2

Thread: Can't display components on applet

  1. #1
    Cody Tang Guest

    Can't display components on applet


    Hi,

    Please help me. When I run the following applet it doesn't show any of the
    components on the first time. However, if I resize the appletviewer window
    then everything show up. Where did I do wrong?

    cody

    *****************************************

    import java.awt.*;
    import java.applet.*;
    import java.awt.event.*;
    import java.util.*;

    class LoginThread extends Thread implements ActionListener {
    private LoginPanel pnlMain;
    private String s;
    private Applet ap;

    public LoginThread(Applet applet, String name) {
    s = name;
    ap = applet;
    }

    public void run() {
    ap.add(s, pnlMain = new LoginPanel(this));
    }

    public void actionPerformed(ActionEvent e) {
    }

    }

    class LoginPanel extends Panel {
    private Label lblWelcome = new Label("Welcome Message");
    private Label lblLogin = new Label("Login: ");
    private Label lblPasswd = new Label("Password: ");
    private TextField txtLogin = new TextField(10);
    private TextField txtPasswd = new TextField(10);
    private Button btnOK = new Button("OK");
    private Button btnReset = new Button("Reset");
    private Panel pnlLogin, pnlRow1, pnlRow2, pnlRow3;
    private ActionListener al;

    // Constructor
    LoginPanel(ActionListener listener) {
    al = listener;
    setLayout(new BorderLayout());
    pnlRow1 = makePanel(new FlowLayout(FlowLayout.CENTER, 4, 2));
    pnlRow1.add(lblWelcome);
    pnlRow2 = makePanel(new FlowLayout(FlowLayout.LEFT, 4, 2));
    pnlRow2.add(lblLogin);
    pnlRow2.add(txtLogin);
    pnlRow2.add(lblPasswd);
    pnlRow2.add(txtPasswd);
    txtPasswd.setEchoChar('*');
    pnlRow3 = makePanel(new FlowLayout(FlowLayout.CENTER, 4, 2));
    pnlRow3.add(btnOK);
    pnlRow3.add(btnReset);
    pnlLogin = makePanel(new BorderLayout(2, 2));
    pnlLogin.add("North", pnlRow1);
    pnlLogin.add("Center", pnlRow2);
    pnlLogin.add("South", pnlRow3);

    btnOK.addActionListener(al);
    btnReset.addActionListener(al);

    add(pnlLogin);
    }

    // returns a Panel object and sets the LayoutManager
    private Panel makePanel(LayoutManager lm) {
    Panel p = new Panel();
    p.setLayout(lm);
    return p;
    }
    }

    public class Test extends Applet {
    public LoginThread mainThread1 = new LoginThread(this, "North");
    public LoginThread mainThread2 = new LoginThread(this, "South");

    public void init() {
    setLayout(new BorderLayout());
    mainThread1.start();
    mainThread2.start();
    }

    }

  2. #2
    Ruchi Dhar Guest

    Re: Can't display components on applet


    Since you are constructing the GUI components in Thread by the time browser(appletviewer)
    invokes init() there is no GUI to display. If you see your applet flow in
    init() method you are spwaning threads which won't display GUI immediatly
    and init method will be done immediatly before your GUI being constructed
    so there is no display after init no sizing of the components etc. Try to
    keep your GUI construction in main thread of applet.

    "Cody Tang" <cody_tang@tom.com> wrote:
    >
    >Hi,
    >
    >Please help me. When I run the following applet it doesn't show any of the
    >components on the first time. However, if I resize the appletviewer window
    >then everything show up. Where did I do wrong?
    >
    >cody
    >
    >*****************************************
    >
    >import java.awt.*;
    >import java.applet.*;
    >import java.awt.event.*;
    >import java.util.*;
    >
    >class LoginThread extends Thread implements ActionListener {
    > private LoginPanel pnlMain;
    > private String s;
    > private Applet ap;
    >
    > public LoginThread(Applet applet, String name) {
    > s = name;
    > ap = applet;
    > }
    >
    > public void run() {
    > ap.add(s, pnlMain = new LoginPanel(this));
    > }
    >
    > public void actionPerformed(ActionEvent e) {
    > }
    >
    >}
    >
    >class LoginPanel extends Panel {
    > private Label lblWelcome = new Label("Welcome Message");
    > private Label lblLogin = new Label("Login: ");
    > private Label lblPasswd = new Label("Password: ");
    > private TextField txtLogin = new TextField(10);
    > private TextField txtPasswd = new TextField(10);
    > private Button btnOK = new Button("OK");
    > private Button btnReset = new Button("Reset");
    > private Panel pnlLogin, pnlRow1, pnlRow2, pnlRow3;
    > private ActionListener al;
    >
    > // Constructor
    > LoginPanel(ActionListener listener) {
    > al = listener;
    > setLayout(new BorderLayout());
    > pnlRow1 = makePanel(new FlowLayout(FlowLayout.CENTER, 4, 2));
    > pnlRow1.add(lblWelcome);
    > pnlRow2 = makePanel(new FlowLayout(FlowLayout.LEFT, 4, 2));
    > pnlRow2.add(lblLogin);
    > pnlRow2.add(txtLogin);
    > pnlRow2.add(lblPasswd);
    > pnlRow2.add(txtPasswd);
    > txtPasswd.setEchoChar('*');
    > pnlRow3 = makePanel(new FlowLayout(FlowLayout.CENTER, 4, 2));
    > pnlRow3.add(btnOK);
    > pnlRow3.add(btnReset);
    > pnlLogin = makePanel(new BorderLayout(2, 2));
    > pnlLogin.add("North", pnlRow1);
    > pnlLogin.add("Center", pnlRow2);
    > pnlLogin.add("South", pnlRow3);
    >
    > btnOK.addActionListener(al);
    > btnReset.addActionListener(al);
    >
    > add(pnlLogin);
    > }
    >
    > // returns a Panel object and sets the LayoutManager
    > private Panel makePanel(LayoutManager lm) {
    > Panel p = new Panel();
    > p.setLayout(lm);
    > return p;
    > }
    >}
    >
    >public class Test extends Applet {
    > public LoginThread mainThread1 = new LoginThread(this, "North");
    > public LoginThread mainThread2 = new LoginThread(this, "South");
    >
    > public void init() {
    > setLayout(new BorderLayout());
    > mainThread1.start();
    > mainThread2.start();
    > }
    >
    >}



Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center