How to use Groovy's TableLayout valign attribute

43 views Asked by At

The TableLayou in Groovy's SwingBuilder package supports vertical alignment. But it seems to have no effect when I use it in my code

import groovy.swing.SwingBuilder;
import javax.swing.BorderFactory;
import java.awt.Color;

def ui = new SwingBuilder();
def frame;

frame = ui.frame(title: "Window", bounds: [0, 0, 500, 500], layout: null)
{
   tableLayout(size:[400,400],border: BorderFactory.createLineBorder(Color.RED, 2))
   {
       tr
       {
           td(valign:'top')
           {
               label(text:"Label", border: BorderFactory.createLineBorder(Color.BLUE, 2))
           }
       }
   }
}
;
frame.show();

the label appears vertically centered instead of being located at the top. Are additional atrributes required for proper alignment, or doesn't it work at all with TableLayout ?

1

There are 1 answers

0
Vlad On

The align and valign attributes of TD only take effect on merged columns or rows that have been merged with colspan or rowspan attributes. It's pretty much the same behavior as for HTML tables.

The TableLayout class is nothing other than a panel that uses the GridBagLayout. Every table cell (TD) is just the component added to the panel, with specific GridBagLayout constraints. These constraints are predefined in the class TableLayoutCell. One of these predefined constraints is weighty, which is set to the value 0. This means that the panel/table will be always centered vertically.