Swing Automatischer Zeilenumbruch bei JTextPanes

Sinus

Aktives Mitglied
Hallo zusammen,

wie kann ich einen automatischen Zeilenumbruch bei einer JTextPane erzwingen, wenn ich die
Framegröße horizontal ändere bzw. verkleinere?

Einfaches, kleines Beispiel:

Java:
	import java.awt.*;
	import javax.swing.*;
	import javax.swing.text.*;
	import static javax.swing.ScrollPaneConstants.*;	

	public class LineWrapTest {
		
		private JFrame frame = new JFrame();
		private String text ="test 1, test 2, test 3, test 4, test 5, test 6\n";
		private Document doc = new DefaultStyledDocument();
		private JTextPane textPane = new JTextPane();
		private JScrollPane scrollPane = null;
		private JPanel pane = new JPanel();		
		
		LineWrapTest() throws BadLocationException {	
			//generate some text
			for(int i=0;i<1000;i++) {
				doc.insertString(doc.getLength(), text, null);
			}
			textPane.setDocument(doc);						
			//make textpane scrollable
			pane.add(textPane);			
			scrollPane = new JScrollPane(pane);
			scrollPane.setHorizontalScrollBarPolicy(HORIZONTAL_SCROLLBAR_NEVER);
			createAndShowFrame();
		}		
		private void createAndShowFrame() {
			frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
			frame.setSize(new Dimension(200,200));
			frame.add(scrollPane, BorderLayout.CENTER);
			frame.setVisible(true);
		}		
		public static void main (String [] args) throws BadLocationException {
			new LineWrapTest();
		}}

Vielen Dank im Voraus!
 

Zurück
Oben