0
chatTextPane = new JTextPane();
chatTextPane.setPreferredSize(new Dimension(350,150));
//chatTextPane.setMaximumSize(new Dimension(350,150));//new
scrollingTextPane = new JScrollPane(chatTextPane);
scrollingTextPane.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
scrollingTextPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
//scrollingTextPane.setMaximumSize(new Dimension(350,150));

I'm writing a chat program on JDialog. My JTextPane is going out of boundary from the JDialog. I have added two pictures. One is before closing the JDialog window and the other is after opening the JDialog window again.

MadProgrammer
  • 343,457
  • 22
  • 230
  • 366

1 Answers1

3

Get rid of chatTextPane.setPreferredSize(new Dimension(350,150));. It's prevent it from telling the JScrollPane how big it should be.

Take a look at Should I avoid the use of set(Preferred|Maximum|Minimum)Size methods in Java Swing? for more reasons why you should avoid using setPreferredSize...just in case your mistake this time didn't make it clear ;)

Community
  • 1
  • 1
MadProgrammer
  • 343,457
  • 22
  • 230
  • 366
  • I removed chatTextPane.setPreferredSize(new Dimension(350,150)); Now chatTextPane is really really small. You can barely see it. How can I set the size?? I tried setSize(new Dimension(350,150)); but it doesn't work. – David0099 May 08 '15 at 04:59
  • Well, normally, I'd use a layout manager. But, `JTextPane` is one of those annoying components which doesn't allow you to change the "preferredScrollableViewportSize` value, which is actually based on the components `preferredSize` ... not good. In your case, you may have no choice but to set the preferredSize of the `JScrollPane` instead...not what I'd prefer, but I can't see a better solution... – MadProgrammer May 08 '15 at 05:09
  • I got it....I set scrollingTextPane..setPreferredSize(new Dimension(350,150)); ......It works now... thanks anyway – David0099 May 08 '15 at 05:15