Results 1 to 11 of 11
  1. #1
    Tainted is offline Member
    Join Date
    Jan 2012
    Posts
    6
    Rep Power
    0

    Default Can't make a textfield = string.

    So, I'm making a basic chatroom, and putting in a way to enter your name.
    So I made a textfield for entering your name, I want to write that to a string so i can recall it each time someone talks.
    Here's what I got:

    String Userinput = Usernameinput.getText();

    Usernameinput is the textfield

    Thanks for the help

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default Re: Can't make a textfield = string.

    Quote Originally Posted by Tainted View Post
    So, I'm making a basic chatroom, and putting in a way to enter your name.
    So I made a textfield for entering your name, I want to write that to a string so i can recall it each time someone talks.
    Here's what I got:

    String Userinput = Usernameinput.getText();

    Usernameinput is the textfield

    Thanks for the help
    OK, ...

    and your specific question is?

  3. #3
    Tainted is offline Member
    Join Date
    Jan 2012
    Posts
    6
    Rep Power
    0

    Default Re: Can't make a textfield = string.

    how do i make a textfield set a string

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default Re: Can't make a textfield = string.

    Quote Originally Posted by Tainted View Post
    how do i make a textfield set a string
    Are you coding with Swing? Is this a JTextField? If so, tell us, and if not, tell us what GUI library and component you're talking about as these are key details that we need to know. We also need to know exactly what problem you're having with your current code as you haven't said yet. You probably already know that programming is an exercise in precision, and so details are important when asking questions here. So far you haven't provided enough so that your question can be answered well.

  5. #5
    Tainted is offline Member
    Join Date
    Jan 2012
    Posts
    6
    Rep Power
    0

    Default Re: Can't make a textfield = string.

    Guess im not making myself clear, sorry.
    Im making a applet with a basic textfield with a button to login. No swing.
    In this textfield you enter a name or whatever, then it sets the usernameinput.
    The problems seems to be where it sets the textfield to a string, as i get a null print out.

  6. #6
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default Re: Can't make a textfield = string.

    Quote Originally Posted by Tainted View Post
    Guess im not making myself clear, sorry.
    Im making a applet with a basic textfield with a button to login. No swing.
    In this textfield you enter a name or whatever, then it sets the usernameinput.
    The problems seems to be where it sets the textfield to a string, as i get a null print out.
    Please show the code then with code tags so we can see what you're doing wrong. So then are you using AWT? If so then you're using a TextField not a textfield. This may seem trivial to you, but after you've done this for a while, you'll see how important these little details are. Please take care to tell us exactly what classes you're using and take care to spell and capitalize them correctly.

    Luck.

  7. #7
    Tainted is offline Member
    Join Date
    Jan 2012
    Posts
    6
    Rep Power
    0

    Default Re: Can't make a textfield = string.

    Okay, give me like 10 minutes or so ill post it up.
    Btw, sorry I type fast and no I dont use caps cause im rushing.

  8. #8
    Tainted is offline Member
    Join Date
    Jan 2012
    Posts
    6
    Rep Power
    0

    Default Re: Can't make a textfield = string.

    Java Code:
    package test1;
    
    import java.applet.Applet;
    import java.awt.*;
    import java.awt.event.*;
    import java.awt.event.ActionEvent;
    import java.util.*;
    
    
    public class Test1 extends Applet implements Runnable, ActionListener{
        
        String Userinput;
        Label Username;
        TextField Usernameinput;
        Button Button1;
        
        @Override
        public void init(){
        //Set Size
        setSize(800, 600);
        //Layout
        setLayout(new FlowLayout());
        //Login Label
        Username = new Label("Username:");
        add(Username);
        //Input for username
        Usernameinput = new TextField(12);
        add(Usernameinput);
        //
        Button1 = new Button("Login");
        Button1.addActionListener(this);
        add(Button1);
        }
        
        @Override
        public void start() {}
     
        @Override
        public void run(){}
        
        @Override
        public void stop(){}
            
        @Override
        public void destroy(){}
        
        @Override
        public void paint(Graphics g){
            //Displaying the userinput(TextField)
        g.drawString(Userinput,200,200);
        }
        
        @Override
        public void update(Graphics g){}
    
        @Override
        public void actionPerformed(ActionEvent e) {
            //Made the string set after you click login.
            String Userinput = Usernameinput.getText();
            
        }
    }
    --------
    I don't get any errors but a blank drawString
    Last edited by Tainted; 01-29-2012 at 01:21 AM. Reason: code tags added

  9. #9
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default Re: Can't make a textfield = string.

    I've added code tags to your post so that the code retains its formatting, and you may wish to do this in future posts.

    On to your problem: The problem with your code is one known as variable "shadowing". You're redeclaring the Userinput variable inside of the actionPerformed method, and yes, you set this variable to hold the text from the TextField, but since this variable is declared in the method, it is local to the method and is not visible outside of the method, and also is in no way related to the class field of the same name. To fix this, use the class field and don't re-declare the variable inside of the method.

    edit: arghhhh you've erased my code tags! please don't do that. I'll let you put them back in.

    anyway an example of your problem:

    Java Code:
    public class ShadowingEg {
       private int foo;
       
       public void method1(int bar) {
          // foo is re-declared and thus shadows the class field
          int foo = bar;
       }
       
       public void method2(int bar) {
          // correct way to do this
          foo = bar;
       }
    
    }
    Last edited by Fubarable; 01-29-2012 at 01:08 AM.

  10. #10
    Tainted is offline Member
    Join Date
    Jan 2012
    Posts
    6
    Rep Power
    0

    Default Re: Can't make a textfield = string.

    Okay, I didn't think about that, i'm sure i can fix it now.

    Thanks for you help.

  11. #11
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default Re: Can't make a textfield = string.

    Quote Originally Posted by Tainted View Post
    Okay, I didn't think about that, i'm sure i can fix it now.

    Thanks for you help.
    You're welcome, but again, you've erased the code tags that I have added to your code post. Please add them back.

    [code]
    your code here
    [/code]

Similar Threads

  1. Replies: 1
    Last Post: 12-11-2011, 11:59 AM
  2. Make the TextField unable to write on
    By Josep_16 in forum Java Applets
    Replies: 4
    Last Post: 08-21-2011, 08:06 AM
  3. How to make Swing JTextField look like AWT TextField?
    By LeonLanford in forum AWT / Swing
    Replies: 1
    Last Post: 09-04-2010, 04:06 PM
  4. how to make window active and textfield focused
    By whwillisiv in forum New To Java
    Replies: 0
    Last Post: 04-28-2009, 04:06 AM
  5. make a variable name from a string?
    By Kinnikinnick in forum New To Java
    Replies: 3
    Last Post: 11-13-2007, 03:54 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
  •