简体   繁体   English

如何使Jtable单元格字符长度

[英]How to make Jtable cell character length

我想让我的j表列限制字符,例如只允许16个字符。我尝试了各种方法都没有用,有人能帮我吗?

An answer found here https://community.oracle.com/thread/1482301 在这里找到答案https://community.oracle.com/thread/1482301

"Custom Cell Editors http://java.sun.com/docs/books/tutorial/uiswing/components/table.html#validtext and a custom document on a JTextField “自定义单元格编辑器http://java.sun.com/docs/books/tutorial/uiswing/components/table.html#validtext和JTextField上的自定义文档

One example worth thousand words" 值得一千个单词的例子”

import java.awt.*;
import javax.swing.*;
public class Test2 extends JFrame {
  public Test2() {
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    Container content = getContentPane();
    String[] head = {"One","Two","Three"};
    String[][] data = {{"R1-C1", "12345678", "R1-C3"},
                       {"R2-C1", "R2-C2", "R2-C3"},
                       {"R3-C1", "R3-C2", "R3-C3"}};
    JTable jt = new JTable(data, head);
    content.add(new JScrollPane(jt), BorderLayout.CENTER);
    JTextField jtf = new JTextField();
    jtf.setDocument(new LimitedPlainDocument(10));
    jt.getColumnModel().getColumn(1).setCellEditor(new DefaultCellEditor(jtf));
    setSize(300,300);
  }
  public static void main(String[] args) { new Test2().setVisible(true); }
}
class LimitedPlainDocument extends javax.swing.text.PlainDocument {
  private int maxLen = -1;
  /** Creates a new instance of LimitedPlainDocument */     
  public LimitedPlainDocument() {}
  public LimitedPlainDocument(int maxLen) { this.maxLen = maxLen; }
  public void insertString(int param, String str, 
                           javax.swing.text.AttributeSet attributeSet) 
                      throws javax.swing.text.BadLocationException {
    if (str != null && maxLen > 0 && this.getLength() + str.length() > maxLen) {
      java.awt.Toolkit.getDefaultToolkit().beep();
      return;
    }
    super.insertString(param, str, attributeSet);
  }
}

Here's what I did 这就是我所做的

The Call: 电话:

    JTable table = new JTable();
    JTextField textField = new JTextField();
    limitCharacters(jtf, 16);
    table.getColumnModel().getColumn(0).setCellEditor(new DefaultCellEditor(jtf));

The limitCharacters Method: limitCharacters方法:

private void limitCharacters(JTextField textField, final int limit) {
     PlainDocument document = (PlainDocument) textField.getDocument();

     document.setDocumentFilter(new DocumentFilter() {

        @Override
        public void replace(DocumentFilter.FilterBypass fb, int offset,
                int length, String text, AttributeSet attrs)
                throws BadLocationException {
            String string = fb.getDocument().getText(0,
                    fb.getDocument().getLength())
                    + text;

            if (string.length() <= limit)
                super.replace(fb, offset, length, text, attrs); 
        }

    });
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM