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:

Recommended Answers

All 3 Replies

Ok I solved the save/open part using serialization.

Now I only need help with auto-complete.Someone?

I have little experience with Swing, but I'm guessing that you want to implement a Listener that would check after each keystroke to see what the user was typing. If they were typing the pattern you wanted to match, it could then insert the appropriate text.

Here's a tutorial from the Oracle documentation on how to implement a KeyListener.

Hello again.

I tried to use pattern/match and it works to a certain degree.I have used a System.out.println to see if the replace code works and it does.For example if I write for it prints for(){}; but I can't make it replace it in the text area of the editor.That's the code I used:

String data = editArea.getText();
String REGEX = "for";
String REPLACE = "for(){}";
String	INPUT = data;
Pattern p = Pattern.compile(REGEX);
Matcher m = p.matcher(INPUT); 
INPUT = m.replaceAll(REPLACE);
data = INPUT;
editArea.setText(data);
System.out.println(INPUT);

When I use the editArea.setText(data); it throws an error when I run the program.Any recommendations?

Furthermore when using serialization for open/save when I open the text the bold/italics etc and the word/line counter does not work so I think I'll try using again the approach I had at first post.However I still can't get the document to open without the HTML tags.Any help?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.