2

I am trying to create a text field similar to the 'tags' field in the Stackoverflow flow 'ask a question' page. That is, every time the use hits tab, the preceding text gets enclosed in a colored rounded rectangle and becomes a 'tag'.

I have started to do this by extending a JTextField; however, it is quite tedious, especially since I am doing custom painting.

Do you have any ideas how to approach this?

Thanks in advance!

rustybeanstalk
  • 2,722
  • 9
  • 37
  • 57

1 Answers1

3

On the "ask a question" page the text from the text field becomes a button so you now have two components.

First question is why would you use the tab key for this processing. How will the user be able to move off the text field. All GUI's should be designed for ease of use with the keyboard as well as the mouse.

So I would start with a JPanel that contains a single text field. Then when the "Enter" key is pressed you remove the text from the text field and create a JButton with the text and insert the button at position 0 of the panel. You can do this processing by adding an ActionListener to the text field to handle the Enter key.

camickr
  • 321,443
  • 19
  • 166
  • 288
  • Thanks for the answer. Of course he tab key would not be as practical, perhaps the enter, or space bar keys would make more sense. It's not so much the ActionListener I am having trouble with. I already have all of that functionality working. I am having a lot of trouble with the spacing. When I create the tag/JButton, it takes up more space than the plain text which it replaced. My cursor has to move ahead to make space etc... And in Java swing it seems that you are not allowed to edit the text from within a Listener (it creates a deadlock according to the docs). Any suggestions? – rustybeanstalk Feb 13 '12 at 22:04
  • I gave the steps required to solve the problem. If you need more help then you need to post your SSCCE that demonstrates the problem because I can't guess what you are doing. – camickr Feb 14 '12 at 04:29