gpt4 book ai didi

java - 带有 DocumentFilter 的 CellEditor 永远不会被调用

转载 作者:行者123 更新时间:2023-11-30 07:10:10 26 4
gpt4 key购买 nike

我无法让这个带有 DocumentFilterCellEditor 正常工作。当我在唯一可编辑的列中键入内容时,PlainDocument 中的 insertString 不会被调用,documentFilter 也不会被调用。我认为我没有正确覆盖某些内容,但我想不通。

我做了这个最小的、完整的、经过测试的和可读的例子

//TODO include imports 
public class Test {

private static final int EDITABLE_COLUMN = 1;
private final TableCellEditor cellEditor;
private final JTextField textfield;
private final PlainDocument document;
private JPanel panel;
private MyTableModel tableModel = new MyTableModel();
private JTable table = new JTable() {

{
getTableHeader().setReorderingAllowed(false);
setAutoCreateRowSorter(false);
setSurrendersFocusOnKeystroke(Boolean.TRUE);

}

@Override
public boolean isCellEditable(int row, int column) {
return convertColumnIndexToModel(column) == EDITABLE_COLUMN;
}

@Override
public TableCellEditor getCellEditor(int row, int column) {
int modelColumn = convertColumnIndexToModel(column);
if (modelColumn == EDITABLE_COLUMN) {
System.out.println(" ENTER HERE ");
return Test.this.cellEditor;
}
return super.getCellEditor(row, column);
}

@Override
public void changeSelection(
final int row, final int column, final boolean toggle, final boolean extend) {
super.changeSelection(row, column, toggle, extend);
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {

if ((getCellEditor(row, column) != null && editCellAt(row, column))) {
JTextComponent comp = (JTextComponent) getEditorComponent();
comp.requestFocusInWindow();
comp.selectAll();

if (column == EDITABLE_COLUMN) {
System.out.println("Is the same editor instance ? " + (textfield == getEditorComponent()));
System.out.println("Is the same document instance ? " + (textfield.getDocument() == Test.this.document));
}

}
}
});
}
};

public Test() {
JScrollPane scroll = new JScrollPane(table);
table.setModel(tableModel);
panel = new JPanel(new BorderLayout());
//panel.add(busyComponent,BorderLayout.CENTER);
panel.add(scroll, BorderLayout.CENTER);


textfield = new JTextField();
textfield.setHorizontalAlignment(JTextField.RIGHT);
document = new PlainDocument() {

@Override
public void insertString(int offs, String str, AttributeSet a) throws BadLocationException {
System.out.println("Working here");
super.insertString(offs, str, a);
}

};
document.setDocumentFilter(new MyDocumentFilter());
textfield.setDocument(document);
//this fixes to show all
textfield.setBorder(null);
cellEditor = new DefaultCellEditor(textfield);
//i also try this without success
//table.getColumnModel().getColumn(EDITABLE_COLUMN).setCellEditor(cellEditor);
tableModel.insertValue(new ItemRow("nonEditable", "Editable"));
}



private class ItemRow {

private String column1;
private String column2;

public ItemRow(String column1, String column2) {
this.column1 = column1;
this.column2 = column2;
}

public String getColumn1() {
return column1;
}

public void setColumn1(String column1) {
this.column1 = column1;
}

public String getColumn2() {
return column2;
}

public void setColumn2(String column2) {
this.column2 = column2;
}
}

private class MyTableModel extends AbstractTableModel {

public static final int COLUMN1_INDEX = 0;
public static final int COLUMN2_INDEX = 1;
private final List<ItemRow> data = new ArrayList<>();

private final String[] columnsNames = {
"Column1",
"Column2",};

private final Class<?>[] columnsTypes = {
String.class,
Integer.class
};


public MyTableModel() {
super();
}

@Override
public Object getValueAt(int inRow, int inCol) {
ItemRow row = data.get(inRow);
Object outReturn = null;

switch (inCol) {
case COLUMN1_INDEX:
outReturn = row.getColumn1();
break;
case COLUMN2_INDEX:
outReturn = row.getColumn2();
break;
default:
throw new RuntimeException("invalid column");
}

return outReturn;
}

@Override
public void setValueAt(Object inValue, int inRow, int inCol) {
System.out.println("Gets called ");
if (inRow < 0 || inCol < 0 || inRow >= data.size()) {
return;
}

ItemRow row = data.get(inRow);
switch (inCol) {
case COLUMN1_INDEX:
row.setColumn1(inValue.toString());
break;
case COLUMN2_INDEX:
row.setColumn2(inValue.toString());
break;
}
fireTableCellUpdated(inRow, inCol);
}

@Override
public int getRowCount() {
return data.size();
}

@Override
public int getColumnCount() {
return columnsTypes.length;
}

@Override
public String getColumnName(int inCol) {
return this.columnsNames[inCol];
}

@Override
public Class<?> getColumnClass(int columnIndex) {
return this.columnsTypes[columnIndex];
}

/**
*
* @param row
*/
public void insertValue(ItemRow row) {
data.add(row);
fireTableRowsInserted(data.size() - 1, data.size() - 1);
}

}

private class MyDocumentFilter extends DocumentFilter {

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

JOptionPane.showMessageDialog(null, "Enter hereeeeeeeee");

super.insertString(fb, offset, string, attr);
}
}

private static void createAndShowGUI(final Container container, final String title) {
//Create and set up the window.
JFrame frame = new JFrame(title);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLocationByPlatform(Boolean.TRUE);
frame.add(container);
//Display the window.
frame.pack();
frame.setVisible(true);
}

public static void main(String args[]) {
SwingUtilities.invokeLater(new Runnable() {

@Override
public void run() {
try {
for (UIManager.LookAndFeelInfo info : UIManager.getInstalledLookAndFeels()) {
if ("Nimbus".equals(info.getName())) {
UIManager.setLookAndFeel(info.getClassName());
break;
}
}

} catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
ex.printStackTrace();
}
createAndShowGUI(new Test().panel, "Test");
}

});
}

}

最佳答案

您没有完全覆盖该类。试试这个来改变:

private class MyDocumentFilter extends DocumentFilter {

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

JOptionPane.showMessageDialog(null, "Enter hereeeeeeeee");

super.insertString(fb, offset, string, attr);
}

@Override
public void replace(FilterBypass fb, int offset, int length, String text,
AttributeSet attrs) throws BadLocationException {
JOptionPane.showMessageDialog(null, "This is really what you should be overriding");
super.replace(fb, offset, length, text, attrs);
}

@Override
public void remove(FilterBypass fb, int offset, int length)
throws BadLocationException {
JOptionPane.showMessageDialog(null, "and don't forget this!");
super.remove(fb, offset, length);
}
}

该类有 3 个方法可以被重写,而您只重写了其中一个。

关于java - 带有 DocumentFilter 的 CellEditor 永远不会被调用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22418159/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com