如何在指定列上操作JTable返回值? [英] how to manipulate JTable return value on specified column?

查看:155
本文介绍了如何在指定列上操作JTable返回值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have a JTable that has two columns (editable JTable). When a user types something in the second column, my requirement is as follows:


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

  • user can only typewrite a number and comma

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

when user type wrong character, it will beep (Toolkit.getDefaultToolkit().beep();)

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

How do I go about achieving this? (if it jtextfield it need document filter or plain document etc, if it JTable, then how?)

推荐答案

实施 TableCellEditor 能够返回具有 JTextField ) .oracle.com / javase / 7 / docs / api / javax / swing / text / DocumentFilter.htmlrel =nofollow noreferrer> DocumentFilter 附于它可以根据需要过滤传入的文本。

Implement a TableCellEditor that is capable of returning a text component (JTextField) that has a DocumentFilter attached to it capable of filtering the incoming text as you require.

你可能想看看

  • Limit the Characters in the text field using document listner
  • MDP's Weblog
  • accept only a single digit in java
  • Text Component Features (look for "Implementing a Document Filter")

更新示例


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

if it jtextfield it need document filter or plain document etc, if it JTable, then how?

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

Read the tutorials, ask questions specific to the problem you're having. It's important that you understand why things are done in a certain way rather then simply copying and pasting some one else code, otherwise you won't know when you're copying bad ideas ;)

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;
        }
    }
}

这篇关于如何在指定列上操作JTable返回值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