- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我无法让这个带有 DocumentFilter
的 CellEditor
正常工作。当我在唯一可编辑的列中键入内容时,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/
我已将其中一列的单元格编辑器设置为 JSpinner 并且它可以工作,但是当数据模型中的数据更改时,我之前启用的编辑器仍处于启用状态,它会显示旧行的值(它不存在)或者它在更改的数据中位于不同的位置)。
在我的 JTable 中,我有两个可编辑的数字列。两列的编辑器都扩展了 AbstractCellEditor 并使用 JFormattedTextField 作为编辑组件。 问题在于输入的格式取决于行
我陷入了以下问题,找不到我做错了什么。我的问题是替换 JTable 单元格的默认编辑器并使用 ComboBox。我的代码看起来工作正常,但没有达到预期的效果。经过深思熟虑,并验证了 Oracle 在他
我用 celleditor 制作了与此类似的表格: http://demo.qooxdoo.org/current/demobrowser/#table~Table_Cell_Editor.html
我在使用 编辑表格时遇到一些问题 我使用encoding='windows-1252'能够使用瑞典语字符 (å, ä, ö) 。创建一个实体工作正常,但是当我在 中编辑它时使用它会提交意想不到的字
我正在创建一个扩展 AbstractTableModel 的 TableModel。但其内容会定期更改,并且列数会根据显示的数据而变化。有时,我需要使用 ComboBox 来编辑第 2 列中的单元格,
我不知道如何在特定行中设置 jcombobox...现在我已经为所有行设置了这个 jcombobox,但我希望它只在一行中: JComboBox cc = new JComboBox(); cc.ad
我正在寻找如何执行此操作的最佳解决方案。我有什么: // model Ext.define("User", { extend: "Ext.data.Model", fields: [
我需要使用 JXDatePicker 使我的 JTable 中的日期单元格可编辑。我想我需要实现 TableCellEditor,但我不敢自己解决它。有没有人有可以帮助我的链接或一段快速代码? 最佳答
我想从用户列表中选择: user.ts export class User { constructor (public id: number, public userName : string
我在 Primefaces 中有一个数据表,带有一个 celleditor 来上传和下载文件。文件上传工作。文件下载不起作用,它只会重新加载页面。当我将下载按钮直接放在 p: 列下时,下载工作正常。
我应该处理CellEditor从 EditingSupport.getCellEditor 返回,如果是这样,我应该什么时候做。 关于 jface 的教程之一 TableViewer我看到以下片段:
我有一个可编辑的 与 我想使用 将该表格的内容导出为 PDF 格式. 我已经包括了 itext 2.1.7 jar .我得到了 PDF 格式的输出,但它显示了 Object#toString()所有
这是我的代码不起作用: ...
我有一个带有 ComboBoxViewerCellEditor 编辑支持的 TreeViewerColumn 。现在,当我使用普通 Combo 时,我可以添加自动完成功能,如下所示: new Auto
简短版本:给定一个带有扩展 DefaultTableModel 的 TableModel 的 JTable,我可以在不使用 CellEditor 的情况下选择具有 String 类的单元格中的所有文本
当先前的编辑完成时,我想通过自动跳转到表格查看器中的下一个单元格来安慰用户。用户需要修改条目列表中的开始-结束时间。我想知道如何手动(以编程方式)触发特定单元格的 CellEditor。我以前没有在
我无法让这个带有 DocumentFilter 的 CellEditor 正常工作。当我在唯一可编辑的列中键入内容时,PlainDocument 中的 insertString 不会被调用,docum
我正在尝试为我的 JTable 列创建一个 DefaultCellEditor ,以便我可以设置计划的开始时间和结束时间。但是,我不明白为什么当我在 JTable 的单元格中使用 JSpinner 输
我有一个带有自定义 CellEditor 的 JTable,在 JScrollPane 中使用 JTextArea。当我通过鼠标点击进入编辑模式时,它工作得很好。但是,当我尝试在单元格聚焦时键入一些字
我是一名优秀的程序员,十分优秀!