Hello.
I am trying the last few days to make a text editor in Java but as I am new to the language I have some problems that I don't know how to deal with.
The first one is that I want to save my text and then open it again.However I want to keep all the bold,italic etc characters.I have managed to save it and when I open the file with Firefox it keeps the formatted text but when I open it in my program it looks like this:
<html>
<head>
<style>
<!--
p.Bold {
bold:bold;
}
p.Underline {
underline:underline;
}
p.Italic {
italic:italic;
}
p.default {
size:4;
bold:normal;
italic:;
family:Lucida Grande;
}
-->
</style>
</head>
<body>
<p class=default>
<span style="font-size: 13pt; font-family: Lucida Grande">
<b>Nice</b>
</span>
<span style="font-size: 13pt; font-family: Lucida Grande">
weather
</span>
<span style="font-size: 13pt; font-family: Lucida Grande">
<u>today</u>
</span>
<span style="font-size: 13pt; font-family: Lucida Grande">
</span>
<span style="font-size: 13pt; font-family: Lucida Grande">
<i>you agree</i>
</span>
<span style="font-size: 13pt; font-family: Lucida Grande">
?
</span>
<span style="font-size: 13pt; font-family: Lucida Grande">
<b> </b>
</span>
</p>
</body>
</html>
This is the code I have written.(editArea is JTextPane)
class OpenListener implements ActionListener {
public void actionPerformed(ActionEvent ae) {
JFileChooser chooser = new JFileChooser();
chooser.setMultiSelectionEnabled(false);
int option = chooser.showSaveDialog(TextEditor.this);
if (option == JFileChooser.APPROVE_OPTION) {
StyledDocument doc = (StyledDocument)editArea.getDocument();
HTMLEditorKit kit = new HTMLEditorKit();
BufferedInputStream in;
try {
in = new BufferedInputStream(new FileInputStream(chooser.getSelectedFile().getAbsoluteFile()));
kit.read(in, doc, doc.getStartPosition().getOffset());
} catch (IOException ioex) {
System.exit(1);
}
catch (BadLocationException be){}
}
}
}
class SaveListener implements ActionListener {
public void actionPerformed(ActionEvent e) {
if (editArea.getText().length() > 0){
JFileChooser chooser = new JFileChooser();
chooser.setMultiSelectionEnabled(false);
int option = chooser.showSaveDialog(TextEditor.this);
if (option == JFileChooser.APPROVE_OPTION) {
StyledDocument doc = (StyledDocument)editArea.getDocument();
HTMLEditorKit kit = new HTMLEditorKit();
BufferedOutputStream out;
try {
out = new BufferedOutputStream(new FileOutputStream(chooser.getSelectedFile().getAbsoluteFile()));
kit.write(out, doc, doc.getStartPosition().getOffset(), doc.getLength());
} catch (FileNotFoundException ex) {
} catch (IOException ioe){
} catch (BadLocationException be){
}
}
}
}
}
And the next problem(and the main one) is that I want when I write for or while the the text editor to auto-complete
for ( expression )
{ // your code goes here
}
or
while (expression){
// your code goes here
}
That's all for now.
Thanks for your help:icon_smile: