我遇到了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&gt; KeyReleased中,您可以看到按“Enter”时会发生什么,在CommentaarTableKeyListener&gt; KeyTyped中,您可以看到达到最大长度时会发生什么。在CommentaarTableMouseListener中,您可以看到没有为双击定义任何内容。
任何人都可以帮助我吗?如果您有更多问题,请询问。
格尔茨 西尔维
答案 0 :(得分:-1)
我自己找到了。它与在65个字符的固定长度的文本字段中放置长度为66个字符的字符串有关。所以问题解决了!