Jtable中单元格的行为(在单元格内部单击鼠标右键) [英] Behavior of cells in Jtable (right click inside of a cell)

查看:113
本文介绍了Jtable中单元格的行为(在单元格内部单击鼠标右键)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个JTable,我想通过右键单击单元格内部来打开PopupMenu.我通过制作自己的AbstractCellEditor进行了尝试,但是它不起作用(当我启动程序时,当第一个方法初始化CellEditor时(这是该方法要做的最后一件事),CellEditor是但是在下一个方法的开头,CellEditor已经消失了.有人能帮帮我吗?我试图找出错误已有数周.

I have a JTable and I want to open a PopupMenu by a right-click inside a cell. I tried this by making my own AbstractCellEditor, but it doesn't work (When I start my program and when the first method initialize the CellEditor (it´s the last thing that this method do), CellEditor is starting. But at the beginning of the next method the CellEditor has already disapeared). Can somebody please help me? I´m trying to find the error for weeks.

这是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

}

}

这是代码的一部分,我的表正在发生某些事情:

This is the part of the code, where something is happening with my table:

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

推荐答案

应该无需创建自定义编辑器.您应该能够执行以下操作:

There should be no need to create a custom editor. You should be able to do something like:

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

另外,请阅读Swing教程中排序和过滤,获取有关如何使用过滤的有效示例.您不应该使用KeyListener.

Also, read the section from the Swing tutorial on Sorting and Filtering for a working example of how to use filtering. You should not be using a KeyListener.

如果您需要更多帮助,请发布您的 SSCCE 来说明问题.

If you need more help then post your SSCCE which demonstrates the problem.

这篇关于Jtable中单元格的行为(在单元格内部单击鼠标右键)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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