Drawing a Button in JTextField

J

Jason Cavett

I want to place a button within a JTextField (at the far right). I'm
easily able to put images and other things in the JTextField by
overriding paint(Graphics), but I can't seem to insert a component.
(For the record, I'm extending JTextField.)

Does anybody have any suggestions? I can't find any good information
in my searches, and I'm somewhat stuck.


Thanks

P.S. I do realize I could just create a panel (or, rather, extend
JPanel) and place a JTextField on it and a button right next to it,
but I want button to be part of the component because developers may
still want to use the JTextField's methods. I could do an adapter to
all the JTextField's methods, but that seems like a waste of time, and
a bit confusing to the user of my component.
 
S

syedbodrul

I want to place a button within a JTextField (at the far right).  I'm
easily able to put images and other things in the JTextField by
overriding paint(Graphics), but I can't seem to insert a component.
(For the record, I'm extending JTextField.)

Does anybody have any suggestions?  I can't find any good information
in my searches, and I'm somewhat stuck.

Thanks

P.S.  I do realize I could just create a panel (or, rather, extend
JPanel) and place a JTextField on it and a button right next to it,
but I want button to be part of the component because developers may
still want to use the JTextField's methods.  I could do an adapter to
all the JTextField's methods, but that seems like a waste of time, and
a bit confusing to the user of my component.

Having the same problem too.
 
J

Jason Cavett

I want to place a button within a JTextField (at the far right).  I'm
easily able to put images and other things in the JTextField by
overriding paint(Graphics), but I can't seem to insert a component.
(For the record, I'm extending JTextField.)
Does anybody have any suggestions?  I can't find any good information
in my searches, and I'm somewhat stuck.  [...]

Maybe you can be more specific about what you mean by "within" and "insert  
a component".

Like all Swing components, JTextField inherits java.awt.Container, which  
means that it can contain other components.  You should be able to just  
add your button as a child of the JTextField (Container.add() method), use  
the layout manager of your preference to put it where you want  
(Container.setLayout(), along with configuring the layout manager itself),  
and have that just work.

Have you tried anything like that?  If so, what didn't work?  If not, why  
haven't you?

Pete

I did try the add method earlier. However, I did not play around with
the layout. I figured the default layout was a border, so everything
should work correctly if I would just do an add. Apparently I was
wrong - as soon as I added a layout, the button was added.

There still is the problem that the text (when I type into the box)
overlaps with the button. I'm trying out the various methods to see
if I can get scrolling to happen before I hit the button. Other than
that, it looks and works great.

Thanks for your help.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,020
Latest member
GenesisGai

Latest Threads

Top