JEditorPane in .txt exportieren

Ich habe einen JEditorPane in dem ich Text strukturiert darstelle.

Dieser Text soll zusammen mit anderen Textteilen als .txt Datei gespeichert werden.

Leider werden nicht alle Tabulatoren korrekt dargestellt.

Hier ein Screenshot:

Hier der Code:

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.StringReader;
import java.util.Scanner;

import javax.swing.JButton;
import javax.swing.JEditorPane;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.text.BadLocationException;
import javax.swing.text.Document;
import javax.swing.text.html.HTMLDocument;
import javax.swing.text.html.HTMLEditorKit;

public class TestExport extends JFrame{

	private JEditorPane tacomment1;	
	private JScrollPane spcomment1;
	private JPanel pcomment1;
	
	private JButton btnadd;
	
	public TestExport(){	
		
		Container content = getContentPane();
		content.setLayout(new BorderLayout());
		
		tacomment1 = new JEditorPane();
		
		tacomment1.setContentType("text/plain");
		tacomment1.setEditorKit(new HTMLEditorKit());
		tacomment1.setEditable(true);
		
		Font font = new Font("Arial", Font.PLAIN, 12);
		String bodyRule = "body { font-family: " + font.getFamily() + "; " +
                "font-size: " + font.getSize() + "pt; }";
        ((HTMLDocument)tacomment1.getDocument()).getStyleSheet().addRule(bodyRule);
		
		tacomment1.setMinimumSize(new Dimension(600,400));		
		tacomment1.setPreferredSize(new Dimension(600,400));

		spcomment1 = new JScrollPane();
		spcomment1.setMinimumSize(new Dimension(600,400));
		spcomment1.setPreferredSize(new Dimension(600,400));
		
		spcomment1.getViewport().setBackground(Color.WHITE);		
		spcomment1.getViewport().add(tacomment1);
		
		pcomment1 = new JPanel();		
		pcomment1.add(spcomment1);

		btnadd = new JButton("Content exportieren");
		btnadd.addActionListener(new ActionListener(){
				public void actionPerformed(ActionEvent ae){

					String text = "";
		
			        try
		            {
			        	
		                Document doc1 = tacomment1.getDocument();
		                text = doc1.getText(0, doc1.getLength());
		                
		            }
		            catch (BadLocationException e)
		            {
		                e.printStackTrace();
		            }
					
			
					
					//------------------
					
					String lastname = "Fa. Meier";
					String firstname = "";
					String street = "Hauptstraße";
					String number = "19a";
					String postalcode = "80331";
					String city = "München";
					String phone = "089 858585";
					String mobile = "0157 8585858";
					String email = "meier@test.de";
					String website = "www.testmeier.de";

				
					
				
						JFileChooser fcexport = new JFileChooser();
						fcexport.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES);
						fcexport.setCurrentDirectory(new File("."));
				    
						int retvalue = fcexport.showSaveDialog(null);
				        
				        if (retvalue == JFileChooser.APPROVE_OPTION){
				            String path = fcexport.getSelectedFile().getPath()+".txt";
				            
				            BufferedWriter bufferedwriter;

				    		String headerline = new String("-----------------------------------------------------------------------");
				    		
				    		StringBuilder layout = new StringBuilder();
				    		layout.append(headerline+"\n");
				    		layout.append("JEditorPaneExport\n");
				    		layout.append(headerline+"\n");
				    		layout.append("\n");
				    		layout.append("\n");
				      		layout.append("Nachname, Firma, Familie: " + lastname + "\n");
				    		layout.append("Vorname: " + firstname + "\n");
				    		layout.append("Straße: " + street + "\n");
				    		layout.append("Hausnummer: " + number + "\n");
				    		layout.append("PLZ: " + postalcode + "\n");
				    		layout.append("Stadt: " + city + "\n");
				    		layout.append("Telefon: " + phone + "\n");
				    		layout.append("Handy: " + mobile + "\n");
				    		layout.append("E-Mail: " + email + "\n");
				    		layout.append("Website: " + website + "\n");
				    		layout.append("Sonstiges: \n" + text + "\n");

				    		Scanner sc =  new Scanner(new StringReader(layout.toString()));
				    		
				            try{
				                bufferedwriter = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(path), "UTF-8"));
				            	String newline = System.getProperty("line.separator");
				                
				            	while(sc.hasNextLine()){
				            		bufferedwriter.write(sc.nextLine());
				            		bufferedwriter.write(newline);
				            		
				            	}
				            	
				                bufferedwriter.flush();
				                bufferedwriter.close(); 
				                
				            }

				            catch(IOException ioe){
				            	ioe.printStackTrace();
				    			
				            }
				            finally {
				            	sc.close();
				            }
				        }					
				}
		});
		
		btnadd.setPreferredSize(new Dimension(200,400));
		
		content.add(pcomment1, BorderLayout.WEST);	
		content.add(btnadd, BorderLayout.CENTER);
			
		pack();
		setLocationRelativeTo(null);
		
		addWindowListener(new WindowAdapter(){
		      public void windowClosing(WindowEvent e){
		    	  setVisible(false);
		    	  dispose();
		    	  
		      }
		});
	}
	
	public static void main(String[] args){
		
			JFrame frame = new TestExport();
			frame.setVisible(true);
			
	}	
}

So sieht die .txt Datei aus:

es wird alles korrekt exportiert, aber in dem jeditorpane verwendest du eine andere Schriftart als in dem Programm, das du zum Anzeigen der txt datei verwendest. Du solltest am Besten wohl auch im jeditorpane eine monospaced Schrift verwenden.

Vermutlich hat AmunRa schon recht. Ein weiterer Punkt ist, dass „tabulator“ eben nicht „so-und-so-viele Leerzeichen“ bedeutet. Man kann bei den meisten Programmen einstellen, wo die Tab-Stops sein sollen. (Beim Windows-Editor nicht - da scheint 8 fest eingestellt zu sein)

Danke.

1 Like