Results 1 to 7 of 7
- 01-29-2013, 02:35 AM #1
Senior Member
- Join Date
- Jan 2013
- Posts
- 317
- Rep Power
- 7
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 02:52 AM.
-
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.
- 01-29-2013, 04:43 AM #3
Senior Member
- Join Date
- Jan 2013
- Posts
- 317
- Rep Power
- 7
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 04:46 AM.
-
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.
- 01-30-2013, 06:30 PM #5
Senior Member
- Join Date
- Feb 2012
- Posts
- 219
- Rep Power
- 8
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.
- 01-31-2013, 01:30 AM #6
Senior Member
- Join Date
- Jan 2013
- Posts
- 317
- Rep Power
- 7
-
Re: Text field code is not working, please help
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 04:57 AM.
Similar Threads
-
Jpassword field is not working
By danpotter in forum AWT / SwingReplies: 4Last Post: 06-05-2012, 10:57 PM -
GUI Clearing Text Field not working
By Aiquoc in forum New To JavaReplies: 3Last Post: 05-31-2011, 09:53 AM -
Help with bcc field and cc field in java code
By eel in forum NetworkingReplies: 1Last Post: 10-25-2010, 01:20 PM -
Regarding Text Field
By adeeb in forum AWT / SwingReplies: 1Last Post: 06-06-2008, 12:01 AM
Bookmarks