Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Resize dialog message (JOptionPane) for long sentence with fixed width

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.

like image 344
swordartist Avatar asked Feb 27 '14 15:02

swordartist


1 Answers

To fix the width and adapt the height, I personally use this trick : You fix an arbitrary height and the targeted width with setSize, and then get the expected height with getPreferredSize() :

jtp.setSize(new Dimension(480, 10));
jtp.setPreferredSize(new Dimension(480, jtp.getPreferredSize().height));

The full code would be :

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);
    jtp.setSize(new Dimension(480, 10));
    jtp.setPreferredSize(new Dimension(480, jtp.getPreferredSize().height));

    //JOptionPane.showMessageDialog(null, jsp, "Title", JOptionPane.INFORMATION_MESSAGE);
    JOptionPane.showMessageDialog(null, jtp, "Title", JOptionPane.INFORMATION_MESSAGE);
}}
like image 52
Sharcoux Avatar answered Nov 12 '22 20:11

Sharcoux