【问题标题】:Java Swing JTable; Right Click Menu (How do I get it to "select" aka highlight the row)Java Swing JTable;右键单击菜单(如何让它“选择”也就是突出显示该行)
【发布时间】:2010-08-24 15:47:14
【问题描述】:

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

我在 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 上执行右键单击时,该行都不会突出显示(我将选择设置为仅行)。我已经寻找了几个 setSelected() 函数,但找不到合适的。默认情况下,左键单击会自动突出显示该行。如何设置右键单击?

【问题讨论】:

    标签: java swing jtable right-click


    【解决方案1】:

    像这样:

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

    ......

    【讨论】:

    • 就这样 :]... mouseReleased() 的上半部分是我需要的。将被投票+接受作为答案。谢谢,你是男人(或女人)!
    • 很好的答案。一个细节:在某些平台(Mac OS X 命名)上,弹出菜单由mousePressed 而不是mouseReleased 触发,因此如果您使用此代码,如果您在 Mac OS 上运行应用程序,弹出菜单将不会显示十。
    • 两次获取rowIndex的目的是什么?我的意思是,rrowindex 有什么区别?
    • 完美解决方案。如果您正在使用此解决方案,请记住不要使用 setComponentPopupMenu() 方法。
    • 请参阅下面来自 Oliver Hoffmann 的评论 16 年 8 月 26 日 12:26: 不需要手动选择处理,JTable 已经尊重 SHIFT 和 CTRL 键等。原始解决方案中需要的唯一更改是不使用 setRowSelectionInterval() 以防已选择焦点行,例如: if (!table.getSelectionModel().isSelectedIndex(r)) table.setRowSelectionInterval(r, r);
    【解决方案2】:

    接受的答案不考虑 ctrlshift 等修饰键,但它们表明当前选择应该被替换,但已扩展。

    此外,我通过检查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);
      }
    });
    

    【讨论】:

    • 无需手动选择处理,JTable 已经支持 SHIFT 和 CTRL 键等。原始解决方案中唯一需要更改的是不使用 setRowSelectionInterval() 以防已经选择了焦点行,例如: if (!table.getSelectionModel().isSelectedIndex(r)) table.setRowSelectionInterval(r, r);
    【解决方案3】:

    您可以创建另一个 MouseEvent(这里的示例是在 JTable 子类中;processMouseEvent() 具有受保护的访问权限,否则可以使用 dispatchEvent() 方法)。负责使用修饰符进行选择更新。

    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(), 1, false, MouseEvent.BUTTON1));
                if (getSelectedRowCount() != 0)
                    popup.show(e.getComponent(), e.getX(), e.getY());
            }
        }
    });
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-09-14
      • 1970-01-01
      相关资源
      最近更新 更多