java组合框条目在离开jtable单元格时消失

ubof19bj  于 2021-07-07  发布在  Java
关注(0)|答案(0)|浏览(211)

我试图在jtable中的某个单元格中添加一个组合框。因此,我为这个单元创建了自己的渲染器和编辑器。一切都按预期工作,直到我选择某个值后离开单元格。此时选定的值丢失,单元格显示为空。我找不到为什么在离开选定单元格时清除单元格条目。
编辑器类:

public class MyWlWertCellEditor extends AbstractCellEditor implements TableCellEditor, ActionListener
{

    /**
     * 
     */
    private static final long serialVersionUID = 1L;
    private MyWlWert _mywlw;
    private List<MyWlWert>_wlwList;

    public MyWlWertCellEditor(List<MyWlWert>wlwlist)
    {
        _wlwList = wlwlist;
    }

    @Override
    public Object getCellEditorValue()
    {
        return _mywlw;
    }

    @Override
    public void actionPerformed(ActionEvent e)
    {
        @SuppressWarnings("unchecked")
        JComboBox<MyWlWert> wlwbox = (JComboBox<MyWlWert>) e.getSource();
        _mywlw = (MyWlWert) wlwbox.getSelectedItem();
    }

    @Override
    public Component getTableCellEditorComponent(JTable table, Object value, boolean isSelected, int row, int column)
    {
        if (value instanceof MyWlWert)
        {
            _mywlw = (MyWlWert) value;
        }

        JComboBox<MyWlWert> wlwbox = new JComboBox<MyWlWert>();

        for (MyWlWert current : _wlwList)
        {
            wlwbox.addItem(current);
        }

        wlwbox.addActionListener(this);

        if (isSelected)
        {
            wlwbox.setBackground(table.getSelectionBackground());
        }
        else
        {
            wlwbox.setBackground(table.getSelectionForeground());
        }
        return wlwbox;
    }

}

渲染器类:

public class MyWlWertCellRenderer extends JComboBox<MyWlWert> implements TableCellRenderer{
     /**
     * 
     */
    private static final long serialVersionUID = 1L;

    public Component getTableCellRendererComponent(JTable table, Object value,
                boolean isSelected, boolean hasFocus, int row, int column) {
            setSelectedItem(value);

            if (isSelected) {
                setBackground(table.getSelectionBackground());
            } else {
                setBackground(table.getSelectionForeground());
            }

            return this;
        }

向表中添加渲染器和编辑器:

_tabelle.getColumnModel().getColumn(4).setCellEditor(new MyWlWertCellEditor(dauern));
_tabelle.getColumnModel().getColumn(4).setCellRenderer(new MyWlWertCellRenderer());

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题