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

17,731

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);
}}
Share:
17,731
swordartist
Author by

swordartist

Updated on June 15, 2022

Comments

  • swordartist
    swordartist almost 2 years

    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.

  • swordartist
    swordartist about 10 years
    Thank you user1967800 this works perfectly for my dialog. I almost gave up on this issue. but you saved me.