7

Is there a way I can create a JTextArea or JTextField with some JLabels inside it, like in this screenshot from Facebook:

sample

What I am trying to do is put some JButtons with titles like "Apple", "Orange", ... When user clicks on a JButton of those, say "Orange", a Jlabel with the word Orange will be added to the JTextArea or JTextField. If user clicked on the [x] on the Jlabel, the word will be removed from the field.

jzd
  • 23,473
  • 9
  • 54
  • 76
Brad
  • 4,457
  • 10
  • 56
  • 93

1 Answers1

6

I would solve this in a different way: create a JPanel with the border you see. Add a JTextField without any borders into it, and simply insert your JLabels to the left (e.g. using BorderLayout in combination with FlowLayout). With a little tweaking of borders and spacings you should be able to create a very similar appearance without having to create new components.

Bombe
  • 81,643
  • 20
  • 123
  • 127
  • Very nice suggestion ... I have started implementing this. I am stuck at making the [x] icon and its event, but initially it is working fine. – Brad Mar 29 '11 at 15:32
  • That can also be implemented by a `JPanel`, two `JLabel`s, and a `BorderLayout`. :) – Bombe Mar 29 '11 at 17:08