Re: Layout labels with variable text length
Simon wrote:
Hi,
assume I have a multiline JLabel with HTML text placed somewhere in a
GridBagLayout in a dialog. If I don't specify the size of the label,
I've been looking all over for this, and finally located it again. If
you use a JOptionPane, there's a method, getMaxCharactersPerLineCount(),
that you can override to limit the width of the dialog. It may not work
100% for you, but I think it's interesting and might work in some
circumstances.
package fubar;
import javax.swing.JOptionPane;
import javax.swing.SwingUtilities;
public class DialogLabelTest {
private static final String MESSAGE =
"This is a rather long message which I have made so that you "+
"can see the effect of the getMaxCharactersPerLineCount() "+
"method when it is used in a JOptionPane. Whew!";
public static void main( String[] args )
{
SwingUtilities.invokeLater( new Runnable() {
public void run() {
createAndShowGui();
}
} );
}
private static void createAndShowGui() {
JOptionPane normal = new JOptionPane( MESSAGE );
normal.createDialog( "Normal" ).setVisible( true );
JOptionPane limited = new LimitedWidthOptionPane( 40 );
limited.setMessage( MESSAGE );
limited.createDialog( "Limited" ).setVisible( true );
}
}
class LimitedWidthOptionPane extends JOptionPane {
private final int width; // width in characters
public LimitedWidthOptionPane( int i ) {
width = i;
}
@Override
public int getMaxCharactersPerLineCount() {
return width;
}
}