Bookmark Topic Watch Topic
  • New Topic

About GridBagLayout  RSS feed

 
anitha nair
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Report post to moderator
Hi forum,

Please help me trace my error.

I am displaying JTextFields in a JPanel.
tf[j] =new JTextField();
tf[j].setSize(new Dimension(50,25));
jPanel.add(tf[j],true);

tf[j].setBounds(100,(100+(j*50)),50,10);
I am able to set the size of the text fields only when the panel is having GridBagLayout.For all other layouts the textfield gets redused to small size.

I am getting only one textField in the top most left corner even if there are more than one.

Will any one please tel me what changes should i bring about in the setBound of the textField to display it all in a column.
Or do i need to set any other properties of the GridBagLayout to get it correct?

Thank you all.
 
Ulf Dittmer
Rancher
Posts: 42972
73
  • Mark post as helpful
  • send pies
  • Report post to moderator
Not a direct answer, but setBounds sets the the position of the component, which may clash with any layout you use (which also sets the position). Remove the setBounds call and see what happens. Also, is there a particular reason you're using GridBagLayout? It's rather involved - GridLayout would be easier to get right for a tabular layout.
 
Ilja Preuss
author
Sheriff
Posts: 14112
  • Mark post as helpful
  • send pies
  • Report post to moderator
As Ulf said, you should never call setBounds.

Doesn't a JTextField have a constructor that accepts the number of columns the field should have? If I remember correctly, that basically affects the preferredSize of the text field...
 
Ilja Preuss
author
Sheriff
Posts: 14112
  • Mark post as helpful
  • send pies
  • Report post to moderator
Rats, there is also a discussion going on at http://www.coderanch.com/t/343360/GUI/java/trouble-setting-size-JTextField

I'm closing this thread to prevent more time being wasted by duplicated effort... :roll:
 
It is sorta covered in the JavaRanch Style Guide.
    Bookmark Topic Watch Topic
  • New Topic
Boost this thread!