I have a JTable and some editable cells which are dynamically formatted with very specific HTML based on some business rules.
However, when you edit these cells all the HTML is in the CellEditor. I just want the plain text in the 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. I don't even see the debugger entering the getCellEditorValue() method. 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()
.