2

我已经将其中一列的单元格编辑器设置为 JSpinner 并且它可以工作,但是当数据模型中的数据发生更改时,我之前启用的编辑器仍然处于启用状态,它显示旧行的值(它不存在或者它在更改的数据中位于不同的位置)。

如何在数据更改时使所选单元格的编辑器消失并保存输入的值?

4

1 回答 1

3

这可能是您的SSCCE的好代码

在此处输入图像描述

import java.awt.*;
import java.util.EventObject;
import javax.swing.*;
import javax.swing.border.*;
import javax.swing.table.*;

public class SpinnerColumn extends AbstractCellEditor implements TableCellEditor, TableCellRenderer {

    private static final long serialVersionUID = 1L;
    private JSpinner editSpinner, renderSpinner;
    private JTable table;
    private String[] list;
    private Border originalBorder;

    public SpinnerColumn(JTable table, int column) {
        editSpinner = new JSpinner();
        renderSpinner = new JSpinner();
        originalBorder = editSpinner.getBorder();
        editSpinner.setBorder(new LineBorder(Color.BLUE));
        this.table = table;
        table.getColumnModel().getColumn(column).setCellEditor(this);
    }

    public SpinnerColumn(String[] list, JTable table, int column) {
        editSpinner = new JSpinner();
        editSpinner.setModel(new SpinnerListModel(list));
        renderSpinner = new JSpinner();
        originalBorder = editSpinner.getBorder();
        editSpinner.setBorder(new LineBorder(Color.BLUE));
        this.list = list;
        this.table = table;
        table.getColumnModel().getColumn(column).setCellEditor(this);
    }

    @Override
    public Object getCellEditorValue() {
        return editSpinner.getValue();
    }

    @Override
    public Component getTableCellEditorComponent(JTable table, Object value, boolean isSelected,
            int row, int column) {
        if (list != null) {
            editSpinner.setValue(list[0]);
        } else {
            editSpinner.setValue(0);
        }
        if (value != null) {
            editSpinner.setValue(value);
        }
        return editSpinner;
    }

    @Override
    public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected,
            boolean hasFocus, int row, int column) {
        if (hasFocus) {
            renderSpinner.setBorder(new LineBorder(Color.BLUE));
        } else {
            renderSpinner.setBorder(originalBorder);
        }// *** here's where we set the spinner's value
        if (value == null) {
            renderSpinner.setValue(0);
        } else {
            int intValue = ((Integer) value).intValue();
            renderSpinner.setValue(intValue);
        }
        return renderSpinner;
    }

    @Override
    public boolean isCellEditable(EventObject evt) {
        return true;
    }

    public static void main(String[] args) {
        javax.swing.SwingUtilities.invokeLater(new Runnable() {

            @Override
            public void run() {
                JFrame frame = new JFrame("SpinnerColumn");
                JPanel panel = new JPanel(new GridLayout(1, 1));
                JTable table = new JTable(5, 1);
                SpinnerColumn spinnerColumn = new SpinnerColumn(table, 0);
                table.setDefaultRenderer(Object.class, spinnerColumn);
                panel.add(table);
                frame.setContentPane(panel);
                frame.pack();
                frame.setVisible(true);
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            }
        });
    }
}
于 2012-04-21T15:15:06.380 回答