Results 1 to 7 of 7
  1. #1
    weezy2894 is offline Member
    Join Date
    Apr 2011
    Location
    SoCal
    Posts
    10
    Rep Power
    0

    Default TextField not displayed

    How come TextField "name" is not displayed when button "mem" is clicked?

    Java Code:
    import java.applet.*; 
    import java.awt.*;
    import javax.swing.JButton;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    public class Test extends Applet implements ActionListener
    {
        Font Title = new Font("Arial",Font.BOLD,40);
        Font Normal = new Font("Arial",Font.PLAIN,20);
        JButton mem, nonmem;
        TextField name; 
        public void init() 
         {
              setLayout(null); 
              Color background = new Color(179,50,55);
              setBackground(background);
              mem = new JButton("I am an existing member");
              mem.addActionListener(this);
              nonmem = new JButton("I am new");
              nonmem.addActionListener(this);
              mem.setBounds(20,100,200,30); 
              nonmem.setBounds(20,150,200,30);
              name = new TextField("Please Enter Your Name Here");
              name.setBounds(20,100,300,25);
              name.addActionListener(this);
        }
         public void actionPerformed(ActionEvent e)
         {
            String source = e.getActionCommand();
            mem.setVisible(false);
            nonmem.setVisible(false);
            if (source.equals(mem)) 
            {
                 add(name);
            } 
            else if (source.equals(nonmem))
            {
             
            }
            else if (source.equals(name))
            {
             
            }
        }
        public void stop() 
        { 
        } 
        public void paint (Graphics g) 
        {
             super.paint(g);
             g.setFont(Title);
             g.drawString("Welcome to Movie2Go Kiosk Services",250,50);
             g.setFont(Normal);
             add(mem);
             add(nonmem);
        }
         
    }

  2. #2
    ra4king's Avatar
    ra4king is offline Senior Member
    Join Date
    Apr 2011
    Location
    Atlanta, Georgia, US
    Posts
    396
    Rep Power
    4

    Default

    Why are you comparing a String with JButton's and JTextField's in the actionPerformed method?

  3. #3
    camickr is offline Senior Member
    Join Date
    Jul 2009
    Posts
    1,226
    Rep Power
    6

    Default

    1. You need to decide whether you are creating an AWT or Swing applet. I would suggest you spend your time learning Swing since that is what most developers use these days. This means don't mix component JButton is Swing. Applet, TextField are AWT. Swing components have a "J" at the beginning.

    2. There is no good reason to override the paint() method of the top level container like a JApplet, JFrame or JDialog. If fact I don't see any reason for doing the custom painting. You add the button and the text field to the applet, so why don't you just add a JLabel as well?

    3. Adding a component will not show the component. You need to tell Swing that a component has been added to the panel by using panel.revalidate().

  4. #4
    ra4king's Avatar
    ra4king is offline Senior Member
    Join Date
    Apr 2011
    Location
    Atlanta, Georgia, US
    Posts
    396
    Rep Power
    4

    Default

    EDIT: Never mind ;)
    Last edited by ra4king; 04-17-2011 at 04:21 AM.

  5. #5
    camickr is offline Senior Member
    Join Date
    Jul 2009
    Posts
    1,226
    Rep Power
    6

    Default

    revalidate() is for Swing. validate() is for AWT.

  6. #6
    ra4king's Avatar
    ra4king is offline Senior Member
    Join Date
    Apr 2011
    Location
    Atlanta, Georgia, US
    Posts
    396
    Rep Power
    4

    Default

    Whoa, I have never seen the revalidate() method. Thanks :)

  7. #7
    weezy2894 is offline Member
    Join Date
    Apr 2011
    Location
    SoCal
    Posts
    10
    Rep Power
    0

Similar Threads

  1. output is not displayed
    By meth in forum New To Java
    Replies: 0
    Last Post: 01-07-2011, 05:44 AM
  2. ImageIcon : Image not displayed
    By niteshwar.bhardwaj in forum Java 2D
    Replies: 0
    Last Post: 02-13-2009, 07:36 AM
  3. Nodes displayed in JTree
    By Orange in forum AWT / Swing
    Replies: 6
    Last Post: 08-08-2008, 05:07 AM
  4. Tooltip not getting displayed...
    By Preethi in forum New To Java
    Replies: 4
    Last Post: 07-31-2008, 10:00 AM
  5. displayed the rownumbers
    By geeta_ravikanti in forum JDBC
    Replies: 1
    Last Post: 04-22-2008, 02:30 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •