3

In my notepad application, I am trying to add an image as if it were a JLabel into a JTextPane by clicking on a JMenuItem called Picture.


private class Picture implements ActionListener
{
    public void actionPerformed(ActionEvent event)
    {
        fc = new JFileChooser();
        FileNameExtensionFilter picture = new FileNameExtensionFilter("JPEG files (*.jpg)", "jpg");
        fc.setFileFilter(picture);
        fc.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES);

        if (fc.showDialog(Notepad.this, "Insert")!=JFileChooser.APPROVE_OPTION)  return;
        filename = fc.getSelectedFile().getAbsolutePath();

        // If no text is entered for the file name, refresh the dialog box
        if (filename==null) return;

        // NullPointerException
        textArea.insertIcon(createImageIcon(filename));
    }

    protected ImageIcon createImageIcon(String path) 
    {
        java.net.URL imgURL = Notepad.class.getResource(path);

        if (imgURL != null) 
        {
            return new ImageIcon(imgURL);
        } 

        else 
        {
            JOptionPane.showMessageDialog(frame, "Could not find file: " + path);
            return null;
        }
    }
}

The issue lies on Line 20 where there is a NullPointerException, which I already know why this is happening but... How do I write that line of code so I could do something similar to textPane.add(image) (since I can't do textPane.add(StyleConstants.setIcon(def, createImageIcon(filename));)? Is there another I should write my code to execute this properly?

Rob
  • 435
  • 1
  • 5
  • 25
  • "Formatting code" just means making the code text look pretty and readable and has nothing whatsoever to do with program execution or function. Please clarify just what you're trying to do. – Hovercraft Full Of Eels Dec 30 '11 at 02:12
  • @HovercraftFullOfEels Okay, forget about the "formatting" part. I'm just wondering how to insert an image into a jtextarea after finding a picture in the dialog box and clicking "Insert". – Rob Dec 30 '11 at 02:15
  • What do you mean by "inserting an image into a JTextArea" though? Do you mean to use it as a background image? Or do you mean to add an image as if it were a JLabel or ImageIcon into the JTextArea? The clearer your problem, the easier it is for others to solve. – Hovercraft Full Of Eels Dec 30 '11 at 02:18
  • @HovercraftFullOfEels I mean add an image as if it were a JLabel into a JTextArea (just like what you do when you insert a picture into a Word document). Definitely not like a background image. – Rob Dec 30 '11 at 02:21
  • I may be wrong, but if that's your goal, a JTextArea may not be the best tool to use to solve it. You're likely much better off using a JEditorPane or JTextPane as these guys have the internal workings to handle what you are trying to do. – Hovercraft Full Of Eels Dec 30 '11 at 02:23
  • @HovercraftFullOfEels Uh oh, I meant JTextPane. That's what I've been using ever since I found out JTextArea wouldn't work for this purpose (along with changing the attributes of a string). Sorry, for the confusion. – Rob Dec 30 '11 at 02:25

2 Answers2

4

You can add components or icons to a text pane:

textpane.insertIcon(...);
textPane.insertComponent(...);
camickr
  • 321,443
  • 19
  • 166
  • 288
  • I have updated my code according to your suggestion. However I am still getting a Null Pointer Exception. After I click Insert in my dialog I am getting my message `"Could not find file: " + path`. – Rob Dec 30 '11 at 13:27
2

After much research I have finally figured it out! Special thanks to this post as well as camickr's post.


private class Picture implements ActionListener
{
    public void actionPerformed(ActionEvent event)
    {
        fc = new JFileChooser();
        FileNameExtensionFilter picture = new FileNameExtensionFilter("JPEG files (*.png)", "png");
        fc.setFileFilter(picture);
        fc.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES);

        if (fc.showDialog(Notepad.this, "Insert")!=JFileChooser.APPROVE_OPTION)  return;
        filename = fc.getSelectedFile().getAbsolutePath();

        // If no text is entered for the file name, refresh the dialog box
        if (filename==null) return;

        try 
        {
            BufferedImage img = ImageIO.read(new File(filename));
            ImageIcon pictureImage = new ImageIcon(img);
            textArea.insertIcon(pictureImage);
        } 

        catch (IOException e) 
        {
            JOptionPane.showMessageDialog(frame, "Could not find file: " + filename);
        }
    }
}
Community
  • 1
  • 1
Rob
  • 435
  • 1
  • 5
  • 25