请告诉我。JTable 表的列存在以下性质的问题,使用了一个编辑器,该编辑器又返回一个 JScrollPane 对象(或任何其他容器),上面有一个 JTextArea 对象,因此使用键盘进行编辑时,无法在 JTextArea 中输入任何值,因为它不是嵌套对象接收焦点(需要单击鼠标)。如何将焦点放在嵌套对象上?
public class PP extends JFrame {
private String data[][]=new String[][]{{"","",""},{"","",""}};
private String column[]=new String[]{"Фамилия","Имя","Отчество"};
public PP() throws HeadlessException {
super("Кадровая работа");
this.setDefaultCloseOperation(EXIT_ON_CLOSE);
this.setExtendedState(MAXIMIZED_BOTH);
this.setVisible(true);
JTable jTable=new JTable(data,column);
jTable.setRowHeight(40);
jTable.getColumnModel().getColumn(1).setCellEditor(new EditorJTA());
this.add(new JScrollPane(jTable));
}
private class EditorJTA extends AbstractCellEditor
implements TableCellEditor {
private JTextArea editor = new JTextArea();
private JScrollPane result=new JScrollPane(editor);
@Override
public Component getTableCellEditorComponent(JTable table, Object value, boolean isSelected, int row, int column) {
editor.setLineWrap(true);
editor.setWrapStyleWord(true);
editor.addKeyListener(new KeyAdapter() {
@Override
public void keyPressed(KeyEvent e) {
if(e.getKeyCode()==KeyEvent.VK_ENTER)
{
stopCellEditing();
}
}
});
result.setBorder(new LineBorder(null, 0));
return result;
}
@Override
public Object getCellEditorValue() {
return editor.getText();
}
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
new PP();
}
});
}
}
这是某种程度的解决方案,但是缺少第一个字符。