I have to extend the size of JTextField on KeyPressed event as user enter the text in textfield.please give me some idea how to achieve this?
thanks in advance
I have to extend the size of JTextField on KeyPressed event as user enter the text in textfield.please give me some idea how to achieve this?
thanks in advance
Okay, jumping for it :-)
Let's assume the question is
Usual collaborators
Quick example:
type ... and nothing happens: size remains at initial size. That's surprise: for some reason, the field's auto-validation simply doesnt't happen. In fact, a manual revalidate of the field on receiving a change notification (Note: the only correct listener type here is a DocumentListener) doesn't change the field as well:
@Gagandeep Bali found out that it's the parent that needs to be revalidated:
Unexpected, so the next question is the notorious why? Here: why doesn't the invalidate bubble up the container hierarchy until it finds a validateRoot? The answer is in the api doc:
Or in other words: it's not bubbled up because the field itself is a validateRoot. Which leaves the other option to override and unconditionally return false:
The price to pay for this, is that the scrolling doesn't work - which isn't a big deal in this context, as the text always fits into the field. Or implement slightly more intelligent, and return true if the actual width is less than the pref.
Depends if you are using a LayoutManager or not. If not, attach a
KeyListener
to theJTextField
and onkeyRelease
you need to calculate the length of the String (in pixels) and determine if the field needs to be updatedPossibly a more sensible solution might be:
I would also pay attention to what kleopatra & mKorbel have to say. While
KeyListener
might seem like a good idea, there are just to many situation's where it won't be notified -setText
is the major one.1) never to use
KeyListener
forJTextComponent
, useDocumentListener
, nothing else2) you can to use
FontMetrics
,TextLayout
,SwingUtilities
3) after resize you have to notify
LayoutManager
,4) if is there only
JTextField
then to usepack()
forTop-Level Container
,5) otherwise (in the case that there is more than one
JPanel
nested otherJComponents
) you have to re_layout whole Container withrevalidate()
andrepaint()
thencall
pack()
to theTop-Level Container
if you want to resize continiously with its contensdon't call
pack()
to theTop-Level Container
if you don't want to resize, but required usage ofJScrollPane
6) in the case that value of String could be very long, then to use proper
JTextComponent
with supporting multiline output to the GUI, to useJTextArea
(inJScrollPane
) rather than plainJTextField
The best way I can think of is to add one CaretListener to the
JTextField
concerned, and with the change in the length of theDocument
, youIncrease/Decrease
the columns of the saidJTextField
by calling it's setColumns(...), which inturn willIncrease/Decrease
the size of theJTextField
Here is one example to show you, how to achieve this :