0

我有一个 JTable 的问题。当我以固定长度的文本字段作为编辑器输入单元格时,当达到长度时,我会自动跳转到下一行。这样我就可以继续写了。之后,当我想更改第一行中的一些文本并双击单元格时,文本消失了。

奇怪的是,当我按“Enter”进入下一行并双击第一行时,文本并没有消失。

private void initialize() {

    vCommentaarHeaders.add(resourceBundle.getString("col_Aan"));
    vCommentaarHeaders.add(resourceBundle.getString("Commentaar"));

    tmCommentaar.addTableModelListener(new CommentaarTableModelListener());

    jtable = new JTable(tmCommentaar);

    //...

    jtable.addMouseListener(new CommentaarTableMouseListener());
    jtable.addKeyListener(new CommentaarTableKeyListener());

    //fill combobox
    //Get saved data
    //Add empty line

    jtable.getColumnModel().getColumn(0).setCellEditor(
            new DefaultCellEditor(cbxAan));
    jtable.getColumnModel().getColumn(1).setCellEditor(
            new DefaultCellEditor(ftfCommentaar));

    //Add table to content pane

}

// MouseListener ....

private class CommentaarTableKeyListener implements KeyListener {

    public void keyTyped(KeyEvent e) {
        if (selectedColumn != jtable.getSelectedColumn()
                || selectedRow != jtable.getSelectedRow()) {
            sCommentaar = (String) ((Vector) vCommentaarData.get(jtable
                    .getSelectedRow())).get(1);
        }

        if (e.getKeyChar() != '\t' || e.getKeyChar() != '\n') {
            if (e.getKeyChar() == '') {
                sCommentaar = sCommentaar.substring(0,
                        sCommentaar.length() - 1);
                selectedColumn = jtable.getSelectedColumn();
                selectedRow = jtable.getSelectedRow();
            } else {
                if (sCommentaar.length() <= 65){
                    sCommentaar = sCommentaar + e.getKeyChar();
                    selectedColumn = jtable.getSelectedColumn();
                    selectedRow = jtable.getSelectedRow();
                }
                else{
                    //addEmptyLine();
                    tmCommentaar.setValueAt(sCommentaar, selectedRow, selectedColumn);
                    selectedColumn = jtable.getSelectedColumn();
                    selectedRow = jtable.getSelectedRow() + 1;
                    String sBestemmeling = (String) ((Vector) vCommentaarData.get(selectedRow-1)).get(0);
                    Vector vHulp = new Vector();
                    vHulp.add(sBestemmeling);
                    vHulp.add("" + e.getKeyChar());
                    sCommentaar = "" + e.getKeyChar();
                    if( vCommentaarData.size() >= selectedRow )
                        vCommentaarData.setSize(selectedRow + 1);
                    vCommentaarData.setElementAt(vHulp, selectedRow);

                    tmCommentaar.fireTableDataChanged();

                    jtable.setCellSelectionEnabled(true);

                    jtable.getCellEditor(selectedRow - 1, selectedColumn).cancelCellEditing();
                    jtable.changeSelection(selectedRow, selectedColumn, false, false);
                    jtable.grabFocus();

                }

            }

        }
    }

    public void keyPressed(KeyEvent e) {
    }

    public void keyReleased(KeyEvent e) {

        if (e.getKeyCode() == KeyEvent.VK_ENTER){
            if(selectedRow > 0){
                String sBestemmeling = (String) ((Vector) vCommentaarData.get(selectedRow-1)).get(0);
                Vector vHulp = new Vector();
                vHulp.add(sBestemmeling);
                vHulp.add("");
                if( vCommentaarData.size() >= selectedRow )
                    vCommentaarData.setSize(selectedRow + 1);
                vCommentaarData.setElementAt(vHulp, selectedRow);

                tmCommentaar.fireTableDataChanged();
                jtable.setCellSelectionEnabled(true);

                jtable.getCellEditor(selectedRow - 1, selectedColumn).cancelCellEditing();
                jtable.changeSelection(selectedRow, selectedColumn, false, false);
                jtable.grabFocus();

            }
        }
    }
}

在 CommentaarTableKeyListener>KeyReleased 中,您可以看到按下“Enter”时会发生什么,在 CommentaarTableKeyListener>KeyTyped 中,您可以看到达到最大长度时会发生什么。在 CommentaarTableMouseListener 中,您可以看到没有为双击定义任何内容。

谁能帮我?如果您有更多问题,请询问。

问候西尔维

4

1 回答 1

-1

我自己找到了。它与将长度为 66 个字符的字符串放在 65 个字符的固定长度文本字段中有关。所以问题解决了!

于 2013-10-04T09:17:11.327 回答