I'm trying to resize the height of the dialog box (JOptionPane
) for long sentence with hyperlink.
My code is ..
public class DialogTest {
public static void main(String[] args) throws Exception {
JTextPane jtp = new JTextPane();
Document doc = jtp.getDocument();
for (int i = 0; i < 50; i++) {
doc.insertString(doc.getLength(), " Hello Java World ", new SimpleAttributeSet());
if ((3 == i) || (7 == i) || (15 == i)) {
doc.insertString(doc.getLength(), " Hello Java World ", new SimpleAttributeSet());
SimpleAttributeSet attrs = new SimpleAttributeSet();
StyleConstants.setUnderline(attrs, true);
StyleConstants.setForeground(attrs, Color.BLUE);
String text = "www.google.com";
URL url = new URL("http://" + text);
attrs.addAttribute(HTML.Attribute.HREF, url.toString());
doc.insertString(doc.getLength(), text, attrs);
}
}
JScrollPane jsp = new JScrollPane(jtp);
jsp.setPreferredSize(new Dimension(480, 150));
jsp.setBorder(null);
JOptionPane.showMessageDialog(null, jsp, "Title", JOptionPane.INFORMATION_MESSAGE);
}}
If I don't set the preferred size, that dialog is gonna be really long, and it's not readable. So, I want to fix the width to 480.
And, I want to adjust height depends on the length of the text.
If I run this code, I see the vertical scrollbar. But I don't want to show that scrollbar and adjust the height of the dialog.
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…