JOptionPane setMaximumSize with a JScrollPane as the object doesn't work

I am trying to use a JOptionPane for error messages. I'm trying to make the JOptionPane dialog be a minimum of 500x100 and a maximum size of 500x450 depending on the amount of text in the JTextArea inside the JScrollPane that's being passed into JOptionPane. I've tried both JOptionPane.showMessagePane(), and instantiating a JOptionPane and then setting its maximumSize, neither of which work as expected. Here is the relevant code:
public static void doCommonMessagePane( Component parent, String optMsg,
                                         String title, int msgType )
    JTextArea msg = new JTextArea( optMsg );
    JScrollPane jsp = new JScrollPane( msg );
    initOptionPaneComponents( msg, jsp );
    Dimension maxSize =new Dimension(500, 450) ;
    JOptionPane op = new JOptionPane(jsp, msgType);
    op.setMaximumSize(maxSize);
    op.revalidate();
    op.setVisible(true);
//here is me also trying showMessageDialog
//    JOptionPane.showMessageDialog(parent, jsp, title, msgType);
  private static void initOptionPaneComponents( JTextArea ta, JScrollPane sp )
          Color bg = UIManager.getColor( "Panel.background" );
          Dimension jspSz = new Dimension( 500, 75 );
          Dimension max = new Dimension( 500, 450 );
          ta.setMargin( new Insets( 5, 10, 10, 5 ) );
          ta.setColumns( 50 );
          ta.setLineWrap( true );
          ta.setWrapStyleWord( true );
          ta.setEditable( false );
          ta.setEnabled( false );
          ta.setBackground( bg );
          ta.setDisabledTextColor( Color.black );
          ta.setFont( UIManager.getLookAndFeelDefaults().getFont( "Dialog" ) );
          ta.setMaximumSize(max);
          ta.setMinimumSize(jspSz);
          sp.getViewport().add( ta );
     //sp.setPreferredSize( jspSz );
          sp.getViewport().setMaximumSize(max);
          sp.getViewport().setMinimumSize(jspSz);
          sp.setBorder( BorderFactory.createLoweredBevelBorder() );
          sp.setVerticalScrollBarPolicy( JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED );
     //     sp.setPreferredSize( jspSz );
       sp.setMinimumSize(jspSz);
          sp.setMaximumSize( max );
     }

When the JScrollPane is placed into the CENTER position the JOptionPane ignores the maxSize of both the JScrollPane and the JTextArea and stretches both of these out to 500x700 so that all text is visible. You still miss the basic concept.
The answer is it depends! First the size of the dialog needs to be determined.
If you use dialog.pack() then the preferred size of all components is used to determine the size of the dialog and the scrollpane will be whatever preferred size you gave it. If you leave the preferred size at (0, 0) then the scrollpane and text area will be invisible.
If you use dialog.setSize(???, ???), then the size of the scrollpane will be adjusted to fit the available space.
The amount of text in the text area is irrelevant in determining the size of the scrollpane. It is relevant in determining whether scrollbars will appear or not once the scrollpane is displayed.
If you need further help then you need to create a [Short, Self Contained, Compilable and Executable, Example Program (SSCCE)|http://homepage1.nifty.com/algafield/sscce.html], that demonstrates the incorrect behaviour.
Don't forget to use the [Code Formatting Tags|http://forum.java.sun.com/help.jspa?sec=formatting], so the posted code retains its original formatting.

Similar Messages

Maybe you are looking for