Search code examples
javaswinglayoutjgoodies

how to maximise component height using jgoodies forms


Noob question: I have the following Forms layout (please excuse the JRuby syntax). I'd like all three buttons to have their heights stretched to fill the available space. but only button 3 does so.

require 'java'
require './lib/jgoodies-common-1.2.1.jar'
require './lib/jgoodies-forms-1.4.2.jar'

java_import javax.swing.JButton
java_import javax.swing.JFrame

java_import com.jgoodies.forms.layout.CellConstraints
java_import com.jgoodies.forms.layout.FormLayout

class Foo < JFrame
  def initialize
    super
    cc = CellConstraints.new

    layout = FormLayout.new(
      "10dlu, pref:grow, 10dlu, pref:grow, 10dlu",
      "10dlu, pref:grow, 10dlu, pref:grow, 10dlu"
    )
    layout.setRowGroups([[2, 4]])
    layout.setColumnGroups([[2, 4]])

    self.setLayout(layout)

    self.add(JButton.new("button 1"), cc.xy(2, 2))
    self.add(JButton.new("button 2"), cc.xy(2, 4))
    self.add(JButton.new("button 3"), cc.xywh(4, 2, 1, 3))

    self.pack
    self.setVisible(true)
    self.toFront
  end
end

Foo.new

Tips and pointers appreciated.

--Ben


Solution

  • Tell the rowSpec to fill the height:

        FormLayout layout = new FormLayout(
                "10dlu, pref:grow, 10dlu, pref:grow, 10dlu",
                "10dlu, fill:pref:grow, 10dlu, fill:pref:grow, 10dlu"
                );
    

    Technically, that's explicitly overruling the "alignment" of the row, which by default is CENTER. For columns, the default alignment is FILL (so no overrule needed if you want the stretch horizontally). The reason the third button appears to magi-stretching vertically is that it is spanning two content rows: not much else it can align to then fill (as far as I remember, didn't check the details)