将鼠标侦听器添加到表模型类

时间:2014-08-21 09:43:15

标签: java swing netbeans tablemodel

我正在使用Java和NetBeans 8.0 IDE开发桌面应用程序  有JTable使用扩展AbstractTableModel的自定义表模型。是否可以通过实现MouseListener接口在表模型类中实现鼠标侦听器?

我试过这样做,但看起来好像现在被困住了。而且我不想使用JTable类。

任何建议。表模型的代码片段如下所示。

   public class CourseTableModel  extends AbstractTableModel implements TableModelListener , 
   MouseListener {

  private List<List<Object>> dataList = new ArrayList<>();
  private String[] header = { "ID","COURSE","YEAR GRADUATED "};

 private int minRowCount = 5;
 public Object rawData = null;
 Object[]  cl = null;

 public CourseTableModel()
 { super(); }

 public List<List<Object>> getDataList() {
    return dataList;
 }

 public void setDataList(List<List<Object>> dataList) {  
    this.dataList = dataList;
    fireTableDataChanged();       
    fireTableStructureChanged();        
 }

 @Override
 public int getRowCount() {
    return Math.max(minRowCount, dataList.size());
 }

 @Override
 public int getColumnCount() {
    return header.length;
 }

  public void setHeader(String[] header) {
    this.header = header;
  }

  public String[] getHeader() {
    return header;
  }

  @Override
  public Object getValueAt(int row, int col) {
    if(row < dataList.size())      
    {rawData = dataList.get(row).get(col);}
    return rawData;
   }

  @Override
  public void setValueAt(Object value, int row, int col) {
    int x = 0;
    for(List<Object> lo : dataList)
    {  
    if(x == row)
        lo.set(col, value);
    }   
    fireTableCellUpdated(row, col);
   }

    @Override
  public String getColumnName(int col) {
  return header[col];
  }

   @Override
   public Class<?> getColumnClass(int column)
    {
        for (int row = 0; row < getRowCount(); row++)
        {   
            Object o = getValueAt(row, column);

            if (o != null)
            {
                return o.getClass();
            }              
        }
        return Object.class;
    }

    @Override
    public boolean isCellEditable(int row, int col) {
    return col != 1;
    }

  @Override
   public void tableChanged(TableModelEvent e) {
    int row = e.getFirstRow();
    int column = e.getColumn();
    TableModel model = (TableModel)e.getSource();
    String columnName = model.getColumnName(column);
    Object data = model.getValueAt(row, column);
    System.out.println(" this is cell value " + data);
   }

   public void fillCourseDialogue()
   {       cl = new Object[dataList.get(0).size()];
        int x = 0;        

        for (List<Object> dl : dataList) {           
         cl[x] = dl.get(1);
         x++;
      }
      }

        @Override
     public void mouseClicked(MouseEvent e) {
    if (e.getClickCount() == 2) {  
    JTable target = (JTable)e.getSource();
    int row = target.getSelectedRow();
    int col = target.getSelectedColumn();
    if(row > -1 && col > 0)
    {   
        JOptionPane.showInputDialog(null, "SELECT A COURSE","COURSE",JOptionPane.PLAIN_MESSAGE ,
                                   null,cl, cl[1]);
      }     
       }  
        }                    

     @Override
     public void mousePressed(MouseEvent e) {
      throw new UnsupportedOperationException("Not supported yet."); //To change body of  
     generated
      methods, choose Tools | Templates.
      }

    @Override
    public void mouseReleased(MouseEvent e) {
    throw new UnsupportedOperationException("Not supported yet."); //To change body of generated
    methods, choose Tools | Templates.
      }

      @Override
     public void mouseEntered(MouseEvent e) {
    throw new UnsupportedOperationException("Not supported yet."); //To change body of generated
     methods, choose Tools | Templates.
     }

     @Override
     public void mouseExited(MouseEvent e) {
    throw new UnsupportedOperationException("Not supported yet."); //To change body of generated 
      methods, choose Tools | Templates.
     }          
     }

0 个答案:

没有答案