wood burning stoves 2.0*
The moose likes Swing / AWT / SWT and the fly likes Inserting an Image in JTextField Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Inserting an Image in JTextField" Watch "Inserting an Image in JTextField" New topic
Author

Inserting an Image in JTextField

Shaan Shar
Ranch Hand

Joined: Dec 27, 2005
Posts: 1249

Hi All,

I don't have a lot of expereince in Swings, Actually I have to insert an Image in JTextField.

I just want to clear that, I am not asking for the code exactly.

But atleast, Could someone throw some light on this thread, And One more thing I cann't use any other components.

Could You all please let me know, whether is it possible.

Moderators: Please don't take otherwise, this is not at all a simple Homework exercise. So please don't mind it.
[ September 20, 2006: Message edited by: Ankur Sharma ]

The Best way to predict your future is to create it - Every great individual common man
Manuel Moons
Ranch Hand

Joined: Mar 05, 2002
Posts: 229
You want to insert an image in a TextField??

What does that mean?

Do you want an image to be drawn in the background of the textfield? Or do you want the url of the image in the textfield?

Please explain a little more what you are trying to do!
Shaan Shar
Ranch Hand

Joined: Dec 27, 2005
Posts: 1249

Originally posted by Manuel Moons:
You want to insert an image in a TextField??

What does that mean?

Do you want an image to be drawn in the background of the textfield? Or do you want the url of the image in the textfield?

Please explain a little more what you are trying to do!


Thankx for your concern Manuel,

Actually I would like to draw an image in background of the TextField.

I didn't get you by
"do you want the url of the image in the textfield?"


Thankx for the early help.
Michael Dunn
Ranch Hand

Joined: Jun 09, 2003
Posts: 4632
this might be another option, transparent textfield in a panel with background image

if you do actually want the image as part of the textfield, create the
textfield the same way as ImagePanel

Shaan Shar
Ranch Hand

Joined: Dec 27, 2005
Posts: 1249

Originally posted by Michael Dunn:
this might be another option, transparent textfield in a panel with background image

if you do actually want the image as part of the textfield, create the
textfield the same way as ImagePanel



Well Michael,

Thankx for your such reply, but here the problem is when I type in this text field, the image is going in the background, but I need a slight change, that If we type some thing in textfield then the image should be shown in left side of the text field. and after the image text should be there.


Well, Hope for early reply.
Thankx Michael once again.
[ September 20, 2006: Message edited by: Ankur Sharma ]
Michael Dunn
Ranch Hand

Joined: Jun 09, 2003
Posts: 4632
see if this gets a bit closer, a label adjacent to a textfield

Shaan Shar
Ranch Hand

Joined: Dec 27, 2005
Posts: 1249

Originally posted by Michael Dunn:
see if this gets a bit closer, a label adjacent to a textfield



It was nice code trick. Thankx a lot Michael. It was nice.

This meets my requirements, but this states a problem in my mind, that whether is it possible to draw a image in JTextField really or not.

Once again Michael, Thankx a lot for your kinda effort.
Shaan Shar
Ranch Hand

Joined: Dec 27, 2005
Posts: 1249

So is this is not possible in JAVA to draw Image Icon in JTextField..???

Could anybody throw some light......

Early responses will be appreciated....
Michael Dunn
Ranch Hand

Joined: Jun 09, 2003
Posts: 4632
it can be done the same way as my earlier post with the JPanel.

the image in the background is no problem, but getting the text to display
adjacent to the image looks a bit tricky - maybe someone has some code for this.

Craig Wood
Ranch Hand

Joined: Jan 14, 2004
Posts: 1535
Shaan Shar
Ranch Hand

Joined: Dec 27, 2005
Posts: 1249

I must HATS OFF for Michael Dunn & Craig Wood for their excellent technical help..... Which I was in need indeed....


Thankx a lot Michael & Craig
[ September 20, 2006: Message edited by: Ankur Sharma ]
Shaan Shar
Ranch Hand

Joined: Dec 27, 2005
Posts: 1249

Originally posted by Craig Wood:


BTW Craig,

Could you please throw some light on this piece of Code....

What you have done exactly, Do you have taken advantage of JTextField's Parent Class JTextComponent.
???

This is what I understand. Could you please eloborate this code some more, for understanding.
Shaan Shar
Ranch Hand

Joined: Dec 27, 2005
Posts: 1249

Actually this is the great code....

But I also want to learn this concept..

Actually I want to know, how to approach regarding this problem/situation in Swings.

I mean how do you know whether we need to create an annoymnous class in which we have to override the paint method....

It was great... That's what I also want to learn..

As I have already marked I am not very much expereinced in Swings...
Could anyone atleast guide me....



How and who call this overriden Paint method of new JTextField class.
Craig Wood
Ranch Hand

Joined: Jan 14, 2004
Posts: 1535
What you have done exactly
I made a simple JTextField to illustrate one way you could draw an image inside it.
You can override the paintComponent method for many JComponents to customize their
appearance. This is explained in the tutorial and detailed in a number of articles
referenced in it. Here is an enty point: Lesson: Performing Custom Painting.

Do you have taken advantage of JTextField's Parent Class JTextComponent.
Yes, through inheritance. The JTextComponent class has a lot of methods that its
subclasses (such as JTextArea, JTextField, JTextPane) use. These methods are "abstracted"
to this superclass so they can be shared by the subsclasses and don't have to be
laboriously repeated in each subclass. The JComponent method used in the code is
setMargin. The paintComponent method that is overridden to draw the image is
actually in the JComponent class.
To see this, look in the JTextField class api. At the top is the class hierarchy shown as
a graph. It shows how this class descends from Object. Then scroll down below the
Method Summary section to the sections that correspond to these classes to find the
methods available to this (JTextField) class. You find the paintComponent method in
the section Methods inherited from class javax.swing.JComponent.
Learning how to find your way and being able to move around in the javadocs like this is a
key to freedom in writing code/making things in java.

how do you know whether we need to create an annoymnous class in which we have to
override the paint method....

This is a fascinating question. The answer seems to be a matter of experience and personal
preference. Java is designed in such a way as to allow many ways of designing and
assembling things. This makes it flexible. But it takes some time and practice to learn
how to do things and then how you want to do them.
Whether you want to make the JTextField a separate class or a nested class, and if a
nested class, whether to create a class that extends JTextField or instantiate a new
JTextField as in the code snippet is a matter of personal choice. One of the main
criterion is that it be easy to read, understand and follow. I chose the latter here
because it was concise, compact, direct and allowed for a linear flow.
Shaan Shar
Ranch Hand

Joined: Dec 27, 2005
Posts: 1249

Craig,

Excellent Help.......

Thankx a lot... for your very much nice explanations.


I Bow myself for your respect.

Once again thankx a lot...
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Inserting an Image in JTextField