FlowLayout Alternative

Status
Nicht offen für weitere Antworten.

hdi

Top Contributor
Hallo,

also erstmal:

Ich wollte das Thema "FlowLayout Alternative" nennen. Ich hab mich halb totgelacht dass ich nach über 5 Minuten einfach noch immer keinen Titel hatte, der nicht abgelehnt wurde. Jegliche Schreibweise von Flow, Layout oder Alternative, sogar sowas wie "Flo-w" etc wurde eiskalt abgelehnt!!!
Was is da los??? Das gibts nich ohne Witz ich konnte keinen auch nur annähernd sinnvollen Titel eingeben Oo

Zum Thema:

Das FlowLayout ist ja an sich ein simpel zu benutzendes Layout, weil man sich nicht wie zB beim BorderLayout darum kümmern muss, wo man etwas genau hinklatscht.

Leider bietet FlowLayout nur:

* ComponentOrientation.LEFT_TO_RIGHT
* ComponentOrientation.RIGHT_TO_LEFT

Für meine Anwendung grad bräuchte ich ein Layout, dass automatisch alles untereinander addet, nicht nebeneinander.

Gibt es irgendein Layout, dass genauso einfach ist wie FlowLayout aber eben alles untereinander addet?

Danke
 

André Uhres

Top Contributor
Versuch's mal mit ListLayout: ListLayout.jar (Quellcode im jar)
Benutzungsbeispiel:
Code:
jScrollPane1 = new JScrollPane();
jPanel1 = new JPanel(new ListLayout());
for (int i = 0; i < 4; i++) {
    JPanel p = new JPanel();
    p.add(new JLabel("Label " + (i + 1)));
    p.add(new JTextField("TextField " + (i + 1)));
    jPanel1.add(p);
}
jScrollPane1.setViewportView(jPanel1);
 

hdi

Top Contributor
Also ich hab mir jetz das FlowLayout geschnappt und es dahingehend geändert. Wer's nutzen will
(hab's noch etwas editiert):

Code:
package log;

import java.awt.Component;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.LayoutManager;

/**
 * This class provides a layout quite similar to Sun's FlowLayout. The
 * difference is that StackLayout realizes a vertical flow, meaning the
 * components are added top-to-bottom.
 */

public class StackLayout implements LayoutManager {

	private int hgap, vgap;

	/**
	 * Sets up a layout where components are added top-to-bottom, without any
	 * gaps to the container borders or between them.
	 */
	public StackLayout() {
		this(0, 0);
	}

	/**
	 * Sets up a layout where components are added top-to-bottom, separated from
	 * each other and their container's borders by specified values.
	 * 
	 * @param hgap
	 *            the horizontal gap of a component to its container's left and
	 *            right border.
	 * 
	 * @param vgap
	 *            the vertical gap between the components and their container's
	 *            top and bottom border.
	 */
	public StackLayout(int hgap, int vgap) {
		this.hgap = hgap;
		this.vgap = vgap;
	}

	/*
	 * Determines and returns the minimum size of the container, whereas the
	 * height is the sum of the minimum height of all components, and the width
	 * equals the minimum width of the widest component.
	 */
	@Override
	public Dimension minimumLayoutSize(Container target) {

		synchronized (target.getTreeLock()) {
			Dimension dim = new Dimension(0, 0);
			int numComponents = target.getComponentCount();
			for (int i = 0; i < numComponents; i++) {
				Component c = target.getComponent(i);
				if (c.isVisible()) {
					Dimension cDim = c.getMinimumSize();
					dim.width = Math.max(cDim.width, dim.width);
					dim.width += hgap;
					dim.height += cDim.height;
					dim.height += vgap;
				}
			}
			dim.height += vgap;
			return dim;
		}
	}

	/*
	 * Determines and returns the preferred size of the container, whereas the
	 * height is the sum of the preferred height of all components, and the
	 * width equals the preferred width of the widest component.
	 */
	@Override
	public Dimension preferredLayoutSize(Container target) {

		synchronized (target.getTreeLock()) {
			Dimension dim = new Dimension(0, 0);
			int numComponents = target.getComponentCount();
			for (int i = 0; i < numComponents; i++) {
				Component c = target.getComponent(i);
				if (c.isVisible()) {
					Dimension cDim = c.getPreferredSize();
					dim.width = Math.max(cDim.width, dim.width);
					dim.width += hgap;
					dim.height += cDim.height;
					dim.height += vgap;
				}
			}
			dim.height += vgap;
			return dim;
		}
	}

	/*
	 * Constructs the container by vertically adding all its components from
	 * top-to-bottom, regarding possibly set gaps.
	 */
	@Override
	public void layoutContainer(Container target) {
		synchronized (target.getTreeLock()) {
			int nmembers = target.getComponentCount();
			int y = 0;
			for (int i = 0; i < nmembers; i++) {
				Component m = target.getComponent(i);
				if (m.isVisible()) {
					Dimension d = m.getPreferredSize();
					m.setSize(d.width, d.height);
					y += vgap;
					m.setLocation(hgap, y);
					y += d.height;
				}
			}
		}
	}

	public void removeLayoutComponent(Component c) {
	}

	public void addLayoutComponent(String s, Component c) {
	}
}

Achtung!
Ich bin eher ein Java Anfänger und habe mir diese Klasse gebaut nur an hand des Source Codes vom FlowLayout.
Ich meine natürlich verstehe ich alles was ich gecodet habe, war jetzt nicht copy&paste oder so ;) ich hab aber einige Dinge weggelassen die FlwoLayout macht weil ich den Sinn nicht begriffen habe. Bei mir funzt alles aber ich garantiere nicht dafür dass diese Klasse jetzt zu 100% "sauber" ist oder in allen Fällen ordentlich funktioniert.
 

