JTextArea - Zeichenbegrenzung nach .txt Datei import

Alublech

Mitglied
Hallo Leute

Ich habe ein Problem mit der Zeichenbegrenzung, nachdem ich den Inhalt aus einer .txt Datei importiert habe. Ohne den Import der Datei funktioniert die Zeichenbegrenzung einwandfrei, doch wenn ich den Befehl für den Import gemacht habe, ist die Zeichebgrenzung praktisch aufgehoben.

Texfeld:
Java:
JTextArea	tp = new JTextArea();
		tp.setLineWrap(true);
		tp.setBorder(BorderFactory.createEtchedBorder());
		tp.setDocument(new FieldSize(140));
		try {
            tp.read(new FileReader("d:/test.txt"), "");
        } catch (Exception e) {
            e.printStackTrace();
        }

FieldSize:
Java:
import javax.swing.text.AttributeSet;
import javax.swing.text.BadLocationException;
import javax.swing.text.PlainDocument;

public class FieldSize extends PlainDocument
{
private int max = 10;

public FieldSize(int max)
{
this.max = max;
}
 
@Override
public void insertString(int offs, String str, AttributeSet a)
throws BadLocationException
{
// check string being inserted does not exceed max length
 
if (getLength()+str.length()>max)
{
// If it does, then truncate it

str = str.substring(0, max - getLength());
}
super.insertString(offs, str, a);
}
}


mfg
Alublech
 
hier der Quellcode, der das aufklären dürfte:

Java:
    public void read(Reader in, Object desc) throws IOException {
        EditorKit kit = getUI().getEditorKit(this);
        Document doc = kit.createDefaultDocument();
        if (desc != null) {
            doc.putProperty(Document.StreamDescriptionProperty, desc);
        }
        try {
            kit.read(in, doc, 0);
            setDocument(doc);
        } catch (BadLocationException e) {
            throw new IOException(e.getMessage());
        }
    }
kannst du ja mehr oder weniger nachbauen und dann dein Document einbeziehen,
zumindest als ersten Versuch,
oder komplett manuell auf normalen Wege die Datei einlesen und normal den String setzen
 
das Direkteste wäre, die Codestellen genau dorthin zu kopieren wo dein Aufruf steht,
bei Aufrufen wie getUI() muss tp. davorgestellt werden,

nicht verwendeter Kram wie
> doc.putProperty(Document.StreamDescriptionProperty, desc);
kann offensichtlich vorerst wäg

Alternativen sind Untermethoden, die GUI-Klasse überschreiben usw., aber was soll ich da groß erzählen
 
Ich habe nach dem einfügen aber noch ein paar Probleme.
Das "read" hat einen fehler: " void is an invalid type for the variable read" und er sagt bei einem komma in der klammer , es sei ein Fehler. Also irgendwas mache ich falsch.

Java:
public void addComponents() {
		//Komponenten initialisieren
		Icon icon = new ImageIcon("send.gif");
		Icon icon2 = new ImageIcon("random.gif");
		Icon icon3 = new ImageIcon("logout.gif");
		Icon icon4 = new ImageIcon("happy.gif");
		Icon icon5 = new ImageIcon("angry.gif");
		Icon icon6 = new ImageIcon("tired.gif");
		Icon icon7 = new ImageIcon("frightened.gif");
		Icon icon8 = new ImageIcon("bad.gif");
		Icon icon9 = new ImageIcon("suprised.gif");
		Icon icon10 = new ImageIcon("tweets.gif");
		
    	TextArea 	tt = new TextArea();
		JTextArea	tp = new JTextArea();
		tp.setLineWrap(true);
		tp.setBorder(BorderFactory.createEtchedBorder());
		tp.setDocument(new FieldSize(140));
		try {
            tp.read(new FileReader("d:/test.txt"), "");
        } catch (Exception e) {
            e.printStackTrace();
        }

		
		JButton		button_logout = new JButton("Logout");
		button_logout.setIcon(icon3);
		button_logout.addActionListener(new ActionListener()
		{
			public void actionPerformed(ActionEvent e)
			{
				int eingabe = JOptionPane.showConfirmDialog(null,
                        "Wollen sie sich wirklich abmelden?",
                        "Abmelden?",
                        JOptionPane.YES_NO_OPTION);
				 if (eingabe == JOptionPane.YES_OPTION) {
			     	 dispose();
					 Login l = new Login();
			     		l.createAndShowUI();
				 }
			}
		});
		JButton		button_send = new JButton("Senden");
		button_send.setIcon(icon);
		JButton		button_random = new JButton("Vorschlag");
		button_random.setIcon(icon2);
		JButton		button_e_happy = new JButton("Fröhlich");
		button_e_happy.setIcon(icon4);
		JButton		button_e_angry = new JButton("Wütend");
		button_e_angry.setIcon(icon5);
		JButton		button_e_tired = new JButton("Müde");
		button_e_tired.setIcon(icon6);
		JButton		button_e_scared = new JButton("Ängstlich");
		button_e_scared.setIcon(icon7);
		JButton		button_e_bad = new JButton("Traurig");
		button_e_bad.setIcon(icon8);
		JButton		button_e_suprised = new JButton("Verblüfft");
		button_e_suprised.setIcon(icon9);
		JButton		button_e_tweets = new JButton("Anzeigen");
		button_e_tweets.setIcon(icon10);
		Label		uhrzeit = new Label("Tweets");
		Label		datum = new Label("Tweets");
		
		//Komponenten platzieren
		tp.setBounds(5,450,350,150);
		tt.setBounds(5,100,800,300);
		button_logout.setBounds(820,370,110,30);
		button_send.setBounds(5,610,110,30);
		button_random.setBounds(125,610,110,30);
		button_e_happy.setBounds(570,450,110,30);
		button_e_angry.setBounds(690,450,110,30);
		button_e_tired.setBounds(570,490,110,30);
		button_e_scared.setBounds(690,490,110,30);
		button_e_bad.setBounds(690,530,110,30);
		button_e_suprised.setBounds(570,530,110,30);
		button_e_tweets.setBounds(820,330,110,30);
		
		//Komponenten sichtbar machen
		this.getContentPane().add(tt);
		this.getContentPane().add(tp);
		this.getContentPane().add(button_logout);
		this.getContentPane().add(button_send);
		this.getContentPane().add(button_random);
		this.getContentPane().add(button_e_happy);
		this.getContentPane().add(button_e_angry);
		this.getContentPane().add(button_e_tired);
		this.getContentPane().add(button_e_scared);
		this.getContentPane().add(button_e_bad);
		this.getContentPane().add(button_e_suprised);
		this.getContentPane().add(button_e_tweets);
		
		public void read(Reader in, Object desc) throws IOException {
	        EditorKit kit = tp.getUI().getEditorKit(this);
	        Document doc = kit.createDefaultDocument();
	        if (desc != null) {
	            doc.putProperty(Document.StreamDescriptionProperty, desc);
	        }
	        try {
	            kit.read(in, doc, 0);
	            setDocument(doc);
	        } catch (BadLocationException e) {
	            throw new IOException(e.getMessage());
	        }
 
eine Methode darf nicht innerhalb einer anderen Methode definiert werden (falls nicht in der Form anonymer Listener),
absolute Grundlage von aller ersten Code-Beispielen, Monate bis Jahrzehnte, bevor man je von komplizierten GUI-Komponenten hört
 

Neue Themen


Zurück
Oben