本文介绍了在jtable中添加行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

 this.tModel.insertRow(rowCount,new Object[]  {"","","",""});
 this.table.setRowSelectionAllowed(true);
 this.table.changeSelection(0, 0, false, false);

 this.table.addKeyListener(new KeyListener() {
      @Override
      public void keyPressed(KeyEvent e) {
          if (e.getKeyCode()==KeyEvent.VK_ENTER) {
               rowCount = this.table.getSelectedRow() + 1;
               tModel.insertRow(rowCount,new Object[]{"", "","",""});
          }
      }
 });

我试图创建一个jtable,在运行时单击鼠标添加行.我已经添加了默认行.但我无法在该行上进行选择.并且我想在按键操作上添加选择时将选择更改为新添加的行?请提出答案?预先感谢

I am trying to create a jtable adding rows at run time on mouse click. i alredy added a default row. but i cant get selection on that row. and i want to change the selection to newly added row when added on the key pressed action?please suggest an answer? thanks in advance

推荐答案

首先,我鼓励您使用键绑定API KeyListener是低级API,可以在看到事件之前先将其消耗掉.

Firstly, I would encourage you to use the key bindings API, KeyListener is a low level API and events can be consumed before you ever see them.

要更改/设置JTable中的行选择,应使用 JTable#setRowSelectionInterval

To change/set the row selection in a JTable, you should use JTable#setRowSelectionInterval

InputMap im = getInputMap(WHEN_FOCUSED);
im.put(KeyStroke.getKeyStroke(KeyEvent.VK_ENTER, 0), "enter");
ActionMap am = getActionMap();
am.put("enter", new AbstractAction() {
    @Override
    public void actionPerformed(ActionEvent e) {
        int rowCount = table.getSelectedRow() + 1;
        tModel.insertRow(rowCount,new Object[]{"", "","",""});
        table.setRowSelectionInterval(rowCount, rowCount);
    }
});

这篇关于在jtable中添加行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-21 00:53