Umbrechendes Flowlayout

Status
Nicht offen für weitere Antworten.

rgubler

Mitglied
Hallo,

ich suche einen Layoutmanager der ein Flowlayout mehrspaltig macht wenn das Fenster zu schmal
für alle Komonenten ist. Gibt es so etwas?

Gruß Rüdiger
 

L-ectron-X

Gesperrter Benutzer
Ich verstehe nicht so ganz, was du meinst, kannst du das vielleicht an Hand eines Beispiels/Bildes präzisieren?
 

rgubler

Mitglied
Ich habe eine Lösung gefunden:

Code:
import java.awt.Component;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.LayoutManager;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;

public class WrapLayout implements LayoutManager
{
  protected int _hgap;
  protected int _vgap;

  public WrapLayout()
  {
    this(8, 8);
  }

  public WrapLayout(final int hgap, final int vgap)
  {
    _hgap = hgap;
    _vgap = vgap;
  }

  public void addLayoutComponent(final String name, final Component c)
  {
  }

  public void layoutContainer(final Container parent)
  {
    final int width = parent.getWidth();
    int x = 0;
    int y = 0;
    int current_height = 0;
    for (final Component c : parent.getComponents())
    {
      final Dimension d = c.getPreferredSize();
      if (_hgap + x + d.width + _hgap > width && x > 0)
      {
        y += current_height + _vgap;
        x = 0;
        current_height = 0;
      }
      if (d.height > current_height)
      {
        current_height = d.height;
      }
      c.setBounds(_hgap + x, _vgap + y, d.width, d.height);
      x += d.width + _hgap;
    }
  }

  public Dimension minimumLayoutSize(final Container parent)
  {
    final Dimension result = new Dimension();
    result.width = 0;
    result.height = 0;
    for (final Component c : parent.getComponents())
    {
      final Dimension d = c.getPreferredSize();
      if (d.width > result.width)
      {
        result.width = d.width;
      }
      if (d.height > result.height)
      {
        result.height = d.height;
      }
    }
    result.width = result.width + _hgap * 2;
    result.height = result.height + _vgap * 2;
    return result;
  }

  public Dimension preferredLayoutSize(final Container parent)
  {
    final Dimension result = new Dimension();
    result.width = parent.getWidth();
    result.height = _vgap;
    int x = 0;
    int current_height = 0;
    for (final Component c : parent.getComponents())
    {
      final Dimension d = c.getPreferredSize();
      if (_hgap + x + d.width + _hgap > result.width && x > 0)
      {
        result.height += current_height + _vgap;
        x = 0;
        current_height = 0;
      }
      if (d.height > current_height)
      {
        current_height = d.height;
      }
      x += d.width + _hgap;
    }
    if (current_height > 0)
    {
      result.height += current_height + _vgap;
    }
    result.width += _hgap;
    return result;
  }

  public void removeLayoutComponent(final Component comp)
  {
  }

  public int getHgap()
  {
    return _hgap;
  }

  public int getVgap()
  {
    return _vgap;
  }

  public void setHgap(final int hgap)
  {
    _hgap = hgap;
  }

  public void setVgap(final int vgap)
  {
    _vgap = vgap;
  }

  public static void main(final String[] args)
  {
    final JFrame frame = new JFrame("WrapDemo");

    final JPanel wrapPanel = new JPanel(new WrapLayout());
    wrapPanel.add(new JButton("01"));
    wrapPanel.add(new JButton("02"));
    wrapPanel.add(new JButton("02"));
    wrapPanel.add(new JButton("03"));
    wrapPanel.add(new JButton("04"));
    wrapPanel.add(new JButton("05"));
    wrapPanel.add(new JButton("06"));
    wrapPanel.add(new JButton("07"));
    wrapPanel.add(new JButton("08"));
    wrapPanel.add(new JButton("09"));
    wrapPanel.add(new JButton("10"));

    frame.getContentPane().add(wrapPanel);

    frame.pack();
    frame.setVisible(true);

    frame.addWindowListener(new WindowAdapter()
    {
      @Override
      public void windowClosed(final WindowEvent e)
      {
        super.windowClosed(e);
        System.exit(0);
      }

    });
  }

}

