import java.awt.Component;
import java.awt.Dimension;
import java.awt.Insets;
import java.awt.Point;
import java.awt.Rectangle;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.table.*;
public class Test {
static class CommentHandler extends MouseAdapter {
private Popup popup;
private int col;
private int row;
private JTable table;
private JLabel label;
private Rectangle cell;
@Override
public void mouseClicked(MouseEvent e) {
if (popup != null) {
popup.hide();
popup = null;
}
init(e);
if (label == null) {
return;
}
String text = label.getText();
if (textFitsLabel(text)) {
return;
}
displayPopup(text);
}
private void init(MouseEvent e) {
label = null;
table = null;
if (!(e.getSource() instanceof JTable)) {
return;
}
table = (JTable) e.getSource();
col = table.columnAtPoint(e.getPoint());
row = table.rowAtPoint(e.getPoint());
Component renderer = table.prepareRenderer(
table.getCellRenderer(row, col), row, col);
if (!(renderer instanceof JLabel)) {
return;
}
label = (JLabel) renderer;
}
private boolean textFitsLabel(String text) {
cell = table.getCellRect(row, col, false);
Insets insets = label.getInsets();
Rectangle viewRect = new Rectangle(insets.left, insets.top,
cell.width - insets.left - insets.right,
cell.height - insets.top - insets.bottom);
String clipped = SwingUtilities.layoutCompoundLabel(
label,
table.getGraphics().getFontMetrics(label.getFont()),
text,
label.getIcon(),
label.getVerticalAlignment(),
label.getHorizontalAlignment(),
label.getVerticalTextPosition(),
label.getHorizontalTextPosition(),
viewRect,
new Rectangle(),
new Rectangle(),
label.getIconTextGap());
return clipped.equals(text);
}
private void displayPopup(String text) {
JTextArea fullText = new JTextArea(5,10);
fullText.setEditable(false);
fullText.setLineWrap(true);
fullText.setWrapStyleWord(true);
fullText.setText(text);
fullText.setCaretPosition(0);
JScrollPane scroller = new JScrollPane(fullText);
scroller.setPreferredSize(new Dimension(cell.width, fullText.getPreferredSize().height));
Point screen = new Point(cell.x, cell.y);
SwingUtilities.convertPointToScreen(screen, table);
popup = PopupFactory.getSharedInstance()
.getPopup(table, scroller, screen.x, screen.y);
popup.show();
}
}
static String LOREM =
"Lorem ipsum dolor sit amet, consetetur sadipscing elitr, " +
"sed diam nonumy eirmod tempor invidunt ut labore et dolore " +
"magna aliquyam erat, sed diam voluptua. At vero eos et " +
"accusam et justo duo dolores et ea rebum. Stet clita kasd " +
"gubergren, no sea takimata sanctus est Lorem ipsum dolor " +
"sit amet. Lorem ipsum dolor sit amet, consetetur sadipscing " +
"elitr, sed diam nonumy eirmod tempor invidunt ut labore et " +
"dolore magna aliquyam erat, sed diam voluptua. At vero eos " +
"et accusam et justo duo dolores et ea rebum. Stet clita kasd " +
"gubergren, no sea takimata sanctus est Lorem ipsum dolor sit";
public void run() {
JTable table = new JTable(new Object[][] {
{"Test", "Test", LOREM},
{LOREM, "Test", "Test"}
}, new Object[]{"A","B","C"});
table.addMouseListener(new CommentHandler());
JFrame frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
frame.add(new JScrollPane(table));
frame.setSize(800, 600);
frame.setVisible(true);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(() -> new Test().run());
}
}