• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Input an image in a Textarea

 
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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);

}


 
Marshal
Posts: 79180
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 118
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for your Replay
 
Rancher
Posts: 3324
32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Marshal
Posts: 28193
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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);
}
 
Check your pockets for water buffalo. You might need to use this tiny ad until locate a water buffalo:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic