Java Swing JTable;右键单击菜单(我如何让它“选择”又名突出显示行)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3558293/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-14 01:50:40  来源:igfitidea点击:

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

javaswingjtableright-click

提问by Carlo del Mundo

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). I have a MouseClicked event listener on the JTable that does the following:

我在 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())
}

The problem is... whenever I execute a right click on the JTable, the row isn't highlighted (I set the selection to rows only btw). I have looked for several setSelected() functions but could not find a suitable one. By default, left clicking automatically highlights the row. How do I set it up for right clicks?

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

采纳答案by clamp

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

......

......

回答by Markus Weninger

The accepted answer does not take modifier keys like ctrlor shiftinto account, yet they indicate that the current selection should notbe replaced, but extended.

接受的答案并不需要修改键像ctrlshift考虑,但他们表示,目前的选择应该不会被取代,但延长。

Also, I added multi-OS support by checking mousePressedandmouseReleased.

此外,我通过检查mousePressed添加了多操作系统支持mouseReleased

Following, you can find a complete example on how to adjust the selected rows, using the ListSelectionModel, including MouseEvent#getModifierschecks. After that, it is possible to open a (optional) JPopupMenu.

下面,您可以找到有关如何使用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);
  }
});