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
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 ]
Joined: Jun 09, 2003
see if this gets a bit closer, a label adjacent to a textfield
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.
Joined: Jan 14, 2004
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.