现在我的代码里有个表格 表格的某列 做了个自定义组件 组件的前面是个JTextFiled 后面是个按钮
需要点击按钮的时候弹出文件选择对话框 还需要在点击这个单元格的时候有右键菜单弹出文件选择对话框 
我在表格和组件都加了鼠标监听 以弹出菜单
现在的问题是 如果点击这个单元格后 组件进入编辑状态 鼠标事件是被单元格触发的 那么e.getX()就是相对单元格的X了 那么怎么才能转化成相对表格的X呢? 我下面的代码是通过计算得出的相对表格的X 但是总是有偏差 而且放表格的Frame被改变大小后更不对了。 
我的计算是 先得到表格的总宽度 (我的组件是放在倒数第三列的) 然后计算最后两列的宽度
把总宽减最后两列 在加上相对于 单元格的e.getX()。。好象不怎么对
希望高手帮看一下 谢谢先~
/**
   *row 表格的行
   *p 坐标点
   *flag 触发方法的标志 inTableCell:代表是表格的单元格触发  inTable:代表是表格触发
   */
public void setFile(int row, Point p, int flag) {
    JPopupMenu SelectFile = new JPopupMenu();
    SelectFile.add(selectedFileMenu);
    if (loadTable.getSelectedRows().length < 2) {
      loadTable.setRowSelectionInterval(row, row);
    }    //计算表格倒数第二列的宽度
    int GUAGE_COL_width = loadTable.getColumnModel().getColumn(GUAGE_COL).getPreferredWidth();    //计算表格倒数第一列的宽度
    int STATUS_COL_width = loadTable.getColumnModel().getColumn(STATUS_COL).
        getPreferredWidth();     //FILE_COL 是我放自定义组件的列
        int showX = loadTable.getWidth() -
        loadTable.getColumnModel().getColumn(FILE_COL).getPreferredWidth() -
        GUAGE_COL_width - STATUS_COL_width + p.x ;    int showY = 0;
    if (row > 0) {
      showY = row * loadTable.getRowHeight() + p.y;
    }
    else {
      showY = p.y;
    }    //TableCell触发的setFile方法
    if (flag == inTableCell) {
      SelectFile.show(loadTable, showX, showY);
    }
    else
    if (flag == inTable) {    //单元格触发的方法
      SelectFile.show(loadTable, p.x, p.y);
    }    selectedFileMenu.setEnabled(true);    //选择的行
    int[] selectedRows = loadTable.getSelectedRows();    for (int i = 0; i < selectedRows.length; i++) {
        selectedFileMenu.setEnabled(false);
      }

解决方案 »

  1.   

    //单元格触发的方法
        if (flag == inTableCell) {
          SelectFile.show(loadTable, showX, showY);
        }
        else
        if (flag == inTable) {    //表格触发的方法
          SelectFile.show(loadTable, p.x, p.y);
        }
    注释有误
      

  2.   

    //选择的行
        int[] selectedRows = loadTable.getSelectedRows();    for (int i = 0; i < selectedRows.length; i++) {
            selectedFileMenu.setEnabled(false);
          }
    这段代码不用管 我删除错了东西
      

  3.   

    在jtable中有属性editingColumn和editingRow,可知道是哪个cell被选中
      

  4.   


    package javax.swing;public class SwingUtilitiesPoint javax.swing.SwingUtilities.convertPoint(Component source, Point aPoint, Component destination)Convert a aPoint in source coordinate system to destination coordinate system. If source>is null,aPoint is assumed to be in destination's root component coordinate system. If destinationis null, aPoint will be converted to source's root component coordinate system. If both source and destination are null, return aPoint without any conversion. 
      

  5.   

    ?奇怪 我的怎么没有成功?
    我在我的组件 编辑器里加了一个方法
    /**
       * 得到表格单元格编辑器组件
       * @return
       */
      public Component getTableCellEditorComponent(){
        return ta;  //JTextField
      }
    然后文件设置方法中写了下面代码
    //loadTable表格  使用行列得到单元格编辑器 TKFileSetEditor为自定义的编辑器类
    TKFileSetEditor editor = (TKFileSetEditor)loadTable.getCellEditor(row,FILEURL_COL);
            //得到表格单元格编辑器组件
            Component component = editor.getTableCellEditorComponent();
    Point convertPoint = javax.swing.SwingUtilities.convertPoint(component, p, loadTable);
              SelectFile.show(loadTable, convertPoint.x, convertPoint.y);
    但是我设了一下断点 看到X Y 位置不对?不知道为什么。。
    x:int= -191
    y:int=-126
    菜单每次都显示在表格外的JFrame左上角
      

  6.   

    同意 congliu(取次花丛都不顾,半缘修道半缘君。)
      

  7.   

    TKFileSetEditor editor = (TKFileSetEditor)loadTable.getCellEditor(row,FILEURL_COL);
    row 行和 column列是知道的啊。
    列是固定的 我得到单元格编辑器就是通过行和列得到的
      

  8.   

    我没试,但我猜想,
    第一个参数,因该是 MouseEvent 的 e.getSouce() 吧。看看getCellEditor怎么实现的:    public TableCellEditor getCellEditor(int row, int column) {
            TableColumn tableColumn = getColumnModel().getColumn(column);
            TableCellEditor editor = tableColumn.getCellEditor();
            if (editor == null) {
                editor = getDefaultEditor(getColumnClass(column));
            }
            return editor;
        }上当了吧,int row 根本没有用到。
    所以肯定不行啦。
      

  9.   

    啊??蚊子老大你的意思是getCellEditor(int row, int column) 方法根本木有使用到row?那怎么办?
      

  10.   

    对阿,木有使用到rowPoint javax.swing.SwingUtilities.convertPoint(Component source, Point aPoint, Component destination)
    的第一个参数,你传 MouseEvent 的 e.getSouce() 吧。
      

  11.   

    Point javax.swing.SwingUtilities.convertPoint(Component source, Point aPoint, Component destination)
    的第一个参数,我是通过 :
    在我的自定义组件 编辑器里加了一个方法
      /**
       * 得到单元格编辑器组件
       * @return
       */
      public Component getTableCellEditorComponent(){
        return ta;  //ta---JTextField    //得到我想要的组件 就是那个按钮前面的文本框
      }然后
    //loadTable表格  使用行列得到单元格编辑器 TKFileSetEditor为自定义的编辑器类
    TKFileSetEditor editor = (TKFileSetEditor)loadTable.getCellEditor(row,FILEURL_COL);
            //得到表格单元格编辑器组件
            Component component = editor.getTableCellEditorComponent();
    row我是通过在表格的得到表格编辑器得到的 因为好象得到表格编辑器的方法里有被编辑的行和列的信息
      

  12.   

    简直是忙晕了 现在要提交测试部了 今天竟然出现怪问题 和别人的代码一样竟然我的编译不通过
    老是报一个文件里的 JTable找不到 我晕 晕死 头都大了 55555555555555555555
    God save me~~~~~