如何在特定列上操纵JTable返回值?

时间:2012-11-13 01:20:22

标签: java swing jtable jtextfield tablecelleditor

我有一个JTable,它有两列(可编辑的JTable )。当用户在第二列中键入内容时,我的要求如下:

  • 用户只能输入数字和逗号

  • 当用户输入错误的字符时,会发出哔声(Toolkit.getDefaultToolkit().beep();

我如何实现这一目标? (如果它是jtextfield它需要文件过滤器或普通文件等,如果它是JTable,那么如何?

1 个答案:

答案 0 :(得分:4)

实施一个TableCellEditor能够返回附加DocumentFilter的文本组件(JTextField),能够根据需要过滤传入的文本。

您可能想看看

更新了示例

  

如果它是jtextfield它需要文件过滤器或普通文件等,如果它   JTable,那怎么样?

阅读教程,询问有关您遇到的问题的具体问题。重要的是你要理解为什么事情是以某种方式完成的,而不是简单地复制和粘贴其他一些代码,否则你不会知道何时复制坏的想法;)

public class TestFilteredCellEditor {

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

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

                JFrame frame = new JFrame();
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.setLayout(new BorderLayout());
                frame.add(new FilteredEditorPane());
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }
        });
    }

    public class FilteredEditorPane extends JPanel {

        public FilteredEditorPane() {
            setLayout(new BorderLayout());
            JTable table = new JTable(new MyTableModel());
            TableColumnModel columnModel = table.getColumnModel();
            columnModel.getColumn(1).setCellEditor(new FilteredTableEditor());
            add(new JScrollPane(table));
        }
    }

    public class FilteredTableEditor extends AbstractCellEditor implements TableCellEditor {

        private JTextField editor;

        public FilteredTableEditor() {
            editor = new JTextField();
            ((AbstractDocument) editor.getDocument()).setDocumentFilter(new NumericDocumentFilter());
        }

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

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

        @Override
        public Component getTableCellEditorComponent(JTable table, Object value, boolean isSelected, int row, int column) {
            if (value instanceof String) {
                editor.setText((String) value);
            } else {
                editor.setText(null);
            }
            return editor;
        }
    }

    public class NumericDocumentFilter extends DocumentFilter {

        public void insertString(DocumentFilter.FilterBypass fb, int offset,
                String string, AttributeSet attr)
                throws BadLocationException {

            StringBuilder buffer = new StringBuilder(string);
            boolean isBad = false;
            for (int i = buffer.length() - 1; i >= 0; i--) {
                char ch = buffer.charAt(i);
                if (!Character.isDigit(ch)) {
                    buffer.deleteCharAt(i);
                    isBad = true;
                }
            }
            if (isBad) {
                Toolkit.getDefaultToolkit().beep();
            }
            super.insertString(fb, offset, buffer.toString(), attr);
        }

        public void replace(DocumentFilter.FilterBypass fb,
                int offset, int length, String string, AttributeSet attr) throws BadLocationException {
            if (length > 0) {
                fb.remove(offset, length);
            }
            insertString(fb, offset, string, attr);
        }
    }

    public class MyTableModel extends AbstractTableModel {

        @Override
        public int getRowCount() {
            return 1;
        }

        @Override
        public int getColumnCount() {
            return 2;
        }

        @Override
        public Object getValueAt(int rowIndex, int columnIndex) {
            return "";
        }

        @Override
        public boolean isCellEditable(int rowIndex, int columnIndex) {
            return true;
        }

        @Override
        public Class<?> getColumnClass(int columnIndex) {
            return String.class;
        }
    }
}