Note that there are some explanatory texts on larger screens.

plurals
  1. POcropping text lines in a jtextpane-based jtable cell renderer
    primarykey
    data
    text
    <p>i'm using a JTextPane as a cell renderer in my table (so i can control color, font, size and links easily). the problem is that lines get wrapped when the cell's get too small to contain the full text.</p> <p>i know the number of expected text lines in advance (or i can just count), so i set the row's height accordingly.<br></p> <p>how do i get the lines to crop (visually! i.e. in the middle of a letter) at the cell end?</p> <p>thanks, asaf :-) <p> <strong>more info:</strong> i've tried two solutions i found on the net. one that involves <a href="http://java-sl.com/wrap.html" rel="nofollow noreferrer">setting my own EditroKit</a> . the other is listed below, and involves overriding of setSize().<br> alas, none worked...</p> <p>here is my renderer (apologies for messed up indentation...):</p> <pre><code>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 &lt; 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 &amp;&amp; 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; } } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload