使用Cell渲染器时获取Click JTable的位置 [英] Get position of Click JTable while using Cell renderer

查看:102
本文介绍了使用Cell渲染器时获取Click JTable的位置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在使用Java中的JTable来显示大量文本信息,因此已经使用以下代码实现了文本环绕:

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;
}
} 

但是,实现此功能后,任何检测被点击单元格的尝试都只是返回"-1"(越界)作为点击点,我正在使用以下代码来检测点击位置:

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() );
  }
});
}

在保持文本换行的同时,有什么方法可以使在JTable中单击的单元格发短信吗?

Is there any way, whilst maintaining the text wrapping, that I can text the cell which is clicked in the JTable?

推荐答案

我目前的情况要求在[相关数据]旁边显示大量文本信息

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

添加 TableModelListener TableModel并更新相邻示例中,TableModelListener更新了相邻JListListModel.

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.

或者,在您的计算机上添加 ListSelectionListener 表的ListSelectionModel并相应地更新相邻组件.在这个使用SINGLE_SELECTION的相关示例中,ListSelectionListener更新了相邻的JButton.

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.

或者,请查看此 TablePopupEditor ,它使用JButton作为TableCellEditor.按钮的ActionListener会唤起包含JTextArea的弹出模式JDialog.

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

这篇关于使用Cell渲染器时获取Click JTable的位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