Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p><code>GridBagLayout</code> is the most powerful Layout that you can use to easily implement grid-like displays. It's a layout with <code>n</code> rows and <code>m</code> columns where each cell is customizable independently of the others in several aspects. In this layout you have to attach a <code>GridBagConstraints</code> object to each <code>panel.add(JComponent, Constraints)</code> as constraints. In the <a href="http://docs.oracle.com/javase/tutorial/uiswing/layout/gridbag.html" rel="nofollow noreferrer">tutorial</a> it's clearly specified what is customizable. It might look a little harsh at the beginning but once you get the hang of it it's great. It's powerful and flexible and you don't have to worry about the uncustomizable restrictions you might encounter with other Layouts.</p> <p>In your layout, the most inconvenient thing I see is having the "Account ID" label aligned by its center with the TextField <em>AND</em> with the empty space over both of them. It would be easier if the label was aligned with the bottom of the TextField. To solve this I'll assume that the Label and the TextField are inside a panel I constructed beforehand that aligns each other correctly (easily with BorderLayout or GridBagLayout... or anything really) and I'll just place the panels in the Layout.</p> <p>Then I see this Layout as a GridBagLayout with 3 rows and 2 columns that looks like this:</p> <p><img src="https://i.stack.imgur.com/LRuCt.png" alt="GridBagLayout rows and columns"></p> <p>This is an <em>overview</em> of how I'd put the constraints to specify each component in the layout. </p> <p><strong>Panel 1 (Account ID Label + TextField)</strong></p> <pre><code>gridx = 0 gridy = 0 weighty = 0.5 weightx = 0.5 anchor = PAGE_END fill = HORIZONTAL </code></pre> <p><strong>Panel 2 (Amount Label + TextField)</strong></p> <pre><code>gridx = 0 gridy = 1 weighty = 0.0 fill = HORIZONTAL </code></pre> <p><strong>Button</strong></p> <pre><code>gridx = 0 gridy = 2 anchor = PAGE_START weighty = 0.5 </code></pre> <p><strong>TextArea</strong></p> <pre><code>gridx = 1 gridy = 0 gridheight = 3 weightx = 0.5 fill = BOTH </code></pre> <p>There is a couple of details I overlooked but the core issues can be aproached with these constraints. The least obvious thing to learn about the <code>GridBagLayout</code> is how do weights work in complex cases, for example what happens when there are several different <code>weightx</code> values in the same column. Does it count the max? or the sum?...</p> <p>For the sake of discussion, you could avoid having those panels using an extra initial row with an invisible component with <code>weighty &gt; 0</code> and then having 2 columns: one for the <code>JLabel</code>s and the other one for the <code>JTextField</code>s, with the apropiate <code>anchor</code>s; the button would have <code>gridwith = 2</code>... but that's totally unnecesary, go for the two auxiliar panels.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload