javaswingvalidationvalidationmessage

Simple, wrapping red validation message in java swing


So I've been developing web applications for 10 years, but have fond memories of working on small swing applications that related to an introductory programming curriculum I was paid to help with before that. I generally like building swing/java UI especially with a decent UI builder. However, in the intervening time I've become very accustomed to providing clear, red error messages of variable length and number when the data on a form is invalid. Sometimes with icons, sometimes with markers on the field etc. Very common stuff that's easy to do in the web world, and is really helpful and improves usability.

Today I was trying to find a way to display a nice list of error messages at the top or bottom of my swing form, icons and field markers can come later, but for now I just want a nice variable sized list of things that need to be corrected, in a red color.

At first blush this seems like it would be easy, but..

There's also the minor issue of pushing outside the window space, and one can call pack(), but Id like to only increase the height of the form and wrap the text not change the dialog to the width of the message (creating lots of strained layout and dead space if a single long message doubles the width)

I also don't really want to have scroll bars either for the message area or the form as a whole.

This surely is a wheel that's already been invented, so what's the right way to show a list of red (but otherwise unformatted) error messages in a swing form?


Solution

  • Label doesn't wrap, so long text is hidden, or you have to make the form ultra wide

    JText Area has the same issue unless you define a number of columns, and I want to fill the space available. A set number of columns won't resize.

    A set number of columns won't resize. and together with "I also don't really want to have scroll bars"

    JTextPane wraps but as far as I can tell it won't turn red unless you get into the whole way over complicated StyledDocument bit. I don't want to do anything more complicated than change the text color so this seems like overkill.