views:

117

answers:

2

What I am trying to do is open up a JFilechooser that filters jpeg,gif and png images, then gets the user's selection and inserts it into the JEditorPane. Can this be done? or am i attempting something impossible? Here is a sample of my program.(insert is a JMenuItem and mainText is a JEditorPane)

insert.addActionListener(new ActionListener(){
  public void actionPerformed(ActionEvent e){
    JFileChooser imageChooser = new JFileChooser();
      imageChooser.setFileFilter(new FileNameExtensionFilter("Image Format","jpg","jpeg","gif","png"));
                int choice = imageChooser.showOpenDialog(mainText);
                if (choice == JFileChooser.APPROVE_OPTION) {
                mainText.add(imageChooser.getSelectedFile());
                }
        }
    });

What i tried to do is use the add method, i know it's wrong but just to give you an idea of what i'm trying to do. Before you complain, i'm sorry about the code formatting, i don't really know all the conventions of what is considered good or bad style. Thank you very much.

This is the part of the code i use to save the html file.

else if (e.getSource() == save) {
        JFileChooser saver = new JFileChooser();
        saver.setFileFilter(new FileNameExtensionFilter(".html (webpage format)" , "html"));
        int option = saver.showSaveDialog(this);
        if (option == JFileChooser.APPROVE_OPTION) {
            try {
                BufferedWriter out = new BufferedWriter(new FileWriter(saver.getSelectedFile().getPath()));
                out.write(mainText.getText());
                out.close();
            } catch (Exception exception) {
                System.out.println(exception.getMessage());
            }
        }
    }
A: 

This should do it:

mainText.setContentType("text/html");
String image = String.format("<img src=\"%s\">", imageChooser.getSelectedFile());
mainText.setText(image);
jonescb
thank you so much, i had been in a trial and error situation with this for days until i decided to ask for help here, thanks.
JIM
and what if i wanted to add it at the end of some text i've already written? I mean , i cant use setText(mainText.getText() + image); because that would put it outside of the body tags, what could i do in that case?
JIM
@JIM, what I would do is String text = mainText.getText(), then using String's substring() method cut off the last 7 characters (</body>). Then append your <img>, and append a new </body>. The substring call might look like text = text.substring(0, text.length() - 7);
jonescb
A: 

Its easier to just use a JTextPane. Then you can use insertIcon(...) anywhere in the text.

Edit:

I have never had much luck trying to manipulate HTML but I've used code like the following before:

HTMLEditorKit editorKit = (HTMLEditorKit)textPane.getEditorKit();
text = "<a href=\"abc\">hyperlink</a>";
editorKit.insertHTML(doc, textPane.getCaretPosition(), text, 0, 0, HTML.Tag.A);

So presumably the code would be similiar for the IMG tag.

camickr
thank you, THIS was what i was looking for, i somehow supposed that JEditorPane and JTextPane would have the same methods, since they are really alike.thank you
JIM
yes, but is it possible that when i save the file as an html, i can open it with my browser and the image will show up this way? because that is my only concern now.
JIM
The two components are used for different purposes. If you need the text in HTML format then this won't work. See my edit above.
camickr