André Uhres

Top Contributor
VerticalBox unterstützt kein hgap, vgap, preferredSize. VFlowLayout unterstützt kein hgap, ansonsten macht es dasselbe wie ListLayout,
ausser daß bei VFlowLayout-vgap kein Abstand vom Rand entsteht.
 
B

Beni

Gast
L-ectron-X hat gesagt.:
hdi hat gesagt.:
Das ist ein Fehler im Anti-Spam-Skript, der z.Z. nicht behoben werden kann.
Ich kümmere mich mal darum. Ob ich was erreichen kann, werden wir sehen.
Ah, ach deshalb habe ich gerade ne halbe Stunde lang Spambeiträge gelöscht. Das Skript ist nicht mehr scharf genug :bae:
 

L-ectron-X

Gesperrter Benutzer
Du warst nicht allein, ich hab dir geholfen. ;)
Das Problem ist, dass ich keinen FTP-Zugriff mehr habe, um das Skript anzupassen. Ging mit dem Betreiberwechsel verloren...
 
B

Beni

Gast
Danke, fürs helfen und fürs skripten :wink:
[Edit: wie jetzt, du kommst gar nicht an das Skript? Das ist natürlich dumm.]
 

hdi

Top Contributor
also ich hab jetz auch hgap eingebaut, im übrigen ist es eben nicht das selbe wie listLayout, denn was mich an ListLayout so sehr gestört hat, war dass man die Komponenten an Strings binden muss:

Code:
add(new JPanel(), "schwachsinnigerStringDenEhKeinerBrauchtAberDerMussHierStehen....");

Das war nich so cool ;) Mein Layout (habs jetzt StackLayout genannt, ist irgendwie sinniger) ist eben dahingehend wie das FlowLayout, man kann einfach hintereinander adden:
Code:
add(new JPanel());
add(new JLabel());
...
 

André Uhres

Top Contributor
ListLayout, ListLayout2

The ListLayout class is a layout manager
that lays out a container's components as a list.
The size of each component is set to its preferredSize.
The container is filled top-down with the components.


The ListLayout2 class is a layout manager
that lays out a container's components as a dynamic list with two columns
(or only one column, if there is not enough space for two columns).
The size of each component is set to its preferredSize, except
for the rightmost column, whose width will take all available space.
The container is filled top-down with rows, each row being filled from left
to right with the components.


Download: ListLayout

