日期:2014-05-20  浏览次数:20673 次

请教如何让 JTable增加一行的时候新增的一行被选中,同时第一个cell处于全选中、可修改状态
Java 达人,

我最近在捣腾 Jtable,搞得头都大了,还有有论坛各位大神的帮忙。在此先说一声谢谢了!

JFrame上有个 “Add”按钮,按下即给 JTable添加一行数据。

我想要的效果是,在给 JTable添加一行数据的时候,能够让新增的一行被选中,同时第一个cell处于全选中、可修改状态。

如下图所示,新增加的一行为第 3行。

十分感谢!



------解决方案--------------------
在你的
buttonAdd.addMouseListener(new MouseListener() {
public void mouseClicked(MouseEvent e) {

}
方法的最后面加上以下代码,试试看。

Java code

int row = model.getRowCount() - 1;
int col = 0;
if (table.editCellAt(row, col)) {
    Component editor = table.getEditorComponent();
    editor.requestFocusInWindow();
    Component c = editor.getComponentAt(0, 0);
    if (c != null && c instanceof JTextComponent) {
        ((JTextComponent) c).selectAll();
    }
}