选中JTable的单元格后如何输入汉字 用tab或enter键在JTable中选中单元格 或是 用 鼠标单击选中单元格的情况下,只能输入字母,不能输入汉字。只有双击鼠标后才能输入。请问有什么方法可以解决上述问题。谢谢! 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 复写单击的事件。实在不行就用robot模拟 TableColumne col = table.getColumn("colName");col.setCellEditor(new ValueTableEditor()); class ValueTableCellEditor extends AbstractCellEditor implements TableCellEditor{JTextField input = new JTextField(); public Component getTableCellEditorComponent(JTable table, Object value, boolean isSelected, int row, int column) { // TODO Auto-generated method stubreturn input;} public Object getCellEditorValue() { // TODO Auto-generated method stub return input.getText(); } 鼠标单击选中单元格确实是可以输入中文了,但如果用TAB键移动切换单元格在中文状态下还是不能录入,各位有何高见啊? public static void addKeyDowntoEditEvent(JTable tb) { tb.addToolTipText("上下键及Tab键进入编辑状态,Esc取消编辑状态"); tb.addKeyListener(new java.awt.event.KeyAdapter() { public void keyReleased(KeyEvent e) { int key = e.getKeyCode(); JTable tb = (JTable) e.getSource(); addKeyDowntoEditEvent(tb, key); } } ); } private static void addKeyDowntoEditEvent(JTable tb,int key) { try { int rows = tb.getRowCount(); int cols = tb.getColumnCount(); int selectingrow = tb.getSelectedRow(); int selectingcol = tb.getSelectedColumn(); if (selectingrow < 0||selectingcol<0) return; try{ tb.getCellEditor(selectingrow, selectingcol).stopCellEditing(); } catch(Exception ex){} switch (key) { case TbEvents.KEY_DOWN: case TbEvents.KEY_UP: case TbEvents.KEY_TAB:{ break; } case KeyEvent.VK_ESCAPE:{ stopEditing(tb); return; } default:{ return; } } try { if (selectingrow >= rows) { selectingrow = 0; selectingcol++; } if (selectingcol >= cols) { selectingcol = 0; } if (selectingcol >= cols) { selectingcol = 0; selectingrow++; } if (selectingrow >= rows) { selectingrow = 0; } if (!tb.isCellEditable(selectingrow, selectingcol)) return;// tb.setRowSelectionInterval(selectingrow,selectingrow);// tb.setColumnSelectionInterval(selectingcol,selectingcol); tb.editCellAt(selectingrow, selectingcol); ( (JTextField) ( (DefaultCellEditor) tb.getCellEditor(selectingrow, selectingcol)).getComponent()).requestFocus(); ( (JTextField) ( (DefaultCellEditor) tb.getCellEditor(selectingrow, selectingcol)).getComponent()).selectAll(); tb.scrollRectToVisible(new java.awt.Rectangle((selectingcol-1)*tb.getColumnModel().getColumn(0).getWidth(),(selectingrow-1)*tb.getRowHeight(),200,200)); } catch (Exception ex) {} } catch (Exception ex) { ex.printStackTrace(); } } 以前学过C 最近学java 有几个问题混淆了 FileInputStream read 如何在一个<input />中区别何时调用onblur事件或者onkeydown事件 关于时区设定的问题 ! 求助:ftp客户端的怪问题 一个严重的问题!!!!! jsp+javabean+sql被它搞死了! 问一个语法方面的问题? 一个JAVA继承的问题(Thinking in JAVA里的一个例子) 如何把jdk打在jar里面 用JAVA 做FTP 急急急~~~~关于一个文件流的问题~
实在不行就用robot模拟
TableColumne col = table.getColumn("colName");
col.setCellEditor(new ValueTableEditor());
class ValueTableCellEditor extends AbstractCellEditor implements TableCellEditor{JTextField input = new JTextField(); public Component getTableCellEditorComponent(JTable table, Object value, boolean isSelected, int row, int column) {
// TODO Auto-generated method stub
return input;
}
public Object getCellEditorValue() {
// TODO Auto-generated method stub
return input.getText();
}
tb.addToolTipText("上下键及Tab键进入编辑状态,Esc取消编辑状态");
tb.addKeyListener(new java.awt.event.KeyAdapter() {
public void keyReleased(KeyEvent e) {
int key = e.getKeyCode();
JTable tb = (JTable) e.getSource();
addKeyDowntoEditEvent(tb, key);
}
}
);
}
private static void addKeyDowntoEditEvent(JTable tb,int key) {
try {
int rows = tb.getRowCount();
int cols = tb.getColumnCount(); int selectingrow = tb.getSelectedRow();
int selectingcol = tb.getSelectedColumn(); if (selectingrow < 0||selectingcol<0)
return; try{
tb.getCellEditor(selectingrow, selectingcol).stopCellEditing();
}
catch(Exception ex){} switch (key) { case TbEvents.KEY_DOWN:
case TbEvents.KEY_UP:
case TbEvents.KEY_TAB:{
break;
}
case KeyEvent.VK_ESCAPE:{
stopEditing(tb);
return;
}
default:{
return;
}
} try {
if (selectingrow >= rows) {
selectingrow = 0;
selectingcol++;
}
if (selectingcol >= cols) {
selectingcol = 0;
}
if (selectingcol >= cols) {
selectingcol = 0;
selectingrow++;
}
if (selectingrow >= rows) {
selectingrow = 0;
} if (!tb.isCellEditable(selectingrow, selectingcol))
return;// tb.setRowSelectionInterval(selectingrow,selectingrow);
// tb.setColumnSelectionInterval(selectingcol,selectingcol);
tb.editCellAt(selectingrow, selectingcol);
( (JTextField) ( (DefaultCellEditor) tb.getCellEditor(selectingrow, selectingcol)).getComponent()).requestFocus();
( (JTextField) ( (DefaultCellEditor) tb.getCellEditor(selectingrow, selectingcol)).getComponent()).selectAll();
tb.scrollRectToVisible(new java.awt.Rectangle((selectingcol-1)*tb.getColumnModel().getColumn(0).getWidth(),(selectingrow-1)*tb.getRowHeight(),200,200));
}
catch (Exception ex) {}
}
catch (Exception ex) {
ex.printStackTrace();
}
}