aspose file tools*
The moose likes Swing / AWT / SWT and the fly likes Input an image in a Textarea Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Input an image in a Textarea" Watch "Input an image in a Textarea" New topic
Author

Input an image in a Textarea

anu johncy
Greenhorn

Joined: Dec 05, 2011
Posts: 11
hi , I am trying to input an image into a JTextArea using following code . Image is not displaying in the textarea

else if(action=="Image")
{
final ImageIcon imageIcon=new ImageIcon("E:/Anu/sec.gif");
final Image image=imageIcon.getImage();
Image grayImage = GrayFilter.createDisabledImage(image);

MF.TxtMsg=new JTextArea(){ //MailFrame MF


public void paint(Graphics g) {
System.out.print("inside paint \n");
g.drawImage(image,0 ,100,this);
super.paint(g);
}
};
System.out.print("outside JTextarea \n");
//repaint();
}

MF.Scroll = new JScrollPane(MF.TxtMsg);
Container content = MF.getContentPane();
content.add(MF.Scroll, BorderLayout.CENTER);
MF.setVisible(true);

}


Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39834
    
  28
Please don't use red text; many people find it difficult to read. I have changed it to black.

Don't use the == operator on reference types, least of all on Strings. Use the equals() method.
Brett Spell
Ranch Hand

Joined: Feb 24, 2001
Posts: 96
anu johncy wrote:I am trying to input an image into a JTextArea using following code . Image is not displaying in the textarea


As mentioned here, you can't display an icon in a JTextArea. You need to be using an instance of JEditorPane (or its subclass JTextPane).
anu johncy
Greenhorn

Joined: Dec 05, 2011
Posts: 11
Thanks for your Replay
Rob Camick
Ranch Hand

Joined: Jun 13, 2009
Posts: 2215
    
    7
Each component is responsible for painting its background color. When you invoke super.paint(...) the background of the component is painted over top of the image.

You need to use



This will prevent the text area from painting its background.

Also, follow Java variable naming conventions. Variable names should NOT start with an upper case character.

And use the "code tags" when posting code so the code retains its formatting and is readable.
anu johncy
Greenhorn

Joined: Dec 05, 2011
Posts: 11
Hai ,
I try to insert an image in a JTextArea on a button click . But the Paint(Graphics g){ } is not executing , the button click event is woking
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18904
    
    8

anu johncy wrote:But the Paint(Graphics g){ } is not executing , the button click event is woking


Did you perhaps create a subclass of some Swing class and try to override its paint(Graphics g) method? If that's the case then you didn't spell the name of the method correctly. Java is a case-sensitive language so a method named "Paint" doesn't override one called "paint". You can avoid this situation by using the @Override annotation, like this:



This code should give you a compiler error saying that Paint(Graphics) doesn't override any method in JPanel.

anu johncy
Greenhorn

Joined: Dec 05, 2011
Posts: 11
Netbeans as my Editor , i create the frame an external file and call the file in main java page . I want to load the image on button click, here is the code

else if(action.equals("Image"))
{

System.out.println("Inside Image");

final ImageIcon imageIcon=new ImageIcon("E:/Anu/sec.gif");
final Image image=imageIcon.getImage();
Image grayImage = GrayFilter.createDisabledImage(image);

System.out.print("inside JTextarea \n");

repaint();
System.out.print("outside JTextarea \n");

/* MF.TxtMsg=new JTextArea(){

public void paint(Graphics g) {
System.out.print("inside paint \n");

g.drawImage(image,0 ,100,this);
super.paint(g);
}
}; */

}



public void paint(Graphics g)
{
System.out.println("Inside Paint");
g.drawImage(image, 50, 500, this);
super.paint(g);
}
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Input an image in a Textarea