简体   繁体   English

"<i>Java Swing JTable;<\/i> Java Swing JTable;<\/b> <i>Right Click Menu (How do I get it to &quot;select&quot; aka highlight the row)<\/i>右键单击菜单(如何让它“选择”也就是突出显示该行)<\/b>"

[英]Java Swing JTable; Right Click Menu (How do I get it to "select" aka highlight the row)

Short: I need a "right-click event" to highlight the cell row.简短:我需要一个“右键单击事件”来突出显示单元格行。

I am using a JTable inside a ScrollPane in Java Swing (Netbeans Matisse).我在 Java Swing (Netbeans Matisse) 的 ScrollPane 中使用 JTable。 I have a MouseClicked event listener on the JTable that does the following:我在 JTable 上有一个 MouseClicked 事件侦听器,它执行以下操作:

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

like this: 像这样:

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());
        }
    }
});

...... ......

The accepted answer does not take modifier keys like ctrl or shift into account, yet they indicate that the current selection should not be replaced, but extended. 接受的答案并不需要修改键,如CtrlShift进去,但他们表示,目前的选择应该被取代,但延长。

Also, I added multi-OS support by checking mousePressed and mouseReleased . 此外,我通过检查mousePressed mouseReleased添加了多操作系统支持。

Following, you can find a complete example on how to adjust the selected rows, using the ListSelectionModel , including MouseEvent#getModifiers checks. 接下来,您可以使用ListSelectionModel找到有关如何调整所选行的完整示例,包括MouseEvent#getModifiers检查。 After that, it is possible to open a (optional) JPopupMenu . 之后,可以打开(可选) 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);
  }
});

I found this solution that creates another MouseEvent to work well inside my JTable subclass (processMouseEvent has protected access).我发现这个解决方案可以创建另一个 MouseEvent 以在我的 JTable 子类中正常工作(processMouseEvent 具有受保护的访问权限)。 Takes care of using the modifiers for the selection update.负责使用修饰符进行选择更新。

addMouseListener(new MouseAdapter() {
    @Override public void mousePressed(MouseEvent e) { checkForPopupMenu(e); }
    @Override public void mouseReleased(MouseEvent e) { checkForPopupMenu(e); }
    private void checkForPopupMenu(MouseEvent e) {
        if (e.isPopupTrigger()) {
            processMouseEvent(new MouseEvent(e.getComponent(), e.getID(), e.getWhen(), e.getModifiersEx(), e.getX(), e.getY(), e.getClickCount(), false, MouseEvent.BUTTON1));
            if (getSelectedRowCount() != 0)
                popup.show(e.getComponent(), e.getX(), e.getY());
        }
    }
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM