Trimming text strings in jextpane-jery-render-renderer

I use JTextPane as a means of displaying cells in my table (so that I can easily control color, font, size and links). the problem is that the lines are wrapped when the cell gets too small to contain the full text.

I know the number of expected text lines in advance (or I can just count), so I set the line height accordingly.

How do I get the lines to trim (visually, that is, in the middle of the letter) at the end of the cell?

thanks, asaf :-)

Additional Information: I tried two solutions that I found on the network. one that includes installing my own EditroKit , the other is below and includes an override of setSize ().
alas, no one worked ...

here is my renderer (apologies for the spoiled indentation ...):

package textTable;
import java.awt.BorderLayout;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.FontMetrics;
import java.awt.Insets;

import javax.swing.JPanel;
import javax.swing.JTable;
import javax.swing.JTextPane;
import javax.swing.table.TableCellRenderer;
import javax.swing.text.BadLocationException;
import javax.swing.text.SimpleAttributeSet;
import javax.swing.text.StyleConstants;
import javax.swing.text.StyledDocument;

public class LogRenderer extends JPanel implements TableCellRenderer {
    static class NoWrapTextPane extends JTextPane {
        public NoWrapTextPane () {
            super();
        }
        @Override
        public boolean getScrollableTracksViewportWidth() {
            return false;
        }
        @Override
        public void setSize(Dimension d) {
            if(d.width < getParent().getSize().width) {
                d.width = getParent().getSize().width;
            }
            super.setSize(d);
        }
    }

    private JTextPane textPane = new NoWrapTextPane();//JTextPane();
    private StyledDocument doc = textPane.getStyledDocument();
    private SimpleAttributeSet attr = new SimpleAttributeSet();
    private FontMetrics fontMetrics;

    public LogRenderer() {
    textPane.setMargin(new Insets(0,0,0,0));

        fontMetrics = textPane.getFontMetrics(getFont());

        StyleConstants.setFontFamily(attr, "monospaced");//"Courier"

    setLayout(new BorderLayout());
    add(textPane,BorderLayout.CENTER);
}

public Component getTableCellRendererComponent(JTable table,
        Object value, boolean isSelected, boolean hasFocus, int row,
        int column) {
        String[] text;
        if (value != null && value instanceof String[]) {
            text = (String[]) value;
        } else {
            text = null;
        }
        try {
            doc.remove(0, doc.getLength());
            if (text != null) {
                int offset = 0;
                for (String line : text) {
                    doc.insertString(offset, line+"\n", attr);
                    offset += (line == null ? 0 : line.length()) +1;
                }
                int preferredHeight = fontMetrics.getHeight() *     text.length;
                if (preferredHeight != table.getRowHeight(row)) {
                    System.out.println("preferredHeight     "+preferredHeight+" = fontMetrics.getHeight() "+fontMetrics.getHeight()+" + text.length     "+text.length);
                    table.setRowHeight(row, preferredHeight);
                } 
            }
        } catch (BadLocationException e) {
            e.printStackTrace();
        }
//      textPane.setToolTipText("hi");
        return this;
    }   
}
+3
source share
1 answer

Use custom renderer . Expansion JLabelshould give you a character with an ellipsis to indicate truncation, but you can use JPaneland this approach to crop the result.

: WrapEditorKit, , , , , . LogRenderer, , JEditorPane . :

private static class LogRenderer
    extends JEditorPane implements TableCellRenderer {

    public LogRenderer() {
        this.setEditorKit(new WrapEditorKit());
    }

    public Component getListCellRendererComponent(
        JList list, Object value, int index,
        boolean isSelected, boolean cellHasFocus) {
        this.setText((String) value);
        return this;
    }

    @Override
    public Component getTableCellRendererComponent(
        JTable table, Object value, boolean isSelected,
        boolean hasFocus, int row, int column) {
        this.setText((String) value);
        return this;
    }
}
+3

Source: https://habr.com/ru/post/1730052/


All Articles