Note that there are some explanatory texts on larger screens.

plurals
  1. POJava Swing GridBagLayout Component Positioning
    primarykey
    data
    text
    <p>I am using <code>Swing</code>'s <code>GridBagLayout</code> for the fist time and I have so far only added two components to a container, although I intend to add more below vertically. So far, the first component (a <code>JLabel</code>) is positioned correctly at <code>PAGE_START</code>, and I have remembered to set weight attributes for the components' corresponding <code>GridBagConstraints</code>. The second component (a <code>JTextField</code>) however is not positioning as I intended and it being centered in the container rather than moving up underneath the <code>JLabel</code>. I have attempted to use multiple anchor constants including <code>FIRST_LINE_START</code>, <code>PAGE_START</code>, <code>NORTH</code> &amp; <code>NORTHWEST</code> but so far nothing is working. </p> <p>And so, once again I call for the gifted coders of stackoverflow for help. Below is a snippet of the code and below that is an image of the problem graphically.</p> <pre><code> // Instantiate components and configure their corresponding GridBagConstraints attributes // refPlusType properties refPlusType = new JLabel("&lt;html&gt;&lt;h3&gt;"+"Reference"+" - "+"Job Type"+" &lt;/h3&gt;&lt;hr /&gt;&lt;/html&gt;"); refPlusTypeGC = new GridBagConstraints(); refPlusTypeGC.gridx = 0; // Grid position refPlusTypeGC.gridy = 0; refPlusTypeGC.gridwidth = 2; // Number of colums occupied by component refPlusTypeGC.insets = new Insets(5, 10, 5, 10); // Specifies margin refPlusTypeGC.weightx = 0.1; // Required for anchor to work. refPlusTypeGC.weighty = 0.1; // Required for anchor to work. refPlusTypeGC.anchor = GridBagConstraints.PAGE_START; // Position in container // addressLine1 properties addressLine1 = new JTextField(); addressLine1GC = new GridBagConstraints(); addressLine1GC.gridx = 0; addressLine1GC.gridy = 1; addressLine1GC.gridwidth = 2; addressLine1GC.insets = new Insets(0, 10, 0, 10); addressLine1GC.fill = GridBagConstraints.HORIZONTAL; // Specifies component fill Horizontal space addressLine1GC.weightx = 0.1; addressLine1GC.weighty = 0.1; addressLine1GC.anchor = GridBagConstraints.FIRST_LINE_START; // Add components to this HALLogisticsDetailsPanel this.add(refPlusType, refPlusTypeGC); this.add(addressLine1, addressLine1GC); </code></pre> <p>Image below;</p> <p><img src="https://i.stack.imgur.com/4stmB.png" alt="enter image description here"></p> <p>Thank you all for any help you can offer.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

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