Gruß Rüdiger
 
Status
Nicht offen für weitere Antworten.
Ähnliche Java Themen
  Titel Forum Antworten Datum
frager2345 JTextLabel im Flowlayout verschieben AWT, Swing, JavaFX & SWT 4
A hilfe für flowlayout AWT, Swing, JavaFX & SWT 6
T Swing Größe einer Komponente in einem BoxLayout / FlowLayout festlegen AWT, Swing, JavaFX & SWT 7
P LayoutManager TextField.setSize(); in einem FlowLayout AWT, Swing, JavaFX & SWT 3
I BoxLayout, FlowLayout und Komponentengrößen AWT, Swing, JavaFX & SWT 1
R LayoutManager JSeparator in FlowLayout AWT, Swing, JavaFX & SWT 4
M Problem mit FlowLayout, JLabel und JButton AWT, Swing, JavaFX & SWT 4
B FlowLayout Buttons verschieben? AWT, Swing, JavaFX & SWT 13
B Swing JScrollBar in einem JPanel mit FlowLayout AWT, Swing, JavaFX & SWT 15
M FlowLayout Alignment?! AWT, Swing, JavaFX & SWT 9
F AlignmentY bei FlowLayout AWT, Swing, JavaFX & SWT 8
P LayoutManager Komponenten einfügen (FlowLayout, BoxLayout) AWT, Swing, JavaFX & SWT 2
D Komponente im FlowLayout nicht mittig sondern rechts. Wie? AWT, Swing, JavaFX & SWT 4
C JPanel mit FlowLayout in JScrollPane - keinen Scrollbalken AWT, Swing, JavaFX & SWT 2
E LayoutManager FlowLayout mit angepassetr PreferedSize? AWT, Swing, JavaFX & SWT 3
aze FlowLayout:Lücken "schließen" AWT, Swing, JavaFX & SWT 2
O FlowLayout Button Event zuweisen? AWT, Swing, JavaFX & SWT 7
R FlowLayout: 1 Objekt pro Zeile AWT, Swing, JavaFX & SWT 5
N JTable in FlowLayout verweigert änderungen AWT, Swing, JavaFX & SWT 3
hdi FlowLayout Alternative AWT, Swing, JavaFX & SWT 10
0 Vertikales FlowLayout ? AWT, Swing, JavaFX & SWT 4
M Problem mit jPanel (FlowLayout) in ScrollPane AWT, Swing, JavaFX & SWT 4
E Mysthische Punkte auf JFrame(FlowLayout) nach Komboboxklick AWT, Swing, JavaFX & SWT 5
D Toolbars und Zeilenumbruch (benutze FlowLayout) AWT, Swing, JavaFX & SWT 2
I Vertical FlowLayout AWT, Swing, JavaFX & SWT 1
G position im FlowLayout AWT, Swing, JavaFX & SWT 11
O Zeilenumbruch erzwingen (FlowLayout? AWT, Swing, JavaFX & SWT 1
K FlowLayout AWT, Swing, JavaFX & SWT 2
F FlowLayout in BorderLayout AWT, Swing, JavaFX & SWT 4
B FlowLayout ausrichtung | "Wasserzeichen" im JFrame AWT, Swing, JavaFX & SWT 8
R FlowLayout-Problem AWT, Swing, JavaFX & SWT 17
L LayoutManager ähnlich wie FlowLayout AWT, Swing, JavaFX & SWT 5
N Problem mit JScrollPane und JPane(FlowLayout) AWT, Swing, JavaFX & SWT 4
S toolbar - flowlayout problem AWT, Swing, JavaFX & SWT 5
M FlowLayout in BorderLayout einbinden? AWT, Swing, JavaFX & SWT 6

Ähnliche Java Themen


Oben