简体   繁体   English

从JTable CellEditor删除HTML

[英]Remove HTML from JTable CellEditor

I have a JTable and some editable cells which are dynamically formatted with very specific HTML based on some business rules. 我有一个JTable和一些可编辑的单元格,它们根据一些业务规则用非常特定的HTML动态格式化。

However, when you edit these cells all the HTML is in the CellEditor. 但是,当您编辑这些单元格时,所有HTML都在CellEditor中。 I just want the plain text in the CellEditor. 我只想要CellEditor中的纯文本。

I am trying to do this to the entire table. 我正在尝试对整个桌子执行此操作。 Here is the code I used. 这是我使用的代码。 I threw together an extended DefaultCellEditor but its still showing the HTML. 我把扩展的DefaultCellEditor放在一起,但它仍显示HTML。 I don't even see the debugger entering the getCellEditorValue() method. 我什至看不到调试器输入getCellEditorValue()方法。 What do I do? 我该怎么办?

public class MyTable extends JTable
{

public MyTable()
{
MyTable.setCellEditor(new DefaultCellEditor(new JTextField())
    {
        @Override
        public Object getCellEditorValue() {
            // get content of textField
            String str = (String) super.getCellEditorValue();
            if (str == null) {
                return null;
            }

            if (str.length() == 0) {
                return null;
            }
            //remove HTML and return plain text
            return Jsoup.parse(str).text();
        }
    });
}
}

I'm not sure where things are going awry; 我不确定哪里出问题了; a complete example may shed some light. 一个完整的例子可能会有所启发。 The normal editing sequence is outlined here , suggesting that you should probably create your own renderer and editor : 此处概述正常的编辑顺序,建议您可能应该创建自己的渲染器和编辑器

class MyRenderer extends DefaultTableCellRenderer {…}
class MyEditor extends DefaultCellEditor {…}

and apply them as follows: 并将它们应用如下:

table.setDefaultRenderer(String.class, new MyRenderer());
table.setDefaultEditor(String.class, new MyEditor());

Be certain that your TableModel returns the correct type token from getColumnClass() . 确保您的TableModelgetColumnClass()返回正确的类型标记。

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

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