如何在编辑时更改JTable单元格文本?

时间:2017-03-15 08:52:04

标签: java swing jtable

我想要达到的效果是:每当用户开始编辑表格中的单元格时,我希望单元格的文本可以删除原来的任何文本('开始编辑'部分),如果没有添加新文本,只需将文本设置为原始文本('停止编辑')

在我看来,我认为setValueAt会更改当前正在编辑的单元格中的文本是错误的 - 相反,它似乎只会在编辑完成后更改单元格的值(这意味着我可能不会需要我放在'停止编辑之后的代码

但是,如果确实如此,我不知道如何编辑当前正在编辑的单元格中的文本,所以这是我的问题:如何更改文本中的文本当前正在编辑的单元格?

例如,制作它以便在我开始编辑'测试bb时,'单元格变为没有文字:

enter image description here

这是我的代码:

 private void tblEntriesPropertyChange(java.beans.PropertyChangeEvent evt) {                                          
    swb.tablePropertyChange(tblEntries, evt);
}                 
public void tablePropertyChange(JTable jtable, PropertyChangeEvent evt) {
    TableModel mdl = jtable.getModel();
    int c = jtable.getSelectedColumn();
    int r = jtable.getSelectedRow();
    if (r >= 0 && c >= 0) {
        String text = mdl.getValueAt(r, c) + "";
        if (evt.getNewValue() == null) {
            System.out.println("Stop Editing");
            String newText = mdl.getValueAt(r, c) + "";
            if (newText.length() <= 0) {
                jtable.setValueAt(text, r, c); // also tried: mdl.setValueAt(text, r, c);
            }
        } else {
            System.out.println("Start Editing");
            jtable.setValueAt("", r, c); // also tried: mdl.setValueAt("", r, c);
        }
    }
}

1 个答案:

答案 0 :(得分:3)

试图&#34;介入&#34;在这种过程中可能是一个真正的伎俩。你有两个基本选择。您可以创建自定义JTable实现,并确保将null之类的内容传递给任何编辑器变为活动状态,这会将您锁定为单次使用JTable,或者您可以提供自己的编辑器。

public class CleanCellEditor extends DefaultCellEditor {

    public static CleanCellEditor make() {
        JTextField field = new JTextField();
        field.setBorder(null);
        System.out.println("make");

        return new CleanCellEditor(field);
    }

    public CleanCellEditor(JTextField textField) {
        super(textField);
    }

    @Override
    public Component getTableCellEditorComponent(JTable table, Object value, boolean isSelected, int row, int column) {
        return super.getTableCellEditorComponent(table, null, isSelected, row, column);
    }

}

虽然这稍微好一点,但是你可以将它应用于JTable的任何实现,但它确实意味着您需要为表可能需要支持的所有可能值/类型创建一个

作为一个基本的例子......

import java.awt.Component;
import java.awt.EventQueue;
import java.util.Vector;
import javax.swing.AbstractCellEditor;
import javax.swing.DefaultCellEditor;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import javax.swing.border.EmptyBorder;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableCellEditor;
import javax.swing.table.TableColumn;
import javax.swing.table.TableColumnModel;

public class Test {

    public static void main(String[] args) {
        new Test();
    }

    public Test() {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
                    ex.printStackTrace();
                }

                DefaultTableModel model = new DefaultTableModel(new Object[]{"A", "B", "C", "D"}, 0);
                for (int row = 0; row < 10; row++) {
                    Vector data = new Vector(model.getColumnCount());
                    for (int col = 0; col < model.getColumnCount(); col++) {
                        data.add(row + "x" + col);
                    }
                    model.addRow(data);
                }

                JTable table = new JTable(model);
                table.setDefaultEditor(Object.class, CleanCellEditor.make());

                JFrame frame = new JFrame("Testing");
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.add(new JScrollPane(table));
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }
        });
    }

    public static class CleanCellEditor extends DefaultCellEditor {

        public static CleanCellEditor make() {
            JTextField field = new JTextField();
            field.setBorder(null);
            System.out.println("make");

            return new CleanCellEditor(field);
        }

        public CleanCellEditor(JTextField textField) {
            super(textField);
        }

        @Override
        public Component getTableCellEditorComponent(JTable table, Object value, boolean isSelected, int row, int column) {
            return super.getTableCellEditorComponent(table, null, isSelected, row, column);
        }

    }

}