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

前端 未结 2 1012
被撕碎了的回忆
被撕碎了的回忆 2020-12-08 07:08

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 e

相关标签:
2条回答
  • 2020-12-08 07:29

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

    ......

    0 讨论(0)
  • 2020-12-08 07:47

    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.

    Also, I added multi-OS support by checking mousePressed and mouseReleased.

    Following, you can find a complete example on how to adjust the selected rows, using the ListSelectionModel, including MouseEvent#getModifiers checks. After that, it is possible to open a (optional) 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);
      }
    });
    
    0 讨论(0)
提交回复
热议问题