Title: shorten text in JTextAre with ...

Hi,

I've got a JList with a custom renderer. I want this renderer to display 2 lines of text. If the text is more then what these 2 lines can display, then I want to break off the text and show "..." at the end. I'm doing this as follows now (see below).

But I want to ask if there can be some improvement in this code, because I think it is higly unoptimised. Especially that while condition is bugging me. Does anybody have a better idea? Also I wonder why I need to subtract 50 from twice the width, otherwise the string I got is still too long?

regards,

Wim


private static class MyListCellRenderer extends DefaultListCellRenderer
{
        public Component getListCellRendererComponent(
                        JList list,
                        Object value,
                        int index,
                        boolean isSelected,
                        boolean cellHasFocus )
        {
                JLabel label = (JLabel)super.getListCellRendererComponent( list, value, index, isSelected, cellHasFocus );

                String text = label.getText();
                JTextArea textArea = new JTextArea(text);
                textArea.setEditable(false);
                textArea.setLineWrap(true);
                textArea.setWrapStyleWord(true);
                textArea.setBackground(label.getBackground());
                textArea.setForeground(label.getForeground());
                // Set size twice a normal label to have 2 lines of text
                Dimension size = new Dimension(400, label.getPreferredSize().height * 2);
                textArea.setPreferredSize(size);
                textArea.setMinimumSize(size);
                textArea.setMaximumSize(size);

                FontMetrics fontMetrics = getFontMetrics(textArea.getFont());

                int width = fontMetrics.stringWidth(text);
                if( width > 2 * size.width)
                {
                        // The text width is more then the JTextArea can display in 2 lines
                        int i = 5;
                        String choppedText = text.substring(0, i);
                        // Search the string that just can be displayed
                        while( fontMetrics.stringWidth(choppedText) < (2 * size.width) - 50)
                        {
                                choppedText = text.substring(0, i);
                                i++;
                        }
                        String newText = choppedText + "...";
                        textArea.setText(newText );
                }

                textArea.setBorder( label.getBorder() );
                return textArea;
        }
}

-------------
Ing. Wim Deblauwe
Software Development Engineer
Medical Imaging Systems - BarcoView
- - - - - - - - DISCLAIMER - - - - - - - -
Unless indicated otherwise, the information contained in this message is privileged and confidential, and is intended only for the use of the addressee(s) named above and others who have been specifically authorized to receive it. If you are not the intended recipient, you are hereby notified that any dissemination, distribution or copying of this message and/or attachments is strictly prohibited. The company accepts no liability for any damage caused by any virus transmitted by this email. Furthermore, the company does not warrant a proper and complete transmission of this information, nor does it accept liability for any delays. If you have received this message in error, please contact the sender and delete the message. Thank you.

Reply via email to