Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>MiGLayout has a lot of appeal, but <a href="http://java.sun.com/docs/books/tutorial/uiswing/layout/box.html" rel="nofollow noreferrer"><code>BoxLayout</code></a> is an alternative.</p> <p><a href="https://i.stack.imgur.com/eWMMF.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/eWMMF.png" alt="image"></a></p> <pre><code>import java.awt.*; import java.util.List; import java.util.ArrayList; import javax.swing.*; public class BoxTest extends JPanel { private List&lt;JTextField&gt; fields = new ArrayList&lt;JTextField&gt;(); public BoxTest() { this.setLayout(new BoxLayout(this, BoxLayout.Y_AXIS)); this.add(createPane(3, "One ", Color.red)); this.add(createPane(3, "Two ", Color.green)); this.add(createPane(10, "Three ", Color.blue)); } private JPanel createPane(int n, String s, Color c) { JPanel outer = new JPanel(); outer.setLayout(new BoxLayout(outer, BoxLayout.Y_AXIS)); outer.setBorder(BorderFactory.createLineBorder(c, 2)); for (int i = 0; i &lt; n; i++) { JPanel inner = new JPanel(); inner.setLayout(new BoxLayout(inner, BoxLayout.X_AXIS)); JLabel label = new JLabel(s + i + ":", JLabel.RIGHT); label.setPreferredSize(new Dimension(80, 32)); inner.add(label); JTextField tf = new JTextField("Stackoverflow!", 32); inner.add(tf); fields.add(tf); outer.add(inner); } return outer; } private void display() { JFrame f = new JFrame(); f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); JScrollPane jsp = new JScrollPane(this, JScrollPane.VERTICAL_SCROLLBAR_ALWAYS, JScrollPane.HORIZONTAL_SCROLLBAR_NEVER); this.validate(); Dimension d = this.getPreferredSize(); d.height /= 2; jsp.getViewport().setPreferredSize(d); jsp.getVerticalScrollBar().setUnitIncrement( this.getPreferredSize().height / fields.size()); f.add(jsp); f.pack(); f.setVisible(true); } public static void main(String[] args) { EventQueue.invokeLater(new Runnable() { @Override public void run() { new BoxTest().display(); } }); } } </code></pre>
    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. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      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