3

I like the convenience of a JOptionPane, but don't like the fact that it doesn't wrap text. So I decided to implement the answer from this question as follows:

public static void main(String[] args)
 {
  String text = "one two three four five six seven eight nine ten ";
  text = text + text + text + text + text
  JTextArea textArea = new JTextArea(text);
  textArea.setColumns(30);
  textArea.setLineWrap( true );
  textArea.setWrapStyleWord( true );
  textArea.append(text);
  textArea.setSize(textArea.getPreferredSize().width, 1); //Explanation for this line in the comments of the linked thread
  JOptionPane.showMessageDialog(
   null, textArea, "Not Truncated!", JOptionPane.WARNING_MESSAGE);
 }

This works just fine on Mac OS X:

enter image description here

But does not work on Windows 8 because the window does not resize as the JTextArea increases in height with more multi-lines, thus pushing the buttons out of the way:

enter image description here

Is there something I'm doing wrong? Does Java Swing on the two platforms behave differently? How can I fix this problem?

Community
  • 1
  • 1
Thunderforge
  • 19,637
  • 18
  • 83
  • 130
  • 1
    Seems fine on Windows 7 too. This looks like a windows 8 thang.... – David Kroukamp Jan 13 '13 at 21:11
  • In Ubuntu it is fine too... – Costis Aivalis Jan 13 '13 at 21:17
  • Well, that sure makes it interesting. Stupid Windows 8... I don't suppose there are any good workarounds? – Thunderforge Jan 13 '13 at 21:24
  • 3
    A simple workaround would be to create a modal JDialog yourself and make it visible either if your program runs in Windows 8, or always. At least while you are waiting for the next Win8 service pack... – Costis Aivalis Jan 13 '13 at 21:29
  • off topic, how did you get the nice shadow on your mac screen capture? – Will Jan 13 '13 at 23:24
  • 1
    @Will: [*How do I create screenshots?*](http://meta.stackexchange.com/questions/99734/how-do-i-create-a-screenshot-to-illustrate-a-post) – trashgod Jan 13 '13 at 23:55
  • 2
    @Thunderforge: See also these [alternatives](http://stackoverflow.com/q/14011492/230513). – trashgod Jan 13 '13 at 23:59
  • @Will, the instructions on that page for the Mac do have the instructions. I used Command+Shift+4 and then the space bar to get just the frontmost window – Thunderforge Jan 14 '13 at 00:19
  • 1
    I discovered that the problem isn't actually the JTextArea, but the fact that I had set the default look and feel [UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());] Turning that of and defaulting to metal makes it look right, but it's not the Windows 8 LAF – Thunderforge Jan 14 '13 at 00:21
  • Also, this behavior happens on Windows 7 too if you call the same code to set the default look and feel. – Thunderforge Jan 14 '13 at 00:44
  • *"I discovered that the problem isn't actually the JTextArea, but the fact that I had set the default look and feel"* By 'default' I am guessing you mean 'system'. Could you code that up as an answer? It seems like this Q&A might be the grounds for a bug report against that version of that PLAF. – Andrew Thompson Jan 14 '13 at 01:41
  • @trashgod I totally failed to be surprised to see your comment re. screenshots. :) From someone who posts a lot of half decent screenshots myself, I have to say, I like yours (OS X & Linux) better. What did surprise me was your reply to that thread you linked for alternatives. The thing that surprised me was that you'd also posted a better answer to that, and I'd apparently never noticed it! Also liked the comment.. *"+1 for good example and no html"*. ;) Alludes, very briefly, to the variety of pitfalls of relying on the way the toolkit renders HTML 3.2 & simple CSS. – Andrew Thompson Jan 14 '13 at 01:49
  • @AndrewThompson: Thank you for commenting; I appreciate your insights. No need to explain; I added my example after your answer. I particularly like that [Q&A](http://stackoverflow.com/posts/14011492/edit) for answers that complement one another. – trashgod Jan 14 '13 at 05:40

1 Answers1

5

I discovered that the problem isn't actually the JTextArea, but the fact that I had set the default look and feel for Windows 8 using the following command elsewhere in the program

UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());

Removing that line and defaulting to the Metal Look and Feel (as was the case in the snippet I posted at top) makes everything look right. It hadn't even occurred to me that it might be the issue until I realized that the dialog boxes from the main method above didn't look the same as it did in my screenshot.

A friend of mine using Windows 7 tried the program with that line of code included and the same thing happened. So it appears that this is a bug in Java with at least Windows 7 and Windows 8 (running Java 7u10 for me, either 6 or 7 for her).

Per the alternatives provided, I've since switched over to using a JLabel with CSS for the moment because it's quick and dirty for my plain text purposes (and I KNOW it will work cross-platform), but I may migrate to JEditorPanes in the future.

Thunderforge
  • 19,637
  • 18
  • 83
  • 130
  • 2
    +1 Great answer. I especially liked the closing sentence conclusion *"may migrate to JEditorPanes"*. It is the better solution. – Andrew Thompson Jan 14 '13 at 02:14