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

2

8

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.

Cetane answered 27/2, 2014 at 15:28 Comment(6)
possible duplicate of Should I avoid the use of set(Preferred|Maximum|Minimum)Size methods in Java Swing?Apc
um.. I didn't find the answer for me in the thread.Cetane
The gist of it is that you should not be using setPreferredSize (it does not do what you think it would do). Sorry for not suggesting an alternative approach but I am short on time at the moment.Apc
1) For better help sooner, post a MCTaRE (Minimal Complete Tested and Readable Example). 2) What is the document type, HTML? If so, you can use the same technique seen in this answer.. (but if you cannot get it to work, post a MCTaRE rather than uncompilable code snippets).Tripp
java-sl.com/tip_text_height_measuring.htmlIsola
thanks everyone who spent time on my question, user1967800 gave me the answer I was looking for.Cetane
W
6

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);
}}
Waterrepellent answered 28/2, 2014 at 14:47 Comment(1)
Thank you user1967800 this works perfectly for my dialog. I almost gave up on this issue. but you saved me.Cetane
E
0

To resize your JOptionPane please use the overridden class displayed in below URL:-

ModifiableJOptionPane

Empirin answered 6/3, 2018 at 7:30 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.