>was mich an ListLayout so sehr gestört hat, war dass man die Komponenten an Strings binden muss
Der kleine Bug ist jetzt raus. ListLayout unterstützt übrigens auch noch die Insets.


Code:
package demo;
/*
* ListLayoutDemo.java
*/
import java.awt.*;
import javax.swing.*;
import javax.swing.border.*;
import tf.net.andreuhres.swing.ui.layout.*;
public class ListLayoutDemo extends JFrame {
    public ListLayoutDemo() {
        setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        setSize(400, 500); 
        setLocationRelativeTo(null);
        setLayout(new GridLayout(0, 1));
        add(createExample(new ListLayout(), "ListLayout demo"));
        add(createExample(new ListLayout2(), "ListLayout2 demo"));
    }
    private Container createExample(final LayoutManager layout, final String title) {
        JScrollPane jScrollPane = new JScrollPane();
        JPanel jPanel1 = new JPanel(layout); 
        jPanel1.setBorder(BorderFactory.createTitledBorder(null, title,
                TitledBorder.LEFT, TitledBorder.BELOW_TOP,
                jPanel1.getFont().deriveFont(22f), Color.BLUE));
        for (int i = 0; i < 4; i++) {
            JPanel p = new JPanel();
            p.setBackground(Color.ORANGE);
            p.add(new JLabel("Label " + (i + 1)));
            p.add(new JTextField("TextField " + (i + 1)));
            jPanel1.add(p);
        }
        jScrollPane.setViewportView(jPanel1);
        return jScrollPane;
    }
    public static void main(final String[] args){
        Runnable gui = new Runnable(){
            public void run(){
                new ListLayoutDemo().setVisible(true);
            }
        };
        //GUI must start on EventDispatchThread:
        SwingUtilities.invokeLater(gui);
    }
}
 
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
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
rgubler Umbrechendes Flowlayout AWT, Swing, JavaFX & SWT 2
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
S Alternative JavaFX TableView AWT, Swing, JavaFX & SWT 1
Aruetiise Schnellere Alternative zu awt.robot AWT, Swing, JavaFX & SWT 3
A JavaFX Webview Alternative AWT, Swing, JavaFX & SWT 2
M SWT Alternative zu AWT/Swing? AWT, Swing, JavaFX & SWT 6
C AWT-Alternative gesucht - Problem mit AWT+JDialogPane AWT, Swing, JavaFX & SWT 8
M Arbeiten mit actionPerformed(ActionEvent) oder Alternative AWT, Swing, JavaFX & SWT 7
A 2D-Grafik Repaint - Alternative? AWT, Swing, JavaFX & SWT 3
E Swing JTextArea Alternative? AWT, Swing, JavaFX & SWT 5
K Alternative zu Applet mit HTML-Einbindung AWT, Swing, JavaFX & SWT 11
C Alternative zu getFontMetrics um Stringlänge zu berechnen AWT, Swing, JavaFX & SWT 2
G Swing Alternative Fensterdekoration? AWT, Swing, JavaFX & SWT 7
J Alternative für SwingUtilities Schachteln AWT, Swing, JavaFX & SWT 2
GilbertGrape Alternative zu TableRowSorter in 1.5 AWT, Swing, JavaFX & SWT 3
E JList tabellarisch aufbauen oder alternative? AWT, Swing, JavaFX & SWT 5
G Alternative zu JFileChooser AWT, Swing, JavaFX & SWT 4
B alternative zu setVisible() AWT, Swing, JavaFX & SWT 2
S Alternative zu paint in JApplet AWT, Swing, JavaFX & SWT 3
B Eine Alternative zur Steuerung einer ProgressBar? AWT, Swing, JavaFX & SWT 5
C Alternative GUI zu Swing AWT, Swing, JavaFX & SWT 5
W Alternative zu JFrame? AWT, Swing, JavaFX & SWT 14
L Hilfebutton in Titelleiste oder gute Alternative AWT, Swing, JavaFX & SWT 2
G Alternative zum JColorChooser AWT, Swing, JavaFX & SWT 4

Ähnliche Java Themen

Neue Themen


Oben