-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathLeftDotRenderer.java
More file actions
50 lines (41 loc) · 1.43 KB
/
LeftDotRenderer.java
File metadata and controls
50 lines (41 loc) · 1.43 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
import java.awt.*;
import javax.swing.*;
import javax.swing.table.*;
/*
* Simple text renderer that will display the text right justified with
* leading dots when the column width is not large enough to display the
* entire text.
*/
class LeftDotRenderer extends DefaultTableCellRenderer
{
public Component getTableCellRendererComponent(
JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column)
{
super.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column);
// Determine the width available to render the text
int availableWidth = table.getColumnModel().getColumn(column).getWidth();
availableWidth -= table.getIntercellSpacing().getWidth();
Insets borderInsets = getBorder().getBorderInsets((Component)this);
availableWidth -= (borderInsets.left + borderInsets.right);
String cellText = getText();
FontMetrics fm = getFontMetrics( getFont() );
// Not enough space so start rendering from the end of the string
// until all the space is used up
if (fm.stringWidth(cellText) > availableWidth)
{
String dots = "...";
int textWidth = fm.stringWidth( dots );
int i = cellText.length() - 1;
for (; i > 0; i--)
{
textWidth += fm.charWidth(cellText.charAt(i));
if (textWidth > availableWidth)
{
break;
}
}
setText( dots + cellText.substring(i + 1) );
}
return this;
}
}