• Post Reply Bookmark Topic Watch Topic
  • New Topic

Setting size of JTextField in Boxlayout?

 
John Corkrend
Ranch Hand
Posts: 51
Chrome Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I built a program that simply asks for the users name and returns it in the form of a message. It works and all, the only problem is that the textfield is huge and takes up most of the screen. I want to know how to set the size of the JtextField in a boxlayout.

Would I use .setSize or .setPrefferedSize? Here is the code for my program if you want.

 
Paul Clapham
Sheriff
Posts: 21892
36
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In Swing, setPreferredSize is always, um, preferred instead of setSize.
 
Darryl Burke
Bartender
Posts: 5155
11
Java Netbeans IDE Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
But in this case, you should look at one of the JTextField constructors that takes an int parameter, as that allows it to compute its own preferredSize.
 
Ramesh Pramuditha Rathnayake
Ranch Hand
Posts: 178
2
Java MySQL Database Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I don't know the reason for enlarging only JTextField. But you can't avoid this by calling setSize() or setPrefferedSize() methods. You have to add the component to new JPanel with FlowLayout and then add it to row1 JPanel.

eg:
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!