the layout looks ok initially, but the text area is not re-sized properly when the window is re-sized.
Any idea how to fix it?
Thanks!
import java.awt.BorderLayout
import javax.swing.BorderFactory
import java.awt.GridLayout
import groovy.swing.SwingBuilder
swing = new SwingBuilder()
frame = swing.frame(title:'Test', location:[200,200], size:[300,216]) {
panel(layout: new BorderLayout()){
scrollPane(constraints: BorderLayout.NORTH){
table {
def people = [
['name':'Johan', 'location':'Olso'],
['name':'John', 'location':'London'],
['name':'Jose', 'location':'Madrid'],
['name':'Jos', 'location':'Amsterdam']
]
tableModel( id:'model', list: people) { m ->
propertyColumn(header: 'Name', propertyName: 'name')
propertyColumn(header: 'Location', propertyName: 'location')
}
}
}
}
panel(constraints: BorderLayout.SOUTH){
scrollPane(constraints: BorderLayout.CENTER){
textArea(id:'TextArea', lineWrap:true,wrapStyleWord:true, columns:35, rows:4,editable:true)
}
}
}
frame.show()
Initially OK
After re-size NOT OK
The main source of the problem is that the default layout manager of JPanel
is FlowLayout
, not BorderLayout
, and you're using BorderLayout
constraints for it.
panel(constraints: BorderLayout.CENTER, layout: new BorderLayout()) {
scrollPane(constraints: BorderLayout.CENTER){
textArea(id:'TextArea', lineWrap:true,wrapStyleWord:true, columns:35, rows:4,editable:true)
}
}
expands the text field and the containing panel to all available space. (The change is using CENTER position for the panel, and setting the layout manager for it).
I also placed the table to the NORTH position (since I moved the lower panel to CENTER):
panel(constraints: BorderLayout.NORTH, layout: new BorderLayout()) {
...
You may wish to do otherwise, but as the choise depends on your exact preferences I don't know what is the correct one for you.
You should also use
frame.pack()
frame.show()
instead of explicitly setting the frame size. That fits the frame size to the preferred size of the contained components.