Layout für Listendarstellung

  • Themenstarter Themenstarter THELayoutPROB
  • Beginndatum Beginndatum
T

THELayoutPROB

Gast
Hallo,

ich suche ein Layout (oder auch eine andere Möglichkeit) um Swing-Componenten
in Form einer Liste darzustellen. Nein die Option z.B. JList zu verwenden habe ich nicht.

Ich habe bisher versucht das Grid-Layout zu verwenden. Das Problem, ich füge immer
wieder neue Componenten der Liste hinzu und ich möchte allerdings, dass alle Elemente
immer eine feste größe haben (egal wie viele Elemente die "Liste" enthält) und ich
möchte, das wenn es mehr Elemente sind, als die größe (durch setPreferredSize()) zulassen
würde, eine ScrollPane aktiv wird.

Gibt es da eine Layout (oder eine sonstige) Lösung?
 
Da hab ich das Problem, dass alle Elemente die gleiche größe haben.
Das heißt, dass das erste Element das komplette panel belegt, füge ich
ein zweites hinzu, teilen sie sich die fläche, usw.
 
da musst du wohl noch die setMaximumSize(); bemühen.

KSKB:

Java:
public class KSKBBoxLayout {

	private JPanel boxPanel;
	private JFrame pFrame;

	public KSKBBoxLayout(JFrame pFrame) {
		this.pFrame = pFrame;
		boxPanel = new JPanel();
		boxPanel.setLayout(new BoxLayout(boxPanel, BoxLayout.Y_AXIS));

		JButton addPane = new JButton("Kalick");
		addPane.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent arg0) {
				boxPanel.add(createThePanel());
				KSKBBoxLayout.this.pFrame.validate();
			}

			private JPanel createThePanel() {
				JPanel pnl = new JPanel();
				int w = (int) (Math.random() * 50);
				int h = (int) (Math.random() * 60);
				pnl.setPreferredSize(new Dimension(w, h));
				pnl.setMaximumSize(new Dimension(w, h));
				pnl.setBackground(new Color((int) (Math.random() * 255),
						(int) (Math.random() * 255),
						(int) (Math.random() * 255)));
				return pnl;
			}
		});

		boxPanel.add(addPane);
		boxPanel.add(Box.createGlue());
	}

	public JPanel getBoxPanel() {
		return boxPanel;
	}

	public static void main(String[] args) {
		JFrame fr = new JFrame();
		fr.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

		KSKBBoxLayout bl = new KSKBBoxLayout(fr);

		fr.add(new JScrollPane(bl.getBoxPanel()));
		fr.pack();
		fr.setLocationRelativeTo(null);
		fr.setVisible(true);

	}

}

Gruß Vanny
 
Zuletzt bearbeitet von einem Moderator:

Zurück
Oben