【问题标题】:How to Clear JTable Cell on double mouse click如何在双击鼠标时清除 JTable 单元格
【发布时间】:2021-06-24 22:40:29
【问题描述】:

我希望在用户双击表格单元格时清除它。我设置为不可编辑的单元格我可以双击并使用 setValueAt 清除单元格。对于我拥有的可编辑单元格,我双击,我使用 setValueAt 清除它,TableModel 显示已清除的单元格,但 GUI 没有。当我移动到下一个单元格时,原始值保持不变。 我再次尝试了 settinbgviewportview 但没有奏效。似乎当它是一个可编辑的单元格时,您双击时,jTable 需要键盘输入。 下面是应该显示问题的代码。第一列不可编辑。其他三个是。当您双击第一列时,单元格将被清除。当您双击其他列时,单元格会获得焦点,但单元格不会在 GUI 中清除。

我错过了什么?

import java.awt.event.MouseEvent;


public class test extends javax.swing.JFrame {

   
    /**
     * Creates new form test
     */
    public test() {
        initComponents();
        jTable1.addMouseListener(new TableMouseListener());
      
    }
  

                            
    private void initComponents() {

        jScrollPane2 = new javax.swing.JScrollPane();
        jTable1 = new javax.swing.JTable();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        jTable1.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {"xxxxxx", "yyyyyy", "zzzzzz", "222222"}
            },
            new String [] {
                "Title 1", "Title 2", "Title 3", "Title 4"
            }
        ) {
            boolean[] canEdit = new boolean [] {
                false, true, true, true
            };

            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit [columnIndex];
            }
        });
        jScrollPane2.setViewportView(jTable1);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap(253, Short.MAX_VALUE)
                .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 375, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 275, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(14, Short.MAX_VALUE))
        );

        pack();
    }                     

    public static void main(String args[]) {

        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new test().setVisible(true);
            }
        });
    }
public class TableMouseListener implements java.awt.event.MouseListener {

        @Override
        public void mouseClicked(MouseEvent e) {
         
        }

        @Override
        public void mousePressed(MouseEvent e) {
        
        }

        @Override
        public void mouseReleased(MouseEvent e) {
            if (e.getClickCount()==2){
                javax.swing.JTable t=(javax.swing.JTable)e.getSource();
                int irow=t.getSelectedRow();
                int icol=t.getSelectedColumn();
                t.getModel().setValueAt("", irow, icol);
            }
          
        }

        @Override
        public void mouseEntered(MouseEvent e) {
//         
        }

        @Override
        public void mouseExited(MouseEvent e) {
          
        }
    }
    // Variables declaration - do not modify                     
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JTable jTable1;
    // End of variables declaration                   
}

【问题讨论】:

    标签: java swing jtable


    【解决方案1】:

    我错过了什么?

    当您双击可编辑单元格时,模型中的数据将被移动到编辑器中(在它被清除之前)。

    完成对单元格的编辑后,编辑器中的数据将保存到模型中。

    所以你需要在显示编辑器时清除编辑器中的数据(不是模型):

    jTable1 = new javax.swing.JTable()
    {
        @Override
        public boolean editCellAt(int row, int column, EventObject e)
        {
            boolean result = super.editCellAt(row, column, e);
            final Component editor = getEditorComponent();
    
            if (editor != null && editor instanceof JTextComponent)
            {
                ((JTextComponent)editor).setText("");
            }
    
            return result;
        }
    };
    

    【讨论】:

    • 是的,我想通了。感谢您快速回复!
    【解决方案2】:

    单击可编辑单元格后,您开始编辑其内容。要取消编辑,在您setValueAt 之后,您可以使用:

        jTable1.editingCanceled(new ChangeEvent(this));
    

    停止编辑并恢复模型中的值(新的空值)。

    【讨论】:

    • 谢谢。我实际上或多或少地想通了。我得到了编辑组件并在其中设置了文本。但是你这么快就提供了解决方案。
    猜你喜欢
    • 2011-08-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-04-24
    • 2015-01-17
    • 2012-11-28
    • 1970-01-01
    • 2016-06-10
    相关资源
    最近更新 更多