aspose file tools*
The moose likes Swing / AWT / SWT and the fly likes Displaying an image in a TextArea Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Displaying an image in a TextArea" Watch "Displaying an image in a TextArea" New topic
Author

Displaying an image in a TextArea

rohith yenumula
Greenhorn

Joined: Dec 25, 2009
Posts: 14
Is it possible to display an image in a TextArea?
If so , using what?

thanks in advance.
Stanislav Lapitsky
Ranch Hand

Joined: Dec 01, 2009
Posts: 53
Not possible. Use JEditorPane/JTextPane.

Add the image in SimpleAttributeSet using StyleConstants.setIcon()

and then call pane.getDocument().insertString(offset, " ", theAttributesWitImage);

Regards,
Stas
Rob Camick
Ranch Hand

Joined: Jun 13, 2009
Posts: 2203
    
    7
Or to insert an Icon at the current caret position you can use:

Michael Dunn
Ranch Hand

Joined: Jun 09, 2003
Posts: 4632
are you talking about a background image,
where your text appears over the image?
rohith yenumula
Greenhorn

Joined: Dec 25, 2009
Posts: 14

are you talking about a background image,
where your text appears over the image?


No. I am trying to create network simulator in which a user can create his own network. The first step being placing the image icons of a router in an empty space and connecting them with a line.Actually I have one more doubt here. How to allow the user to draw a line between two icons?

Thanks in advance
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18662
    
    8

You wanted to display images. And now you want to draw lines? It's looking more and more of a bad idea to choose a text component for this. Maybe you should think again? You can draw on a JPanel, so that might be a better choice.
rohith yenumula
Greenhorn

Joined: Dec 25, 2009
Posts: 14
If you can spare some time, have a look at this page.http://www.mathiaz.com/index.php?n=Routage.Routage
Start the applet and choose the distance vector routing algorithm. In the space provided one can draw a network using the left click to display an icon and the right click to draw a line between two icons. I want to make something similar using swing.
pete stein
Bartender

Joined: Feb 23, 2007
Posts: 1561
rohith yenumula wrote:If you can spare some time, have a look at this page.http://www.mathiaz.com/index.php?n=Routage.Routage
Start the applet and choose the distance vector routing algorithm. In the space provided one can draw a network using the left click to display an icon and the right click to draw a line between two icons. I want to make something similar using swing.


Yes it can absolutely be done. As noted above, many would do the drawing in a JPanel.
rohith yenumula
Greenhorn

Joined: Dec 25, 2009
Posts: 14
Thanks pete for taking your time off. One thing that worries a bit is how to enable the user to draw lines between the two icons. If you have any idea please share it.
pete stein
Bartender

Joined: Feb 23, 2007
Posts: 1561
rohith yenumula wrote:Thanks pete for taking your time off. One thing that worries a bit is how to enable the user to draw lines between the two icons. If you have any idea please share it.


Myself, I'd have a non-GUI model that knows what the icons represent and their location as well as knows about and manages the connection lines. I'd have a drawing JPanel that uses this model to draw the icons in their positions as well as the connection lines. I'd give this JPanel both a MouseListener and a MouseMotionListener both of which use the same MouseAdapter object. The MouseAdapter would create tell the model to create the icons on mouse press and to create the connecting lines on mouse drag (if the start position is near one icon and the end position near another) and then would call repaint which would ask the paintComponent method to use the model data to update the image.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Displaying an image in a TextArea