Drawing a Button in JTextField

Discussion in 'Java' started by Jason Cavett, Nov 17, 2008.

  1. Jason Cavett

    Jason Cavett Guest

    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.
     
    Jason Cavett, Nov 17, 2008
    #1
    1. Advertising

  2. Jason Cavett

    Guest

    On Nov 17, 8:25 pm, Jason Cavett <> wrote:
    > 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.
     
    , Nov 18, 2008
    #2
    1. Advertising

  3. Jason Cavett

    Jason Cavett Guest

    On Nov 17, 7:20 pm, "Peter Duniho" <>
    wrote:
    > On Mon, 17 Nov 2008 12:25:10 -0800, Jason Cavett <>  
    > wrote:
    >
    > > 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.
     
    Jason Cavett, Nov 18, 2008
    #3
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    1
    Views:
    676
    Kevin Spencer
    Jan 9, 2006
  2. Peter

    JTextField JPassword

    Peter, Jul 11, 2003, in forum: Java
    Replies:
    0
    Views:
    3,327
    Peter
    Jul 11, 2003
  3. me
    Replies:
    2
    Views:
    4,557
    Sudsy
    Jul 21, 2003
  4. Andrew Tucker

    JTable + JTextField

    Andrew Tucker, Jul 17, 2003, in forum: Java
    Replies:
    0
    Views:
    767
    Andrew Tucker
    Jul 17, 2003
  5. defn noob
    Replies:
    1
    Views:
    420
    Mark Space
    Jun 28, 2008
Loading...

Share This Page