Results 1 to 7 of 7
  1. #1
    MW130 is offline Senior Member
    Join Date
    Jan 2013
    Posts
    317
    Rep Power
    2

    Default Text field code is not working, please help

    hi guys. I am trying to make a text box in swing so that I can write in it. I have a problem though; the window pops up, but nothing appears on it.. What could I be doing wrong? also, if you can, could you explain the line that I put a comment on, i'd really appreciate it. Thank you all very much in advanced.
    Regards, MW130

    Java Code:
    import java.awt.*;
    import javax.swing.*;
    public class TBox extends JFrame {
    	JPanel p1 = new JPanel();
    	JFrame frame = new JFrame();
    public void TBox(){
    	setSize(500,500);
    	setDefaultCloseOperation(EXIT_ON_CLOSE);
    	setVisible(true);
    	setTitle("The Box of text.");	}
    public static void main(String[] args){
    	TBox obj = new TBox();
    	JTextField NUM = new JTextField("Hey. Write here!", 20); /* right here, what is the 20 doing? I am learning from a tutorial but it doesn't explain. Also, what is in quote is just what will be displayed inside of the box, correct? Thanks!*/
    	obj.p1.add(NUM);
    	obj.TBox();
        }
    }
    Last edited by MW130; 01-29-2013 at 01:52 AM.

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

    Default Re: Text field code is not working, please help

    You're calling setVisible *before* adding components to the JFrame. You shouldn't extend a JFrame in general by the way. Just use it.

  3. #3
    MW130 is offline Senior Member
    Join Date
    Jan 2013
    Posts
    317
    Rep Power
    2

    Default Re: Text field code is not working, please help

    Hi Fubarable, thanks for the reply. I cannot add the textfield anywhere else in the code. In the TBox method, it won't let me, and right after creating the panel it wont let me either. Thanks.

    EDIT: I now created the panel inside of the TBox method, and then added the textbox to it right after. This compiled, but I am still sadly not seeing my textbox.. :(
    Last edited by MW130; 01-29-2013 at 03:46 AM.

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

    Default Re: Text field code is not working, please help

    OK, if you're not seeing it then you have to ask yourself, has it been added to something whose ancestor hierarchy leads to a top level window? In other words, what component have you added your JTextField to? And does this component exist inside of a JFrame or inside of a component that is held by a JFrame? Check carefully and you'll see that it in fact does not.

  5. #5
    Wnt2bsleepin is offline Senior Member
    Join Date
    Feb 2012
    Posts
    219
    Rep Power
    3

    Default Re: Text field code is not working, please help

    Just to chime in, you really need to familiarize yourself with Javadocs.

    If you look at the constructor for a JTextField, it tells you what the parameters are.

  6. #6
    MW130 is offline Senior Member
    Join Date
    Jan 2013
    Posts
    317
    Rep Power
    2

    Default Re: Text field code is not working, please help

    Quote Originally Posted by Fubarable View Post
    OK, if you're not seeing it then you have to ask yourself, has it been added to something whose ancestor hierarchy leads to a top level window? In other words, what component have you added your JTextField to? And does this component exist inside of a JFrame or inside of a component that is held by a JFrame? Check carefully and you'll see that it in fact does not.
    Hey, sorry for not replying to this first, but I still did not get it, and I didn't get your answer. I know that that frustrates you, so I decided to leave it at that.

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

    Default Re: Text field code is not working, please help

    Quote Originally Posted by MW130 View Post
    Hey, sorry for not replying to this first, but I still did not get it, and I didn't get your answer. I know that that frustrates you, so I decided to leave it at that.
    And if you ignore responses, then no one will want to help you.

    Please read your code and critically think about what you're adding to what. This isn't an exercise in programming but more in just plain simple logic. If you put a painting in an attic, it cannot be seen by anyone. Rather if you put a painting on the wall, then all will see it.

    What are you adding the JTextField to? To the p1 JPanel field of the TBox class. And what are you adding the p1 to? Nothing. If p1 is added to nothing, then it will not be displayed. It's still in the attic. To display anything, it must be shown on something that eventually is a top level window -- here the JFrame. To show the p1 content, it must eventually ether be added to the JFrame or to a component that eventually leads to the JFrame.

    It's frustrating that you simply ignored my request for a little effort to try to solve this. I don't mind your not knowing so much as not showing that you're trying. Maybe programming isn't for you.
    Last edited by Fubarable; 01-31-2013 at 03:57 AM.

Similar Threads

  1. Jpassword field is not working
    By danpotter in forum AWT / Swing
    Replies: 4
    Last Post: 06-05-2012, 09:57 PM
  2. GUI Clearing Text Field not working
    By Aiquoc in forum New To Java
    Replies: 3
    Last Post: 05-31-2011, 08:53 AM
  3. Replies: 1
    Last Post: 10-25-2010, 12:20 PM
  4. Regarding Text Field
    By adeeb in forum AWT / Swing
    Replies: 1
    Last Post: 06-05-2008, 11:01 PM

Tags for this Thread

Posting Permissions

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