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.

Andrew Thompson
  • 163,965
  • 36
  • 203
  • 405
swordartist
  • 272
  • 2
  • 3
  • 7
  • possible duplicate of [Should I avoid the use of set(Preferred|Maximum|Minimum)Size methods in Java Swing?](http://stackoverflow.com/questions/7229226/should-i-avoid-the-use-of-setpreferredmaximumminimumsize-methods-in-java-swi) – Kevin Panko Feb 27 '14 at 15:49
  • um.. I didn't find the answer for me in the thread. – swordartist Feb 27 '14 at 16:53
  • 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. – Kevin Panko Feb 27 '14 at 16:58
  • 1
    1) For better help sooner, post a [MCTaRE](http://stackoverflow.com/help/mcve) (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](http://stackoverflow.com/a/7861833/418556).. (but if you cannot get it to work, post a MCTaRE rather than uncompilable code snippets). – Andrew Thompson Feb 28 '14 at 03:20
  • 2
    http://java-sl.com/tip_text_height_measuring.html – StanislavL Feb 28 '14 at 05:19
  • thanks everyone who spent time on my question, user1967800 gave me the answer I was looking for. – swordartist Feb 28 '14 at 17:35

2 Answers2

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);
}}
Sharcoux
  • 4,282
  • 4
  • 29
  • 65
-1

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

ModifiableJOptionPane

Abhishek K
  • 477
  • 8
  • 19