How to customize JBTextField?

I am looking for this end result:

  1. A “Submit Button” is presented WITHIN the text field, aligned at the right side.
  2. Hint text is displayed when no text is entered by the user.
  3. The user can input new-lines (e.g. copy-paste multiple lines of code, or press “SHIFT+ENTER” to add a new-line).

If JBTextField is not the right component to use, please advise on which one to use instead.

I tried using a JEditorPane, and it seems like I’m getting closer at least for the “multiple lines” things, but then I end up with some other issues I need to fix: the styling is wrong.
Is there a way to easily make it look like a JBTextField ? E.g. padding/insets, blue highlight when focused, normal font, etc.

Have you looked at ExtendableTextField ? You can probably register a custom ExtendableTextComponent.Extension that has submit like icon.

Or maybe ExpandableTextField, that looks like a regular text field, but when you edit it opens popup with a text area. This one has some interesting code that you maybe interested to as well.


You can tag your question with swing , so it’s easier to discover them :slight_smile:

Thanks, I’ll look into these recommendations! :slight_smile:
Just posting now to point out that there doesn’t seem to be a swing tag:

Turns out the easiest option for me was to use a JPanel with a custom border that was setup using JBUI.Borders stuff.

Then, this wrapper contained the JEditorPane I previously had, and I used addKeyboardAction to customize its behavior as I wanted.

This at least takes care of most of the UX requirements I had. The “internal button” requirement will probably be met by eventually refactoring this to use ExtendableTextField, as suggested above.