1

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?

mKorbel
  • 109,525
  • 20
  • 134
  • 319
kxdan
  • 187
  • 1
  • 15
  • `table.addMouseListener(..` Use [`table.getSelectionModel().addListSelectionListener(ListSelectionListener)`](http://docs.oracle.com/javase/8/docs/api/javax/swing/ListSelectionModel.html#addListSelectionListener-javax.swing.event.ListSelectionListener-) instead to make life much simpler. *"I'm currently using a JTable in Java to display a large amount of text information"* That's really not what a table component is designed for, or particularly good at.. – Andrew Thompson May 23 '15 at 18:02
  • @AndrewThompson Agreed, but my current situation requires a large amount of text information to be displayed next to it's relevant data, using a row from a JTable seemed a relevant option. – kxdan May 23 '15 at 18:05
  • Your code doesnt wrap fields to me: http://pastebin.com/ARLvN3bR – kukis May 23 '15 at 18:34
  • There is also an additional section where you apply the wrapper to the model – kxdan May 23 '15 at 19:41

2 Answers2

3

My current situation requires a large amount of text information to be displayed next to [the] relevant data

Instead of a MouseListener, add a TableModelListener to your TableModel and update the Document model of an adjacent JTextComponent. In this related example, the TableModelListener updates the ListModel of an adjacent JList.

Alternatively, add a ListSelectionListener to your table's ListSelectionModel and update an adjacent component accordingly. In this related example using SINGLE_SELECTION, the ListSelectionListener updates an adjacent JButton.

Alternatively, look at this TablePopupEditor, which uses a JButton as a TableCellEditor. The button's ActionListener evokes a popup modal JDialog containing a JTextArea.

Community
  • 1
  • 1
trashgod
  • 203,806
  • 29
  • 246
  • 1,045
0

The following code gives the above functionality, (it ignores all wrapping cell renderers) table.addMouseListener(new java.awt.event.MouseAdapter() {

  public void mouseClicked(java.awt.event.MouseEvent e) {
    JTable target = (JTable)e.getSource();
    int row = target.getSelectedRow();
    int column = target.getSelectedColumn();
    if((row >=0) && (column >=0)){
      //Stuff
    }

  }
});
kxdan
  • 187
  • 1
  • 15