aspose file tools
The moose likes Swing / AWT / SWT and the fly likes including an icon inside a text field. Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Reply Bookmark "including an icon inside a text field." Watch "including an icon inside a text field." New topic
Author

including an icon inside a text field.

Arun Thulaseedharan
Greenhorn

Joined: May 07, 2008
Posts: 20
hi all,
i want to include an icon inside a JTextField(at the extreme right)and when that icon is clicked a dialog box should appear.Can anyone suggest an approach for doing this??
thanks in advance.
arun
Maneesh Godbole
Saloon Keeper

Joined: Jul 26, 2007
Posts: 8430

Originally posted by Arun Thulaseedharan:
hi all,
i want to include an icon inside a JTextField(at the extreme right)and when that icon is clicked a dialog box should appear.Can anyone suggest an approach for doing this??
thanks in advance.
arun


1) Override the JTextField#paintComponent to paint the Icon at your desired location.
2) Add a MouseListener to your JTextField.
3) On receiving mouse clicks, find out if the click happened on your icon.
4) If yes, process it accordingly.


[Donate a pint, save a life!] [How to ask questions] [Onff-turn it on!]
Michael Dunn
Ranch Hand

Joined: Jun 09, 2003
Posts: 4632
create a component:
JPanel(BoxLayout) with JTextField and JLabel(icon).
modify borders so it looks like a single component.
add mouseListener to the label (to do whatever you want).
Maneesh Godbole
Saloon Keeper

Joined: Jul 26, 2007
Posts: 8430

I agree we are splitting hair here, but then we end up with three components instead of one.
 
I agree. Here's the link: http://zeroturnaround.com/jrebel - it saves me about five hours per week
 
subject: including an icon inside a text field.
 
Similar Threads
how to get favicon
Image inside jar file
how to change the ImageIcon of a JLabel?
How do i put a graphic behind another graphic icon?
Lets play a word game...