Search code examples
javaswinggroovyswingbuilder

GridBagLayout keeps resizing when entering text in text field


I have a problem getting a GridBagLayout in groovy with SwingBuilder to run. I guess the problem is GridBagLayout and not Groovy, so I assume this would work the same in Java.

I have the following simple layout:

new SwingBuilder().edt {
  frame(title: 'test', defaultCloseOperation: JFrame.EXIT_ON_CLOSE, pack: true, show: true) {
    gridBagLayout()
    widget(textPane(background: java.awt.Color.RED), constraints: gbc(gridx:0, gridy: 0, weightx: 0.5, weighty: 1, fill: GBC.BOTH, gridheight: GBC.REMAINDER))
    widget(textPane(background: java.awt.Color.GREEN), constraints: gbc(gridx: 1, gridy: 0, weightx: 0.5, weighty: 1, fill: GBC.BOTH))
    label(text: "test label", background: java.awt.Color.BLUE, opaque: true, constraints: gbc(gridx: 1, gridy: 1, weightx: 0.5, fill: GBC.BOTH))
  }
}

I would like to have a window, with a text pane on the left side taking exactly 50% of the window. On the right side of the windows (the other 50%) another text pane and below that (but only taking minimum required size) a label.

The code above works and creates the window I want, but as soon as I begin to type in the left sided text pane, the left side will start to take more space (than 50% of the window).

How can I achieve, the above described layout, so the both sided will always be 50:50 no matter what text is entered into the text panes?


Solution

  • I would like to have a window, with a text pane on the left side taking exactly 50% of the window.

    Then you should be using a panel with a GridLayout for those two components.

    On the right side of the windows (the other 50%) another text pane and below that (but only taking minimum required size) a label.

    Then maybe a BorderLayout with the text pane added to the "CENTER" and the label added to the "PAGE_END".