Note that there are some explanatory texts on larger screens.

plurals
  1. POSwing GroupLayout: Resizing and limiting component sizes
    primarykey
    data
    text
    <p>I'm using <code>GroupLayout</code> to manage components in some dynamically generated data input forms. The layout is more or less like so:</p> <pre><code>*-----------------------------------------------* | label A | field A | | label B | field B | | label C | field C | *-----------------------------------------------* </code></pre> <p>I'm using 2 parallel groups for the horizontal layout, and a single sequential group for the vertical layout. For the most part, everything is working just fine.</p> <p>I want to limit the maximum width of the labels (which are just instances of <code>JLabel</code>) to 1/3 of the width of the parent <code>JFrame</code>. If the <code>JFrame</code> was a fixed size this would be trivial, but I have to deal with it being resized.</p> <p>I'm picking up <code>ComponentListener.componentResized()</code> events from the <code>JFrame</code> but I'm a bit stuck on what to do once I receive such an event.</p> <p>I've tried this approach without any luck:</p> <pre><code>public void componentResized(ComponentEvent e) { int maxW = parentFrame.getWidth() / 3; for (JLabel l : labels) { l.setMaximumSize( // have also tried setSize() and setPreferredSize() new Dimension( Math.min(l.getSize().width, maxW), l.getMaximumSize().height)); } groupLayout.invalidateLayout(getContentSpace()); } </code></pre> <p><b>Can anyone suggest a way to limit the width of the labels which will work?</b></p> <p>I could probably rebuild the layout from scratch every time, but I feel like there ought to be a simpler way.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    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