In our Swing application, we show error messages using JOptionPane.showOptionDialog. However, some of the messages are long and we开发者_如何学运维 would like to limit the height of the dialog and show a scrollbar. We thought of using HTML and CSS in the dialog text to limit the height and show scrollbar, but it seems Swing's HTML & CSS is quite limited.
Can anyone thing of a way to do it, other then developing our own dialog to show error messages?
Thanks
splintorThe message
parameter to the showOptionDialog
method is of Object
type. The API documentation specifies different behaviours depending on what type is passed there. What should be helpfull in your case is the possibility of passing Component
object. This way you can pass any component, including JScrollPane
which will handle scrollbars if necessary. So if your message is in String msg instead of passing it directly you should construct something like this: new JScrollPane(new JLabel(msg))
.
What you show on the JOptionPane
is an Object
. While you usually use a String
, nothing stops you from displaying a non-editable JTextArea
(which has scrollbar behavior built in, I believe) or a JScrollPane
containing a large JLabel
(or anything like that).
You'll want to set the PreferredSize
on the JScrollPane, and perhaps also setHorizontalScrollBarPolicy
and setVerticalScrollBarPolicy
.
精彩评论