FormLayout

Status
Nicht offen für weitere Antworten.
G

Guest

Gast
Hallo zusammen,

ich hab ein Problem mit meinem FormLayout in meinem E-mail Client.
Und zwar will ich, dass die TextFields das fenster komplett in der Breite füllen (das funktioniert ja auch). Jedoch soll das AttatchmentPanel umbrechen, wenn die Anhänge in der breit nichtmehr drauf passen(deshalb das FlowLayout an der stelle)

Wie kann ich das schaffen???

Danke im voraus!

Code:
package newClient.gui;

import java.awt.Dimension;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextArea;
import javax.swing.JTextField;

import com.jgoodies.forms.builder.PanelBuilder;
import com.jgoodies.forms.layout.CellConstraints;
import com.jgoodies.forms.layout.FormLayout;

public class NewMail extends JFrame {

	public NewMail(){
		this.setTitle("Neue E-Mail");
		this.layoutComponents();
		this.setPreferredSize(new Dimension(800,600));
		this.pack();
		this.setLocationRelativeTo(null);
		this.setVisible(true);
	}
	
	private void layoutComponents(){
		PanelBuilder builder = new PanelBuilder(new FormLayout("2dlu,p,4dlu,fill:p:grow,2dlu","2dlu,p,4dlu,p,4dlu,p,4dlu,p,6dlu,fill:p:grow,2dlu"));
		
		CellConstraints contraints = new CellConstraints();
		
		builder.add(new JButton("Mail senden"), contraints.xy(2, 2));
		builder.add(new JLabel("Empfänger:"), contraints.xy(2, 4));
		builder.add(new JLabel("Betreff:"), contraints.xy(2, 6));
		builder.add(new JLabel("Anhänge:"), contraints.xy(2, 8));
		
		builder.add(new JTextField(), contraints.xy(4, 4));
		builder.add(new JTextField(), contraints.xy(4, 6));
		builder.add(new AttatchmentPanel(false), contraints.xy(4,8));
		
		builder.add(new JTextArea(), contraints.xyw(2, 10, 3));
		this.getContentPane().add(builder.getPanel());
	}
}
Code:
package newClient.gui;

import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;

import javax.swing.Icon;
import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
import javax.swing.filechooser.FileSystemView;

public class AttatchmentPanel extends JPanel {

	private boolean isReadOnly = false;
	private JPanel attatchmentsPanel = new JPanel(new FlowLayout(FlowLayout.LEFT));
	
	public AttatchmentPanel(boolean readOnly){
		this.isReadOnly = readOnly;
		layoutComponets();
	}
	
	private void layoutComponets(){
		this.setLayout(new BorderLayout());
		this.add(this.attatchmentsPanel, BorderLayout.CENTER);
		if (!isReadOnly) {
			JButton addButton = new JButton("Anhang hinzufügen");
			this.add(addButton, BorderLayout.WEST);
			addButton.addActionListener(new ActionListener(){
				public void actionPerformed(ActionEvent e) {
					addAttatchment();
				}
			});
		}
	}
	
	private void addAttatchment(){
		JFileChooser fileChooser = new JFileChooser();
		int returnVal = fileChooser.showOpenDialog(this);
		if(returnVal == JFileChooser.APPROVE_OPTION){
			fileChooser.getSelectedFile().getAbsolutePath();
			this.attatchmentsPanel.add(new JLabel(fileChooser.getSelectedFile().getName(), getIcon(fileChooser.getSelectedFile().getAbsolutePath()), JLabel.LEFT));
			
			SwingUtilities.invokeLater(new Runnable(){
				public void run() {
					AttatchmentPanel.this.attatchmentsPanel.revalidate();
				}
			});
		}
	}
	
	private Icon getIcon(String path){
		Icon icon;
		FileSystemView systemview = FileSystemView.getFileSystemView();
		File file = new File(path);
	    icon = systemview.getSystemIcon(file);
	    return icon;
	  }
}
 

Marco13

Top Contributor
Hab' zwar das FormLayout nicht, aber du kannst mal schauen, ob das mit dem Umbruch funktioniert, wenn du das "FlowLayout mit Zeilenumbruch" ausprobierst, das Hobbit_Im_Blutrausch mal irgendwann in den Codeschnipseln gepostet hatte.
 
Status
Nicht offen für weitere Antworten.

Ähnliche Java Themen

Neue Themen


Oben