• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

JTextField not displaying initial value

 
tom davies
Ranch Hand
Posts: 168
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Bartender
Posts: 825
5
Java Python Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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).
 
Michael Dunn
Ranch Hand
Posts: 4632
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
result.setHorizontalAlignment(JTextField.RIGHT);
 
tom davies
Ranch Hand
Posts: 168
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic