Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I investigated your problem and as far as I saw there a multiple <a href="http://code.google.com/p/verticalflowlayout/">VerticalFlowLayouts</a> which aren't wrappable and there also are <a href="http://tips4java.wordpress.com/2008/11/06/wrap-layout/">wrappableFlowLayouts</a>, which aren't vertical.</p> <p>Instead of putting those together (what smart people would have done) I wrote a poor workaround based on answer one for you. It gets the job done, but sadly it turned out to be not nearly as smooth/reliable as I wanted. But I thought I post it anyway.</p> <pre><code>public class CustomFrame extends JFrame { private int borderWidth = 10; private int labelCounter = 0; private Box box; private JPanel pane; private List&lt;JLabel&gt; registeredLabels = new ArrayList&lt;JLabel&gt;(); public CustomFrame() { super("Custom JFrame"); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); pane = new JPanel(); pane.setLayout(new BoxLayout(pane, BoxLayout.X_AXIS)); JScrollPane scrollPane = new JScrollPane(pane); scrollPane.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS); scrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_NEVER); add(scrollPane); setSize(200, 130); // this just calls the method reAddAllLabels() upon resizing this.addComponentListener(new ComponentListener() { @Override public void componentShown(ComponentEvent e) { // ignore } @Override public void componentResized(ComponentEvent e) { reAddAllLabels(); repaint(); } @Override public void componentMoved(ComponentEvent e) { // ignore } @Override public void componentHidden(ComponentEvent e) { // ignore } }); setVisible(true); } /** * Build the Label, register the Label in the Label-List, add the Label through further method * * @param text * for the new Label */ public void addNewLabel(String text) { JLabel myJLabel = new JLabel(text); registeredLabels.add(myJLabel); addLabel(myJLabel); } /** * Reset stuff, add all registered Labels * */ private void reAddAllLabels() { labelCounter = 0; pane.removeAll(); if (registeredLabels.size() &gt; 0) { for (JLabel label : registeredLabels) { addLabel(label); } } } /** * Calculate max-Labels per Column, eventually create new Box, add Label to box * * @param label */ private void addLabel(JLabel label) { int maxLabels = (pane.getHeight() - borderWidth * 2) / label.getPreferredSize().height; if (labelCounter % maxLabels == 0) { box = Box.createVerticalBox(); box.setBorder(BorderFactory.createEmptyBorder(borderWidth, borderWidth, borderWidth, borderWidth)); } box.add(label); pane.add(box); labelCounter++; } /** * How to use the frame * * @param args */ public static void main(String[] args) { SwingUtilities.invokeLater(new Runnable() { @Override public void run() { CustomFrame customFrame = new CustomFrame(); for (int i = 1; i &lt;= 20; i++) { customFrame.addNewLabel("Label " + i); } } }); } } </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. 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