• Post Reply Bookmark Topic Watch Topic
  • New Topic

Add textfield under radio buttons

 
Mohammad Ashari Rahman
Ranch Hand
Posts: 55
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi. I want jtextfiled under radio buttons but its always giving me sideways...

 
Campbell Ritchie
Marshal
Posts: 52543
119
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What layout are you using? If Grid, how many rows and how many columns? Have a look at the different sorts of layout available. I think you need to specify numbers of rows and coplumns if you are going to use grid layout.
 
Piet Souris
Rancher
Posts: 1630
35
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Indeed it has to do with the number of colums and rows you sert in your GridLayout (none!);

Try this:
setLayout(new GridLayout(0, 3));
 
Rob Camick
Ranch Hand
Posts: 2699
10
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


You could use:



But that is not very flexible to force all the components into a GridLayout.

Instead you might want to consider using:

1) a GridBagLayout.
2) a vertical BoxLayout. Then you can create a panel with the radio buttons and add it to the frame. Then you can also add the text field.

Read the section from the Swing tutorial on Using Layout Managers for more information and working examples.

Also, the setVisible(...) statement should be invoked AFTER the pack() statement and after all components have been added to the frame.

I suggest you download the demo code from the tutorial to see how you can better structure your class by using the "createAndShowGUI(...)" approach so all your GUI is created on the Event Dispatch Thread.

 
Mohammad Ashari Rahman
Ranch Hand
Posts: 55
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks the Textfield is under now but not centered..Am trying to center it.
 
Piet Souris
Rancher
Posts: 1630
35
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Again, a very quick solution is to add this line between adding option1 and the textfield:
add(new JLabel());
This will force the texfield to appear in the middle column.

A disadvantage of GridLayout is that it makes each cell equal in size. So, an alternative set up may be at hand.
Rob already described some options.
A simple alternative is to use two panels.
Say, panel 1 uses the GridLayout, panel 2 uses a default FlowLayout.
Add the optons to panel 1, the textfield to panel 2, add panel 1 to the PAGE_START of the contentpane, and panel 2 to the CENTER of the contentpane.

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