This existing question asks for left cutoff in a left-aligned column. This question is about left cutoff in a right-aligned column.
Asked
Active
Viewed 88 times
0
-
A better user experience would be to show the entire long String in a [tool tip](https://docs.oracle.com/javase/tutorial/uiswing/components/tooltip.html) on the JTable cell. – Gilbert Le Blanc Sep 13 '21 at 12:52
-
@GilbertLeBlanc better than what? i agree that a tooltip would enhance the user experience further. – Reto Höhener Sep 13 '21 at 12:54
-
Better than just showing the last part of the String in the JTable cell. – Gilbert Le Blanc Sep 13 '21 at 12:57
-
@GilbertLeBlanc I believe that if the column is narrower than the content, there is no other option but to show only part of the content. – Reto Höhener Sep 13 '21 at 12:59
-
Yes, a tooltip should still be available to show the entire text if the user wants to see it. However the point of this question is how to best display the text when it is too large to see the entire text by default in the column width. Sometimes the text at the right is more relevant than the text at the left. For example a list of files in a directory. The individual file names would be more relevant the see the same directory name. See: [Left Dot Renderer](https://tips4java.wordpress.com/2008/11/12/left-dot-renderer/) for a renderer with dots on the left when text is truncated. – camickr Sep 13 '21 at 13:34
-
@camickr This question is about how to have a right-aligned column cut off at the left. Your nice renderer (and the existing question I linked) are about left-aligned columns. The question about how to best display large content has no general answer, but depends on your use case. – Reto Höhener Sep 13 '21 at 13:41
-
Excel for example doesn't show numeric content at all when it doesn't fit, but overflows or cuts off right-aligned text on the left side. – Reto Höhener Sep 13 '21 at 13:47
-
@RetoHöhener *Your nice renderer are about left-aligned columns.* - no, it is about displaying "..." at the left if the text is truncated. Yes, the example is for left aligned text. If you want to have the column right aligned then change the alignment the same way you do in your example. – camickr Sep 13 '21 at 23:28
1 Answers
0
Here is an idea that does not generate an ellipsis on the left side, but I found that I prefer an immediate cut off anyway, to show as much information as possible.
import java.awt.BorderLayout;
import java.awt.Graphics;
import java.awt.Graphics2D;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.SwingConstants;
import javax.swing.plaf.basic.BasicLabelUI;
import javax.swing.table.DefaultTableCellRenderer;
import javax.swing.table.DefaultTableModel;
public class TableColumnRightAlignedLeftCutoff
{
public static void main(String[] args)
{
DefaultTableModel model = new DefaultTableModel(new String[] { "default", "custom" }, 0);
model.addRow(new String[] { "short", "short" });
model.addRow(new String[] { "long long long long long", "long long long long long" });
JTable table = new JTable(model);
table.setShowGrid(false);
table.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
DefaultTableCellRenderer defaultRenderer = new DefaultTableCellRenderer();
defaultRenderer.setHorizontalAlignment(SwingConstants.RIGHT);
table.getColumn("default").setCellRenderer(defaultRenderer);
table.getColumn("custom").setCellRenderer(new RightAlignedRenderer());
JFrame frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getRootPane().setLayout(new BorderLayout());
frame.getRootPane().add(new JScrollPane(table), BorderLayout.CENTER);
frame.setSize(600, 400);
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
private static class RightAlignedRenderer extends DefaultTableCellRenderer
{
private int m_currentCellWidth = 0;
public RightAlignedRenderer()
{
setUI(new RightAlignedRendererUI());
setHorizontalAlignment(SwingConstants.RIGHT);
setOpaque(false);
}
@Override
public void setBounds(int x, int y, int width, int height)
{
m_currentCellWidth = width;
// make the label think that it has a lot of space to layout the text
super.setBounds(x, y, 1000, height);
}
private class RightAlignedRendererUI extends BasicLabelUI
{
@Override
public void paint(Graphics g, JComponent c)
{
((Graphics2D) g).translate(-1000 + m_currentCellWidth, 0);
super.paint(g, c);
}
}
}
}

Reto Höhener
- 5,419
- 4
- 39
- 79