Java Swing JTable;右键单击菜单(如何将其设为“选择”并突出显示该行)

时间:2010-08-24 15:47:14

标签: java swing jtable right-click

简短:我需要一个“右键单击事件”来突出显示单元格行。

我在Java Swing(Netbeans Matisse)的ScrollPane中使用JTable。我在JTable上有一个MouseClicked事件监听器,它执行以下操作:

if (evt.getButton() == java.awt.event.MouseEvent.BUTTON3) {
          System.out.println("Right Click");
          JPopUpMenu.show(myJTable, evt.getX(), evt.getY())
}

问题是......每当我在JTable上执行右键单击时,行都不会突出显示(我将选择设置为仅限btw的行)。我找了几个setSelected()函数但找不到合适的函数。默认情况下,左键单击会自动突出显示该行。如何设置右键点击?

2 个答案:

答案 0 :(得分:81)

像这样:

table.addMouseListener(new MouseAdapter() {
    @Override
    public void mouseReleased(MouseEvent e) {
        int r = table.rowAtPoint(e.getPoint());
        if (r >= 0 && r < table.getRowCount()) {
            table.setRowSelectionInterval(r, r);
        } else {
            table.clearSelection();
        }

        int rowindex = table.getSelectedRow();
        if (rowindex < 0)
            return;
        if (e.isPopupTrigger() && e.getComponent() instanceof JTable ) {
            JPopupMenu popup = createYourPopUp();
            popup.show(e.getComponent(), e.getX(), e.getY());
        }
    }
});

...

答案 1 :(得分:1)

接受的答案不会考虑修改键,如 ctrl shift ,但它们表示当前选择会被替换,但延长了。

此外,我通过选中mousePressed mouseReleased添加了多操作系统支持。

关注后,您可以使用ListSelectionModel找到有关如何调整所选行的完整示例,包括MouseEvent#getModifiers项检查。之后,可以打开(可选)JPopupMenu

JPopupMenu contextMenu = new JPopupMenu();
// ...
// add elements to the popup menu
// ...

table.addMouseListener(new MouseAdapter() {
  @Override
  public void mousePressed(MouseEvent e) {
    handleRowClick(e);
    if (e.isPopupTrigger()) {
      doPop(e);
    } else {
      hidePop();
    }
  }

  @Override
  public void mouseReleased(MouseEvent e) {
    if (e.isPopupTrigger()) {
      doPop(e);
    }
  }

  private void handleRowClick(MouseEvent e) {
    ListSelectionModel selectionModel = table.getSelectionModel();
    Point contextMenuOpenedAt = e.getPoint();
    int clickedRow = table.rowAtPoint(contextMenuOpenedAt);

    if (clickedRow < 0) {
      // No row selected
      selectionModel.clearSelection();
    } else {
      // Some row selected
      if ((e.getModifiers() & InputEvent.SHIFT_MASK) == InputEvent.SHIFT_MASK) {
        int maxSelect = selectionModel.getMaxSelectionIndex();

        if ((e.getModifiers() & InputEvent.CTRL_MASK) == InputEvent.CTRL_MASK) {
          // Shift + CTRL
          selectionModel.addSelectionInterval(maxSelect, clickedRow);
        } else {
          // Shift
          selectionModel.setSelectionInterval(maxSelect, clickedRow);
        }
      } else if ((e.getModifiers() & InputEvent.CTRL_MASK) == InputEvent.CTRL_MASK) {
        // CTRL
        selectionModel.addSelectionInterval(clickedRow, clickedRow);
      } else {
        // No modifier key pressed
        selectionModel.setSelectionInterval(clickedRow, clickedRow);
      }
    }
  }

  private void doPop(MouseEvent e) {
    if (table.getSelectedRowCount() == 0) {
      return;
    }
    contextMenu.setVisible(true);
    contextMenu.show(e.getComponent(), e.getX(), e.getY());
  }

  private void hidePop() {
    contextMenu.setVisible(false);
  }
});