2011-10-14 94 views
2

我有一個JTable,我想通過在單元格內右鍵單擊來打開PopupMenu。我試圖通過製作自己的AbstractCellEditor,但它不起作用(當我開始我的程序,並且當第一個方法初始化CellEditor(這是該方法最後一件事)時,CellEditor開始。的下一個方法CellEditor已經消失)。有人能幫幫我嗎?我試圖找出幾個星期的錯誤。Jtable中單元格的行爲(單擊鼠標右鍵)

這是CellEditor代碼:

public class CellEditor extends AbstractCellEditor implements TableCellEditor { 

    JTextField component; 
    JPopupMenu popmen; 

    CellEditor(){ 
     component = new JTextField(); 
     popmen = new JPopupMenu(); 

     createPopmen(component); 
     component.addMouseListener(new MouseAdapter(){ 
      public void mouseReleased(MouseEvent e) { 
       if (e.isPopupTrigger()) 
         popmen.show(e.getComponent(), e.getX(), e.getY()); 
       } 
     }); 
    } 

    public Component getTableCellEditorComponent1(JTable table, Object value, 
      boolean isSelected, int rowIndex, int vColIndex) {  
     if (isSelected) {  
     } 

     component.setText((String)value); 
     return component; 
    } 

    public Object getCellEditorValue() { 
     return component.getText(); 
    } 

    @Override 
    public Component getTableCellEditorComponent(JTable arg0, Object arg1, boolean arg2,int arg3, int arg4) { 
     return null; 
    } 


public void createPopmen(final JTextField text){ 

    // creating Popmen 

} 

} 

這是代碼,在那裏的東西與我的表發生的部分:

Vector<Vector> nu = new Vector<Vector>(); 
Vector<String> columnNames = new Vector<String>(); 
TableModel model; 


model = new DefaultTableModel(nu, columnNames){ 
      public Class getColumnClass() { 
       Class returnValue; 
       if ((3 >= 0) && (3 < getColumnCount())) { 
       System.out.println(getValueAt(0, 3)); 
       returnValue = getValueAt(0, 3).getClass(); 
       } else { 
       returnValue = Object.class; 
       } 
       return returnValue; 
      }     
       public boolean isCellEditable(int row, int col) { 
        if(col == 0){ 
         return false; 
        } 
        else{ 
         return true; 
        } 

       } 
      }; 

    table = new JTable(model);  
    final TableRowSorter<TableModel> sorter = new TableRowSorter<TableModel>(model); 
    table.setRowSorter(sorter); 
    table.putClientProperty("terminateEditOnFocusLost", Boolean.TRUE); 
    table.getSelectionModel().addListSelectionListener(new ListSelectionListener(){ 
      public void valueChanged(ListSelectionEvent e){ 
       int a = table.getSelectedRow(); 
       if (!changes.contains(a) && a!=-1 && table.getSelectedColumn() != 0){ 
        changes.add(a); 
        saved = false; 
       } 
      } 
    }); 



    // SearchingBar 
    textfield.addKeyListener(new KeyListener(){ 

     @Override 
     public void keyPressed(KeyEvent arg0) { 

     } 
     @Override 
     public void keyReleased(KeyEvent arg0) { 

      String searchedText =textfield.getText();  
      if (searchedText.length() == 0) { 
       sorter.setRowFilter(null); 
       } else { 
       sorter.setRowFilter(RowFilter.regexFilter(searchedText)); 
       } 
     } 
     @Override 
     public void keyTyped(KeyEvent arg0) {  
     }  
    }); 

    ((DefaultTableModel) model).removeRow(0); 
    table.setModel(model); 
    table.setCellEditor(new CellEditor()); 

回答

2

不應該有任何需要創建一個自定義編輯。你應該能夠做這樣的事情:

JTable table = new JTable(...); 
DefaultCellEditor dce = table.getDefaultEditor(Object.class); 
Component editor = dce.getComponent(); 
editor.addMouseListener(...); 

此外,閱讀從Swing教程中的部分上Sorting and Filtering有關如何使用過濾的工作示例。你不應該使用KeyListener。

如果您需要更多的幫助,然後發佈您的SSCCE這表明問題。

+0

你是神^^ 非常感謝! – Bax995961

相關問題