dcsimg


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 3 of 3

Thread: java applet problem...a beginner problem :D

  1. #1
    Join Date
    Aug 2005
    Posts
    6

    Exclamation java applet problem...a beginner problem :D

    Code:
    import java.awt.*;
    import java.applet.*;
    import java.awt.event.*;
    import javax.swing.*;
    import java.util.*;
    
    public class Main extends Applet implements ActionListener
    { 
    Panel p = new Panel();
    JButton admin = new JButton ("Admin");
    JButton user = new JButton ("User");
    JPanel TextField = new JPanel();
    JTextField username = new JTextField(10);
    
    public void init()
    { 
     setSize(400,400);
     MainMenu();
     UserPass();
       
    
    }
    
    public void MainMenu()
    {
    
     setLayout(new BorderLayout());
    
     p.add(admin);
     p.add(user);
     add(p, BorderLayout.SOUTH);
     
     admin.addActionListener(this);
     user.addActionListener(this);
    
    }
    
    public void UserPass()
    {
    
     TextField.setLayout(new BorderLayout(5,0));
     TextField.add(new JLabel("Username : "), BorderLayout.WEST);
     TextField.add(username, BorderLayout.CENTER);
                                   add(TextField);
    
    }
    
    public void actionPerformed(ActionEvent e)
    {
    
     if (e.getSource() == admin)
     {
         remove(p);
     }
     
     if (e.getSource() == user)
     {
         remove(p);
     }
    
    }
    }
    can somebody help me..as you can see, if the e.getSource() == admin, it will remove the object p from class Panel...now after it remove the object p, i want the applet to invoke the method UserPass()...the problem is when i put something like this :-

    Code:
    if (e.getSource() == admin)
     {
         remove(p);
         UserPass();
     }
    it would not invoke the UserPass() but when i put the UserPass in public void init(), it easily invoked the UserPass method but the problem is it would produce the output the button named Admin and User as well as the Username : and a textfield synchronously....

    i dont want that kind of output...i want if a user click the button Admin or User, then it will show up the Username : and the text field...can somebody help me solve this problem

  2. #2
    Join Date
    Nov 2004
    Location
    Norway
    Posts
    1,560

    Try this:

    The init method like:
    Code:
    public void init() {
        setSize(400, 400);
        mainMenu();
    }
    and use this in the actionPerformed method
    Code:
    if (e.getSource() == admin) {
          remove(p);
          userPass();
          validate(); // <--- important
    }
    Whenever you rearrange the contents of a container you must invoke the
    validate() method for the container in order to make the new changes take effect.

    PS: it is good practise to use lowercase as first letter in methods and variables,
    and you should not use a variable name like "TextField", when I saw the code
    it looked like you were using some static methods in java.awt.TextField.
    And a class name like "Main" is not quite ok either, that word is "taken" in
    java.

    This is not picky, check the java coding style guidelines.
    eschew obfuscation

  3. #3
    Join Date
    Aug 2005
    Posts
    6
    thanks sjalle...it really works...thanks also for the guidelines

Similar Threads

  1. Java vs. .Net. A questionnaire
    By Basil in forum .NET
    Replies: 1
    Last Post: 05-13-2005, 06:46 AM
  2. Java Applet Compiler problem?
    By mdl in forum Java
    Replies: 3
    Last Post: 03-07-2005, 03:34 AM
  3. DevX does seem one sideded
    By Rob Abbe in forum Talk to the Editors
    Replies: 44
    Last Post: 01-13-2003, 03:57 PM
  4. using JSObject from within java applet
    By Patrick in forum Java
    Replies: 1
    Last Post: 04-05-2001, 02:12 PM
  5. Replies: 1
    Last Post: 08-01-2000, 01:51 PM

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