TextEditor

Hallo, ich hab folgenden Quellcode bekommen, der einen TextEditor darstellt. Wie kann ich einen StringText in diesen Editor setzen, sodass er als normaler Text editierbar sich in dem Fenster befindet:
Der Quellcode:

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.text.*;
import java.io.*;

/**
*
*/
public class EditorExample
extends JPanel
{
private Document doc;
private JEditorPane pane;
private JLabel statusInfo;

public EditorExample() {
setLayout(new BorderLayout());
add(createMenuBar(), BorderLayout.NORTH);

JPanel panel = new JPanel();
panel.setLayout(new BorderLayout());
statusInfo = new JLabel(" ");
panel.add(statusInfo, BorderLayout.SOUTH);

// Setup Text Pane
doc = new PlainDocument();
pane = new JEditorPane();
pane.setDocument(doc);
panel.add(new JScrollPane(pane), BorderLayout.CENTER);
add(panel, BorderLayout.CENTER);
}

private JMenuBar createMenuBar() {
JMenuBar menuBar = new JMenuBar();

// DATEI Menu
JMenu file = new JMenu(„Datei“);

JMenuItem item = new JMenuItem („Neu“);
file.add(item);
item.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
doNewCommand();
}
});

file.add (item = new JMenuItem („Öffne“));
item.addActionListener (new ActionListener() {
public void actionPerformed (ActionEvent e) {
doOpenCommand();
}
});

file.add (item = new JMenuItem („Load Text“));
item.addActionListener (new ActionListener() {
public void actionPerformed (ActionEvent e) {
doLoadCommand();
}
});

file.add (item = new JMenuItem („Speichern“));
item.addActionListener (new ActionListener() {
public void actionPerformed (ActionEvent e) {
doSaveCommand();
}
});

file.addSeparator();
file.add (item = new JMenuItem („Ende“));
item.addActionListener (new ActionListener() {
public void actionPerformed (ActionEvent e) {
doCloseCommand();
}
});
menuBar.add(file);

JMenu edit = new JMenu(„Bearbeiten“);
edit.add(item = new JMenuItem („Ausschneiden“));
item.addActionListener(new ActionListener() {
public void actionPerformed (ActionEvent e) {
// doCutCommand();
}
});
menuBar.add(edit);

return menuBar;
}

public void doNewCommand() {
doc = new PlainDocument();
pane.setDocument(doc);
validate();
}

public void doCloseCommand() {
System.exit (0);
}

public void doOpenCommand() {
try {
FileInputStream fis = new FileInputStream („doc.ser“);
ObjectInputStream ois = new ObjectInputStream (fis);
doc = (PlainDocument)ois.readObject();
ois.close();
pane.setDocument (doc);
validate();
statusInfo.setText („Reloaded from disk“);
} catch (Exception e) {
statusInfo.setText („Unable to reload“);
e.printStackTrace();
}
}

public void doSaveCommand() {
try {
FileOutputStream fos = new FileOutputStream („doc.ser“);
ObjectOutputStream oos = new ObjectOutputStream (fos);
oos.writeObject (doc);
oos.flush();
oos.close();
statusInfo.setText („Saved to disk“);
} catch (IOException e) {
statusInfo.setText („Unable to save“);
e.printStackTrace();
}
}

public void doLoadCommand() {
String msg;
FileDialog file = new FileDialog (
new Frame(), „Load File“, FileDialog.LOAD);
file.setFile ("*.java"); // Set initial filename filter
file.show(); // Blocks
String curFile;
if ((curFile = file.getFile()) != null) {
String filename = file.getDirectory() + curFile;
char[] data;
setCursor (Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR));
File f = new File (filename);
try {
doc = new PlainDocument();
pane.setDocument (doc);
// Read in text file
FileReader fin = new FileReader (f);
BufferedReader br = new BufferedReader (fin);
char buffer[] = new char[4096];
int len;
while ((len = br.read (buffer, 0, buffer.length)) != -1) {
// Insert into pane
doc.insertString(doc.getLength(),
new String (buffer, 0, len), null);
}
statusInfo.setText ("Loaded: " + filename);
} catch (BadLocationException exc) {
statusInfo.setText ("Error loading: " + filename);
} catch (FileNotFoundException exc) {
statusInfo.setText ("File Not Found: " + filename);
} catch (IOException exc) {
statusInfo.setText ("IOException: " + filename);
}
setCursor (Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR));
}
}

public static void main (String args[]) {
JFrame frame = new JFrame(„Beispiel für einen Editor“);
frame.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
frame.addComponentListener(new ComponentAdapter() {
public void componentResized(ComponentEvent e) {
System.out.println("componentResized: " + e);
}
});

Container contentPane = frame.getContentPane();
contentPane.setLayout(new BorderLayout());
//contentPane.add(new LAFPanel(frame,false), BorderLayout.SOUTH);

EditorExample edit = new EditorExample();
contentPane.add(edit, BorderLayout.CENTER);

frame.setSize(600,400);
frame.setVisible(true);
}

}

Vielen Dank für eine Antwort
Gruß
Manfred

Hi,
es füge einfach unter
pane.setDocument(doc);

die Zeile:

pane.setText(„dein Text“);

ein.

Wenn ich dich net richtig verstanden hab. Beschreib dein Vorhaben doch bitte noch mal genauer :smiley:

grüße

ema

Hi,
vielen Dank für die Antwort, aber leider mekert der Compiler, wenn ich es so mache. Er will in pane.setDocument ein javax.swing.Text.Document haben und akzeptiert den String nicht.

Um mein Vorhaben noch genauer zu beschreiben, ich möchte einen Editor programmieren, der von sich aus Text in sein EingabenFeld schreiben kann. Also sozusagen wie der normale WindowsEditor, nur das mein Programm dann auch Text in sich selbst schreiben kann. Wie z.B. zur Begrüßung „Hallo“, etc…

Vielen Dank für eine Antwort
Manfred

Ja, da meckert er natürlich,
ich habe ja auch geschrieben du sollst
pain.setDocument stehen lassen und darunter
pain.setText schreiben… das verlangt ein String Argument.

ema

[Bei dieser Antwort wurde das Vollzitat nachträglich automatisiert entfernt]

Hallo ema!
Vielen Dank für die Antwort, tut mir leid, dass eine muss ich wohl überlesen haben; aber jetzt funktioniert es.
Viele Grüße
Manfred