Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 4 of 4

Thread: Help with JButtons in a GridLayout?

  1. #1
    Junior Member
    Join Date
    Nov 2013
    Posts
    8
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Help with JButtons in a GridLayout?

    Hi,

    I am building a game in which I have a few buttons set in a JPanel with a GridLayout. This panel is put on the East side of another JPanel which has a BorderLayout. When done this way, the buttons stretch themselves to take up all the vertical space in the JPanel. Here is a screenshot:

    link

    I wanted to fix these buttons so they would be a fixed height and not stretch like this, so at first I looked into using SWT and GridData objects to configure them. I know how to code it that way, but unfortunately I had some problems getting the SWT libraries to work right.

    Is there a simpler way to tell these buttons not to stretch this way? Any help appreciated.


  2. #2
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,295
    My Mood
    Mellow
    Thanks
    193
    Thanked 669 Times in 656 Posts

    Default Re: Help with JButtons in a GridLayout?

    You've verified the defined operation of a GridLayout. If you are not satisfied with this, then I recommend you investigate other layout managers.

  3. #3
    Junior Member
    Join Date
    Nov 2013
    Posts
    8
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Help with JButtons in a GridLayout?

    SWT allows you to change this default behavior of a gridlayout with GridData objects and the boolean grabExcessVerticalSpace field. As I am new to Swing, I was only wondering if it offered a similar option. Of course I looked through the API, but in the case of the SWT example I offered above, sometimes these things are determined by more than a single Object.

    It is unfortunate that the attitudes of some developers have deteriorated to the point where they seem to spend their time looking for reasons to be jerks rather than offering simple and polite answers to those with less experience. Congratulations: you've been unpleasant and rude to a newcomer when a simple "no" answer would have sufficed. You should be very proud.

    I will not be back to this forum.

  4. #4
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,307
    Thanks
    181
    Thanked 824 Times in 767 Posts
    Blog Entries
    5

    Default Re: Help with JButtons in a GridLayout?

    At the risk of speaking to an empty room (or being called rude for trying to help): as mentioned above, this is the default (and from what I know not directly customizable) behavior of GridLayout - so you could try a different LayoutManager, for instance GridBagLayout. Alternatively, nest your panel inside another which uses a different LayoutManager - for instance nest the GridLayout panel inside another JPanel having a FlowLayout.

Similar Threads

  1. Buttons Multicoloured gridlayout
    By newtolearningjava in forum What's Wrong With My Code?
    Replies: 7
    Last Post: April 13th, 2014, 03:19 PM
  2. GridLayout not working the way i want it
    By gokuball in forum AWT / Java Swing
    Replies: 3
    Last Post: August 16th, 2013, 10:02 AM
  3. GridLayout/JButton question
    By captain in forum Object Oriented Programming
    Replies: 1
    Last Post: February 24th, 2012, 01:05 PM
  4. GridLayout help
    By sambar89 in forum AWT / Java Swing
    Replies: 1
    Last Post: November 26th, 2011, 10:00 PM
  5. Images in GridLayout
    By BuhRock in forum AWT / Java Swing
    Replies: 4
    Last Post: November 5th, 2011, 12:15 AM