Search code examples
javaswingjtextfieldrenderer

Custom renderer on a JTextField. Is it possible ?


I need to create component that looks like JTextField (same borders, shadows, rounded corners etc) but for displaying some graphic with text.

Is it possible to create something like renderer for a JTextField? Or Create custom component based on JPanel, but how to force it to looks same as JTextField

Another complication is that application will be running on different LAF's changed on the fly.

Do you have any ideas ?


Solution

  • No, Swing does not provide any DefaultTextFieldRenderer or TextFieldRendere interface you can build on top of. You mention you like L&F aware rendering; thats already part of the answer: you would have to implement a UI for each L&F to render graphics into a TextField - this may be feasible for a selected few (lets say Windows/Mac/Metal), but hardly for any L&F that a user may want to use (if you leave them a choice).

    Let me guess, you have the requirement to display some sort of status/validation icon with the field. Do the next best thing and display that as an icon next to the field, not in the TextField itself. Much less trouble :)