File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Swing / AWT / SWT and the fly likes JTextField not displaying initial value Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "JTextField not displaying initial value" Watch "JTextField not displaying initial value" New topic

JTextField not displaying initial value

tom davies
Ranch Hand

Joined: Apr 27, 2012
Posts: 168
I am creating a calculator in java
I am using a JTextField to display the result of the operations. For some reason there is nothing at all appearing in the JTextFIeld.

I have tried the way it is above, and also using the commented out line as well. I can see the JTextField has been placed onto my panel and frame, but no value appears in it.
Kemal Sokolovic

Joined: Jun 19, 2010
Posts: 825

When you put JTextField to your frame as you did in your code, it's placed at the top of the frame (north) and centered horizontally. Since you created text field of width 400 (characters) it's to wide so you're seeing just the middle part of it, and the beginning (where the text should appear) is hidden.

Put smaller value in the constructor of text field and you should be able to see it. If you want it to be as wide as the frame then change the layout of its parent panel (e.g. to GridBagLayout).

The quieter you are, the more you are able to hear.
Michael Dunn
Ranch Hand

Joined: Jun 09, 2003
Posts: 4632
the alignment problem is mainly caused by adding the textfield to a JPanel
with default layout of FlowLayout, which will center the textfield in the panel.

get rid of the panel.
also, you may want to use this for a calculator
tom davies
Ranch Hand

Joined: Apr 27, 2012
Posts: 168
Looks like i underestimated (by a long way) the size of the textFIeld. Apparently 30 characters is the limit for my frame size.
It is all working now though, thanks.

EDIT: Michael your right, i don't really need that JPanel. I have adjusted it now and the right alignment makes it looks a lot better as well
Thank you.
I agree. Here's the link:
subject: JTextField not displaying initial value
It's not a secret anymore!