Results 1 to 4 of 4
  1. #1
    typedef is offline Member
    Join Date
    Feb 2014
    Posts
    52
    Rep Power
    0

    Default Make a cell width larger than largest gridy (GridBagLayout)

    Hello, this question has been plaguing me for the last 2 weeks after many searches on how to resolve this problem. Perhaps its mostly due to the fact that I don't know how to phrase without a good description. Here goes:
    How do I make a cell in GridBagLayout wider than the total x coordinates of the grid. For example, say I have the following components:
    JLabel:
    gridx & gridy = 0
    gridwidth & gridheight = 1
    JTextField
    gridx = 1
    gridy = 0
    gridwidth & gridheight = 1
    JLabel:
    gridx = 0
    gridy = 1
    gridwidth & gridheight = 1
    JTextField
    gridx = 1
    gridy = 1
    gridwidth = 3
    gridheight = 1

    I did not include the weights but they are set to 1.0. This last textfield in code will not extend 3 cells it only takes up its own cell. I'm assuming this is because the last textfield hits a y coordinate of 2 (visualizing the coordinates starting from 1). I would like it to extend a width of 3 cells without having to use a GridLayout so I may use the benefits of padding, fill, etc. How can I do this without jumping hoops of invisible components at farther cells or some other hack?

  2. #2
    camickr is offline Senior Member
    Join Date
    Jul 2009
    Posts
    1,236
    Rep Power
    7

    Default Re: Make a cell width larger than largest gridy (GridBagLayout)

    Read the section from the Swing tutorial on How to Use GridBagLayout (The Java™ Tutorials > Creating a GUI With JFC/Swing > Laying Out Components Within a Container), It has a working example of a button that spans 3 cells.

  3. #3
    typedef is offline Member
    Join Date
    Feb 2014
    Posts
    52
    Rep Power
    0

    Default Re: Make a cell width larger than largest gridy (GridBagLayout)

    Unfortunately, I've already done my homework of RTFM but that is precisely why I'm asking the question. The example you are relating is irrelevant. Allow me to me illustrate:
    Make a cell width larger than largest gridy (GridBagLayout)-gridbaglayoutexample.png
    In the picture above right you can see that button extends three gridwidths because button 3 (reemphasized with a black circle) validates the layout to have 3 columns. In my example, shown on the left, there is no component to validate column 3. What I wish to do is extend the yellow component (a JTextField) to have a component space of 2 cells. How can I do this since I have no component in gridx 2 (the the third column)? All I can think of is to place an invisible component in column 3 someway or wrapping components in another component with another layout manager.

    Any advice/help is greatly appreciated. Thank you for your time.

  4. #4
    camickr is offline Senior Member
    Join Date
    Jul 2009
    Posts
    1,236
    Rep Power
    7

    Default Re: Make a cell width larger than largest gridy (GridBagLayout)

    How can I do this without jumping hoops of invisible components at farther cells
    I don't think you can.

    If you play with the tutorial example you will note that the first row contains 3 components each with a default width of 1 cell and the second row contains a component with a cell width of 3.

    Try changing the example so that each cell on the first row has a column width of 2 and the component on the 2nd row has a with of 6.

    Intuitively this is the same layout since all components have the same relative width (each row has a total width of 6 instead of 3), however it doesn't work.

    As far as I know you need to have a component in every column in order for the layout to determine the number of columns supported. You can't just arbitrarily say your "yellow" component takes up 2 cells because there is no width for the extra cell that doesn't appear in the first row. Columns widths can only be calculated when a real component is added to a column.

    I would guess you need to nest panels to get you layout. So for the second row you create a panel and add multiple components to the panel.

Similar Threads

  1. Make specific cell a hyperlink (triggers event)
    By javanewbie in forum AWT / Swing
    Replies: 3
    Last Post: 06-21-2009, 03:26 AM
  2. Help with larger image
    By cachi in forum Java Applets
    Replies: 1
    Last Post: 08-07-2007, 07:59 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •