{"id":442,"date":"2012-09-04T14:26:26","date_gmt":"2012-09-04T08:56:26","guid":{"rendered":"http:\/\/ramkulkarni.com\/blog\/?p=442"},"modified":"2012-09-04T14:26:26","modified_gmt":"2012-09-04T08:56:26","slug":"eclipse-swt-gridlayout-making-the-widget-fill-horizontally","status":"publish","type":"post","link":"http:\/\/ramkulkarni.com\/blog\/eclipse-swt-gridlayout-making-the-widget-fill-horizontally\/","title":{"rendered":"Eclipse SWT GridLayout &#8211; Making the widget fill horizontally"},"content":{"rendered":"<p>I have been stuck with the problem of making UI widgets fill horizontally in a grid layout a few times. The solution is simple, but when I have to create UI using Eclipse SWT after a long gap, I tend to forget how I had made this work earlier, so I decided to blog about this.<\/p>\n<p>I had to create a simple UI with one label and associated text box. I coded it like this &#8211;<\/p>\n<pre style=\"color: #000020; background: #f6f8ff;\">GridLayout layout = new GridLayout(2, false)<span style=\"color: #308080;\">;<\/span>\ntabContainer.setLayout(layout)<span style=\"color: #308080;\">;<\/span> <span style=\"color: #595979;\">\/\/this is the parent composite<\/span>\n\nLabel label1 = new Label(tabContainer, SWT.NONE)<span style=\"color: #308080;\">;<\/span>\nlabel1.setText(\"Some Label:\")<span style=\"color: #308080;\">;<\/span>\nGridData gd = new GridData()<span style=\"color: #308080;\">;<\/span>\ngd.grabExcessHorizontalSpace = false<span style=\"color: #308080;\">;<\/span>\nlabel1.setLayoutData(gd)<span style=\"color: #308080;\">;<\/span>\n\nText txtBox1 = new Text(tabContainer, SWT.BORDER)<span style=\"color: #308080;\">;<\/span>\ngd = new GridData()<span style=\"color: #308080;\">;<\/span>\ngd.grabExcessHorizontalSpace = true<span style=\"color: #308080;\">;<\/span>\ntxtBox1.setLayoutData(gd)<span style=\"color: #308080;\">;<\/span><\/pre>\n<p><!--more--><\/p>\n<p>UI created by the above code is &#8211;<\/p>\n<p><a href=\"https:\/\/i0.wp.com\/138.197.85.232\/blog\/wp-content\/uploads\/2012\/09\/gd11.png\"><img loading=\"lazy\" decoding=\"async\" data-attachment-id=\"445\" data-permalink=\"http:\/\/ramkulkarni.com\/blog\/eclipse-swt-gridlayout-making-the-widget-fill-horizontally\/gd1-2\/\" data-orig-file=\"https:\/\/i0.wp.com\/ramkulkarni.com\/blog\/wp-content\/uploads\/2012\/09\/gd11.png?fit=497%2C52\" data-orig-size=\"497,52\" data-comments-opened=\"1\" data-image-meta=\"{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;0&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;,&quot;orientation&quot;:&quot;0&quot;}\" data-image-title=\"gd1\" data-image-description=\"\" data-image-caption=\"\" data-medium-file=\"https:\/\/i0.wp.com\/ramkulkarni.com\/blog\/wp-content\/uploads\/2012\/09\/gd11.png?fit=300%2C31\" data-large-file=\"https:\/\/i0.wp.com\/ramkulkarni.com\/blog\/wp-content\/uploads\/2012\/09\/gd11.png?fit=497%2C52\" class=\"alignnone size-full wp-image-445\" title=\"gd1\" src=\"https:\/\/i0.wp.com\/138.197.85.232\/blog\/wp-content\/uploads\/2012\/09\/gd11.png?resize=497%2C52\" alt=\"\" width=\"497\" height=\"52\" srcset=\"https:\/\/i0.wp.com\/ramkulkarni.com\/blog\/wp-content\/uploads\/2012\/09\/gd11.png?w=497 497w, https:\/\/i0.wp.com\/ramkulkarni.com\/blog\/wp-content\/uploads\/2012\/09\/gd11.png?resize=300%2C31 300w\" sizes=\"(max-width: 497px) 100vw, 497px\" data-recalc-dims=\"1\" \/><\/a><\/p>\n<p>With grabExcessHorizontalSpace attribute of GridData set to true, I was expecting text box to fill the horizontal space. JavaDoc for this attribute says &#8211; &#8220;grabExcessHorizontalSpace specifies whether the width of the cell changes depending on the size of the parent Composite&#8221;. So I thought it should be enough to make text box fill horizontal space. But it did not.<\/p>\n<p>After digging into previously written code for similar UI, I found that I had to set another attribute to make it work. I had to set gd.horizontalAlignment = SWT.FILL;<\/p>\n<pre style=\"color: #000020; background: #f6f8ff;\">Text txtBox1 = new Text(tabContainer, SWT.BORDER)<span style=\"color: #308080;\">;<\/span>\ngd = new GridData()<span style=\"color: #308080;\">;<\/span>\ngd.grabExcessHorizontalSpace = true<span style=\"color: #308080;\">;<\/span>\ngd.horizontalAlignment = SWT.FILL<span style=\"color: #308080;\">;<\/span>\ntxtBox1.setLayoutData(gd)<span style=\"color: #308080;\">;<\/span><\/pre>\n<p>The above change generated the UI that I was expecting &#8211;<\/p>\n<p><a href=\"https:\/\/i0.wp.com\/138.197.85.232\/blog\/wp-content\/uploads\/2012\/09\/gd21.png\"><img loading=\"lazy\" decoding=\"async\" data-attachment-id=\"446\" data-permalink=\"http:\/\/ramkulkarni.com\/blog\/eclipse-swt-gridlayout-making-the-widget-fill-horizontally\/gd2-2\/\" data-orig-file=\"https:\/\/i0.wp.com\/ramkulkarni.com\/blog\/wp-content\/uploads\/2012\/09\/gd21.png?fit=501%2C49\" data-orig-size=\"501,49\" data-comments-opened=\"1\" data-image-meta=\"{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;0&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;,&quot;orientation&quot;:&quot;0&quot;}\" data-image-title=\"gd2\" data-image-description=\"\" data-image-caption=\"\" data-medium-file=\"https:\/\/i0.wp.com\/ramkulkarni.com\/blog\/wp-content\/uploads\/2012\/09\/gd21.png?fit=300%2C29\" data-large-file=\"https:\/\/i0.wp.com\/ramkulkarni.com\/blog\/wp-content\/uploads\/2012\/09\/gd21.png?fit=501%2C49\" class=\"alignnone size-full wp-image-446\" title=\"gd2\" src=\"https:\/\/i0.wp.com\/138.197.85.232\/blog\/wp-content\/uploads\/2012\/09\/gd21.png?resize=501%2C49\" alt=\"\" width=\"501\" height=\"49\" srcset=\"https:\/\/i0.wp.com\/ramkulkarni.com\/blog\/wp-content\/uploads\/2012\/09\/gd21.png?w=501 501w, https:\/\/i0.wp.com\/ramkulkarni.com\/blog\/wp-content\/uploads\/2012\/09\/gd21.png?resize=300%2C29 300w\" sizes=\"(max-width: 501px) 100vw, 501px\" data-recalc-dims=\"1\" \/><\/a><\/p>\n<p>I think name &#8216;horizontalAlignment&#8217; is confusing &#8211; JavaDoc says : &#8220;horizontalAlignment specifies how controls will be positioned horizontally within a cell. The default value is BEGINNING&#8221;. So you might think that this attribute controls position of the widget in the cell, but it also has a value that controls its size (SWT.FILL).<\/p>\n<p>Note that both the attributes,\u00a0grabExcessHorizontalSpace and horizontalAlignment, have to be set to make this work.<\/p>\n<p>So next time I face the same problem, I will remember to refer to this post.<\/p>\n<p>-Ram Kulkarni<\/p>\n","protected":false},"excerpt":{"rendered":"<p>I have been stuck with the problem of making UI widgets fill horizontally in a grid layout a few times. The solution is simple, but when I have to create UI using Eclipse SWT after a long gap, I tend to forget how I had made this work earlier, so I decided to blog about &hellip; <\/p>\n<p class=\"link-more\"><a href=\"http:\/\/ramkulkarni.com\/blog\/eclipse-swt-gridlayout-making-the-widget-fill-horizontally\/\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;Eclipse SWT GridLayout &#8211; Making the widget fill horizontally&#8221;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":false,"jetpack_social_options":{"image_generator_settings":{"template":"highway","enabled":false}}},"categories":[38,1],"tags":[25,47,48,49],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"jetpack_shortlink":"https:\/\/wp.me\/p2g9O8-78","jetpack-related-posts":[],"_links":{"self":[{"href":"http:\/\/ramkulkarni.com\/blog\/wp-json\/wp\/v2\/posts\/442"}],"collection":[{"href":"http:\/\/ramkulkarni.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/ramkulkarni.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/ramkulkarni.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/ramkulkarni.com\/blog\/wp-json\/wp\/v2\/comments?post=442"}],"version-history":[{"count":0,"href":"http:\/\/ramkulkarni.com\/blog\/wp-json\/wp\/v2\/posts\/442\/revisions"}],"wp:attachment":[{"href":"http:\/\/ramkulkarni.com\/blog\/wp-json\/wp\/v2\/media?parent=442"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/ramkulkarni.com\/blog\/wp-json\/wp\/v2\/categories?post=442"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/ramkulkarni.com\/blog\/wp-json\/wp\/v2\/tags?post=442"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}