简体   繁体   English

如何清除 JTable 中选定的单元格?

[英]How to clear a selected cell in JTable?

I can select and set focus to cells in a JTable by clicking it, now I want to change the value on the focused cell.我可以 select 并通过单击将焦点设置到JTable中的单元格,现在我想更改焦点单元格上的值。 In order to change the value of it, I have to double click?为了改变它的值,我必须双击? Is there any way to clear/change the value of that focused cell (by single-clicking)?有什么方法可以清除/更改该聚焦单元格的值(通过单击)?

I have tried jTable1.setValueAt("", row, column);我试过jTable1.setValueAt("", row, column); , this clears the value in the background(It's not updated in the GUI/Same old value appears in the cell). ,这会清除背景中的值(它不会在 GUI 中更新/相同的旧值出现在单元格中)。

Table structure:表结构:

表结构

jTable1.addMouseListener(new MouseAdapter(){
    @Override
    public void mouseClicked(final MouseEvent e) {
        if (e.getClickCount()==1){
            final JTable jTable=(JTable)e.getSource();
            final int row = jTable.getSelectedRow();
            final int column = jTable.getSelectedColumn();
            jTable1.editCellAt(row,column);
            jTable1.getEditorComponent().requestFocus();
            final Double valueInCell = (Double)jTable.getValueAt(row, column);
            System.out.println(valueInCell);
        }
    }
});

If all you desire is to clear or change a JTable cell that holds Strings, why not simply call JTable's setValueAt(Object o, int row, int column) method?如果您只想清除或更改包含字符串的 JTable 单元格,为什么不简单地调用 JTable 的setValueAt(Object o, int row, int column)方法? To clear, pass in "" , to set to something different, then pass in a different String.要清除,请传入"" ,设置为不同的值,然后传入不同的字符串。

For example, my minimal reproducible example :例如,我最小的可重现示例

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.event.*;
import java.util.Arrays;
import java.util.Vector;
import java.util.Random;

import javax.swing.*;
import javax.swing.table.DefaultTableModel;

@SuppressWarnings("serial")
public class TableFoo extends JPanel {
   private static final Vector<String> COLUMN_NAME_VECTOR = new Vector<String>(
         Arrays.asList(new String[] { "A", "B", "C" }));
   private static final int COLUMN_COUNT = COLUMN_NAME_VECTOR.size();
   private JTable table = new JTable();

   public TableFoo() {
      table.setFillsViewportHeight(true);
      fillTableData();
      MyMouse myMouse = new MyMouse();
      table.addMouseListener(myMouse);

      setLayout(new BorderLayout());
      add(new JScrollPane(table), BorderLayout.CENTER);
   }

   class MyMouse extends MouseAdapter {
       @Override
       public void mousePressed(final MouseEvent e) {
            final int row = table.getSelectedRow();
            final int column = table.getSelectedColumn();
            table.setValueAt("", row, column);
       }

   }

   public void fillTableData() {
      Vector<Vector<String>> matrix = new Vector<Vector<String>>();
      int rowCount = 8;
      for (int i = 0; i < rowCount ; i++) {
         Vector<String> row = new Vector<String>();
         for (int j = 0; j < COLUMN_COUNT; j++) {
            String rowText = String.format("row %d col %d", i, j);
            row.add(rowText );
         }
         matrix.add(row);
      }
      DefaultTableModel model = new DefaultTableModel(matrix,
            COLUMN_NAME_VECTOR);
      table.setModel(model);
   }

   private static void createAndShowGui() {
      TableFoo mainPanel = new TableFoo();

      JFrame frame = new JFrame("TableFoo");
      frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      frame.getContentPane().add(mainPanel);
      frame.pack();
      frame.setLocationByPlatform(true);
      frame.setVisible(true);
   }

   public static void main(String[] args) {
      SwingUtilities.invokeLater(new Runnable() {
         public void run() {
            createAndShowGui();
         }
      });
   }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM