File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Java in General and the fly likes Lining up JTextFields next to JLabels Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "Lining up JTextFields next to JLabels" Watch "Lining up JTextFields next to JLabels" New topic
Author

Lining up JTextFields next to JLabels

Jeff Grant
Ranch Hand

Joined: Dec 19, 2001
Posts: 169
What do you people suggest I use for a layout when I have a bunch of JLabels next to a bunch of corresponding JTextFields? I am currently using BorderLayout, but they line up with the JLabel, not eachother. I also tried using GridLayout(5,2) for example, but my window is large and the JTextFields are spaced too far from the JLabels. I also took a quick glance at GridBagLayout, but that seemed quite a bit more complicated than this should be (was hoping it was just a simple setx, sety for placement). What do you all think I should use for a layout to do this the simplest way?
Also, is there a way to actually size a JTextField without an ActionListener? For example, having a "new JTextField(10)" only be about 1.5 inches wide on the screen (just an example, size may vary).
Paul Stevens
Ranch Hand

Joined: May 17, 2001
Posts: 2823
I would use GridBagLayout. It really isn't that difficult to use. You don't have to use all of the settings. You might try searching in the Swing/AWT forum.
khella smith
Ranch Hand

Joined: May 06, 2002
Posts: 39
i think u could use panel within panels with different layout managers mainly border and flow layout i have just tried it,works but quite tedious the best to use is the gridbaglayout its not that difficult or if u think it is then u could use the borderlayout east or west(depeding on which side to place it) and center i.e a label and a text field in one panel east or west(depending on which side ) and center then add this panel to another panel with flow layout i think u should do this for every label and textfield they should align when u finish u now add all the panels in one panel again using gridlayout well we sort of had to do it this way in class for practice it worked its quite tedious and needs alot of thinking but gridbag is the best and most simple one
its ur choice
-----------------
khella smith
Jeff Grant
Ranch Hand

Joined: Dec 19, 2001
Posts: 169
Thanks for trying to help. I did some more searches in the AWT / Swing conference here as well as on Sun's site. Unfortunately, GridBagLayout seems way more complicated than anything I needed to deal with. So, fortunate for me, I thought up a different way to do it.
Here's how I did it... The height of my window is approximately 25 lines high (GirdLayout size mind you). So I made my big this.setLayout(new BorderLayout()). I created a new JPanel.setLayout(new GridLayout(25,1)) to hold all of my JLabels. Then I added that panel to the contentPane,BorderLayout.WEST. THEN I created another new JPanel.setLayout(new GridLayout(25,1)) to hold all of my JTextFields... then added that to my contentPane,BorderLayout.CENTER so it'd take as much space as it could and butt up against my JLabels and line up with each other! Works great! Thanks!
Paul Stevens
Ranch Hand

Joined: May 17, 2001
Posts: 2823
Quick example with GridBagLayout. You could play with the settings just a little to get the effect you want. Maybe have the GridBagLayout components in their own panel.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Lining up JTextFields next to JLabels
 
Similar Threads
Tabbing in Swing components
Need help understanding GridBagLayout
Newbie JTextField Listener question
Trouble with GridLayout, BoxLayout .... any sort of layout!
Resizing TextArea()