简体   繁体   English

使用Cell渲染器获取Click JTable的位置

[英]Get position of Click JTable while using Cell renderer

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: 我目前正在使用Java中的JTable来显示大量的文本信息,因此使用以下代码实现了文本换行:

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: 但是,当执行此操作时,任何检测单击的单元格的尝试,只返回“-1”(越界)作为点击,我使用以下代码来检测点击位置:

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? 有没有什么方法,在保持文本包装的同时,我可以对JTable中单击的单元格进行文本化处理?

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 . 而不是MouseListener ,将TableModelListener添加到TableModel并更新相邻JTextComponentDocument模型。 In this related example , the TableModelListener updates the ListModel of an adjacent JList . 在此相关示例中TableModelListener更新相邻JListListModel

Alternatively, add a ListSelectionListener to your table's ListSelectionModel and update an adjacent component accordingly. 或者,将ListSelectionListener添加到表的ListSelectionModel并相应地更新相邻组件。 In this related example using SINGLE_SELECTION , the ListSelectionListener updates an adjacent JButton . 在使用SINGLE_SELECTION此相关示例中, ListSelectionListener更新相邻的JButton

Alternatively, look at this TablePopupEditor , which uses a JButton as a TableCellEditor . 或者,查看此TablePopupEditor ,它使用JButton作为TableCellEditor The button's ActionListener evokes a popup modal JDialog containing a JTextArea . 按钮的ActionListener唤起包含JTextArea的弹出模式JDialog

The following code gives the above functionality, (it ignores all wrapping cell renderers) table.addMouseListener(new java.awt.event.MouseAdapter() { 以下代码给出了上述功能,(它忽略了所有包装单元格渲染器)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
    }

  }
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM