English 中文(简体)
How to show scrollbar in Swing s JOptionPane.showOptionDialog?
原标题:

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
splintor

最佳回答

The 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.





相关问题
CSS working only in Firefox

I am trying to create a search text-field like on the Apple website. The HTML looks like this: <div class="frm-search"> <div> <input class="btn" type="image" src="http://www....

image changed but appears the same in browser

I m writing a php script to crop an image. The script overwrites the old image with the new one, but when I reload the page (which is supposed to pickup the new image) I still see the old one. ...

Firefox background image horizontal centering oddity

I am building some basic HTML code for a CMS. One of the page-related options in the CMS is "background image" and "stretch page width / height to background image width / height." so that with large ...

Separator line in ASP.NET

I d like to add a simple separator line in an aspx web form. Does anyone know how? It sounds easy enough, but still I can t manage to find how to do it.. 10x!

热门标签