Results 1 to 5 of 5
  1. #1
    jamesfrize is offline Member
    Join Date
    Mar 2008
    Posts
    19
    Rep Power
    0

    Default [SOLVED] Confused by the textField

    I'm just having a look at ways to input and retrieve a string from a text field.
    I've got confused somewhere along the way now I get an exception thrown and my components are missing from the JFrame (Plus It won't setBounds)
    Any help would be appreciated, Das code:


    Java Code:
    import javax.swing.*;
    import java.awt.*; 
    import java.awt.event.*;
    
    public class ActionExample extends JFrame 
    {
         Button goButton; 
         TextField tField; 
    
         public void ActionExample()  
         { 
              setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);         
              setBounds(0,0,200,200);
                   
              Container c = getContentPane();
              c.setLayout(new FlowLayout());
              
              tField = new TextField("*Enter your text here*",35);
              goButton = new Button("Go"); 
              
              c.add(tField);
              c.add(goButton); 
              
              goButton.addActionListener
              (
                  new ActionListener()
                  {
                      public void actionPerformed( ActionEvent evt )
                      {
                          repaint();
                      }
                  }
              );
         }
             
         public static void main(String args[])
         {
              ActionExample ae = new ActionExample();
              ae.setVisible(true);     
         }
    
         public void paint(Graphics g) 
         { 
              g.drawString(tField.getText(),20,100);         
         }
    
    }
    Cheers :{p

  2. #2
    hardwired's Avatar
    hardwired is offline Senior Member
    Join Date
    Jul 2007
    Posts
    1,576
    Rep Power
    8

    Default

    Java Code:
    Change
         public void ActionExample()
    to
         public ActionExample()

  3. #3
    jamesfrize is offline Member
    Join Date
    Mar 2008
    Posts
    19
    Rep Power
    0

    Default Cheers, :{p

    Nice one! I can't believe I missed that. I'm assuming it can't be void because it returns a string from the textField to the public void paint(Graphics g)

  4. #4
    hardwired's Avatar
    hardwired is offline Senior Member
    Join Date
    Jul 2007
    Posts
    1,576
    Rep Power
    8

    Default

    it can't be void because it returns a string from the textField to the public void paint(Graphics g)
    No. It doesn't return anything. To return something to the paint method the paint method would have to call it first, eg,
    Java Code:
    public String getString { ... }
    
    public void paint(Graphics g) {
        ...
        String s = getString();
        ...
    }
    Actually, it's a constructor which is the only kind of method that does not have a return type in its method declaration. Java calls the constructor for class initialization. With the return type included, viz, void the method was never called.

  5. #5
    jamesfrize is offline Member
    Join Date
    Mar 2008
    Posts
    19
    Rep Power
    0

    Default thanks for your help

    Cheers, Are you like the only person on here? I't seems like your always giving answers. Much appreciated :{p

Similar Threads

  1. set focus to the textfield
    By paty in forum New To Java
    Replies: 2
    Last Post: 07-09-2010, 08:28 AM
  2. Allowing only numeric values in a TextField
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 03-01-2008, 10:08 PM
  3. what does it mean:confused:
    By sivasayanth in forum New To Java
    Replies: 2
    Last Post: 01-12-2008, 04:52 AM
  4. JSP - getting value from a textfield
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 12-01-2007, 08:58 PM
  5. alignment of textfield in awt
    By nitinborge5 in forum New To Java
    Replies: 2
    Last Post: 07-30-2007, 11:16 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
  •