java中的自定义表格单元格编辑器,列数据类型为Float

iaqfqrcu  于 2023-01-19  发布在  Java
关注(0)|答案(1)|浏览(170)

正如我们所知,默认情况下,在JTable中编辑单元格时,必须按Enter键两次才能更改选择。
为了覆盖这种行为,我尝试编写一个自定义表格单元格编辑器,其中在按下Enter键后,当前单元格中的编辑完成,选择更改为下一行,并在新选择的单元格中开始编辑,其内容将自动选择。
整个方案对整数运行良好,但对浮点数据类型不适用。对于数据类型为浮点/双精度型的列,我得到错误
java. lang.非法参数异常:无法将给定对象的格式设置为数字
按Enter键更改选择时。
我正在使用以下代码,我发现,而在网上搜索.

package javaapplication1;

import java.awt.Component;
import java.awt.Dimension;
import java.awt.EventQueue;
import javax.swing.AbstractCellEditor;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableCellEditor;
import javax.swing.table.TableColumn;
import javax.swing.text.JTextComponent;

/**
 *
 * @author alqama
 */
public class test extends JPanel{
    
    private JTable table;
    
    test()
    {
        table = new JTable(){

        @Override
        public void changeSelection(    int row, int column, boolean toggle, boolean extend)
        {
            super.changeSelection(row, column, toggle, extend);

            if (editCellAt(row, column))
            {
                Component editor = getEditorComponent();
                editor.requestFocusInWindow();
                ((JTextComponent) editor).selectAll();

            }
        }};
        DefaultTableModel dtm = new javax.swing.table.DefaultTableModel(
                new Object [][] {

                    },
                new String [] { "Integer","Float"}
            ) {
                public Class getColumnClass(int col) {
                if(col==0) return java.lang.Integer.class;
                return java.lang.Float.class;
            }

            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return true;
            }
        };
        table.setModel(dtm);
        CustomTableCellEditor et = new CustomTableCellEditor(table);
        table.getColumnModel().getColumn(1).setCellEditor(et);
        table.getColumnModel().getColumn(0).setCellEditor(et);
        
        for (int i = 0; i < 5; i++) {
            dtm.addRow(new Object[]{i,1.5+i});
        }    
        
        
        table.setPreferredScrollableViewportSize(new Dimension(123, 123));
        this.add(new JScrollPane(table));
        
    }
    private void display() {
        JFrame f = new JFrame("Test Table");
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        f.add(this);
        f.pack();
        f.setLocationRelativeTo(null);
        f.setVisible(true);
    }

    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {

            @Override
            public void run() {
                new test().display();
            }
        });
    } 
    
    
    
    
    protected class CustomTableCellEditor  extends AbstractCellEditor implements TableCellEditor {
     
         private JTable table;
         JTextField component = new JTextField();

        public CustomTableCellEditor(JTable table) {
            this.table = table;
        }

        public boolean stopCellEditing() {
            
            boolean ans = super.stopCellEditing();            
            return ans;
        }

        @Override
        public void cancelCellEditing() {
            super.cancelCellEditing();
        }

        @Override
        public Object getCellEditorValue() {
            return component.getText();        
        }

        @Override
        public Component getTableCellEditorComponent(JTable arg0, Object value,
                boolean arg2, int arg3, int arg4) {
            component.setText(value.toString());
            return component;
        }
     }
}
x7rlezfr

x7rlezfr1#

您的模型对列1getColumnClass()实现返回Float.class,因此您的getCellEditorValue()实现应该返回相同类型的值:

@Override
public Object getCellEditorValue() {
    return Float.valueOf(component.getText());

或者,您可以按类指定编辑器:

CustomTableCellEditor et = new CustomTableCellEditor(table);
table.setDefaultEditor(Float.class, et);

相关问题