I'm currently using a JTable in Java to display a large amount of text information, and as such have implemented text wrapping, using the following code:
MyCellRenderer mcr = new MyCellRenderer();
table.getColumnModel().getColumn(0).setCellRenderer(mcr);
class MyCellRenderer extends JTextArea implements TableCellRenderer {
public MyCellRenderer() {
setLineWrap(true);
setWrapStyleWord(true);
}
public Component getTableCellRendererComponent(JTable table, Object
value, boolean isSelected, boolean hasFocus, int row, int column) {
setText(value.toString());
setSize(table.getColumnModel().getColumn(column).getWidth(),
getPreferredSize().height);
if (table.getRowHeight(row) != getPreferredSize().height) {
table.setRowHeight(row, getPreferredSize().height);
}
return this;
}
}
However, when this is implemented, any attempt to detect the cell which is clicked, simply returns "-1" (out of bounds) as the point of click, I am using the following code to detect the click location:
table.addMouseListener(new java.awt.event.MouseAdapter() {
public void mouseClicked(java.awt.event.MouseEvent e) {
int row = table.rowAtPoint( e.getPoint() );
int column = table.columnAtPoint( e.getPoint() );
}
});
}
Is there any way, whilst maintaining the text wrapping, that I can text the cell which is clicked in the JTable?
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…