• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Liutauras Vilda
  • Jeanne Boyarsky
  • paul wheaton
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Henry Wong
Saloon Keepers:
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Tim Moores
  • Mikalai Zaikin
Bartenders:
  • Frits Walraven

Lining up JTextFields next to JLabels

 
Ranch Hand
Posts: 169
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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).
 
Ranch Hand
Posts: 2823
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 39
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 169
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 2823
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
And then the flying monkeys attacked. My only defense was this tiny ad:
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic