java Netbeans 中的 JTable 右键单击​​弹出菜单

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3241537/
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-10-30 00:56:51  来源:igfitidea点击:

JTable Right-Click popupmenu in Netbeans

javanetbeansnetbeans6.8

提问by DRJTower

I want to add a right click popupmenu to a JTable in NetBeans IDE (seems like a simple task... lol)

我想在 NetBeans IDE 中向 JTable 添加一个右键单击弹出菜单(似乎是一个简单的任务......哈哈)

I got it to partly work by

我让它部分工作

  1. adding a popupmenu to the form
  2. adding menuitems to the popupmenu
  3. go to properites of JTable
  4. click binding tab
  5. set ComponentPopupMenu value to my popupmenu
  1. 向表单添加弹出菜单
  2. 将菜单项添加到弹出菜单
  3. 转到 JTable 的属性
  4. 单击绑定选项卡
  5. 将 ComponentPopupMenu 值设置为我的弹出菜单

But this only partly works. Now I when I right click on the Table the menu pops up, but the selected row in the JTable does not change. So in when the menuitem's actionPerformed is called I have no idea what row in the JTable was clicked on.

但这只是部分有效。现在,当我右键单击表时,菜单会弹出,但 JTable 中的选定行不会更改。因此,在调用菜单项的 actionPerformed 时,我不知道 JTable 中的哪一行被单击。

How can I get this? or is there an easier way to do this in netbeans?

我怎样才能得到这个?或者在netbeans中有更简单的方法吗?

I know there are others ways of doing this (in code), but I would prefer to use netbeans GUI builder.

我知道还有其他方法可以做到这一点(在代码中),但我更喜欢使用 netbeans GUI 构建器。

Has anyone ever done this before?

以前有人这样做过吗?

Thanks for your help!

谢谢你的帮助!

回答by camickr

Why do you rely on an IDE to generate code for you? What happens when you move to a different IDE and you have to learn how to do it for that ide? Learn how to write your own code then the IDE doesn't matter:

为什么要依靠 IDE 为您生成代码?当您转移到不同的 IDE 并且必须学习如何为该 IDE 执行此操作时会发生什么?学习如何编写自己的代码,然后 IDE 就无所谓了:

table.addMouseListener( new MouseAdapter()
{
    public void mouseReleased(MouseEvent e)
    {
        if (e.isPopupTrigger())
        {
            JTable source = (JTable)e.getSource();
            int row = source.rowAtPoint( e.getPoint() );
            int column = source.columnAtPoint( e.getPoint() );

            if (! source.isRowSelected(row))
                source.changeSelection(row, column, false, false);

            popup.show(e.getComponent(), e.getX(), e.getY());
        }
    }
});

回答by xchiltonx

Hopefully I can answer it for Netbeans... and I hope this helps someone

希望我可以为 Netbeans 回答......我希望这对某人有所帮助

  1. adding a popupmenu to the form (it goes in Other Components) call it jPopupMenu for example
  2. adding menuitems to the popupmenu
  3. go to properites of JTable (call it jTableDataOrSomething for example)
  4. click binding tab (or right click on jTable Bind > elements)
  5. set ComponentPopupMenu value to my called jPopupMenu

    Next steps,

  6. while in properties select Events and goto mouseReleased set it to your jTableDataOrSomething (or right click on table, Events > Mouse > mouseReleased)

    Netbeans creates an empty function and set the following code

    private void jTableDataOrSomethingMouseReleased(java.awt.event.MouseEvent evt) {
    if (evt.isPopupTrigger())
    {
        JTable source = (JTable)evt.getSource();
        int row = source.rowAtPoint( evt.getPoint() );
        int column = source.columnAtPoint( evt.getPoint() );
    
        if (!source.isRowSelected(row)) {
            source.changeSelection(row, column, false, false);
        }
        jPopupMenu.show(evt.getComponent(), evt.getX(), evt.getY());
    }
    }
    
  7. create the menuitem action performed for EACH menu item

    Then in there you can use:

        int[] rows = jTableDataOrSomething.getSelectedRows();
        for (int row : rows) {
            boolean j = true;
            try {
                modelRow = jTableDataOrSomething.convertRowIndexToModel(row);
                //do something with the selected rows...
    
  1. 向表单添加一个弹出菜单(它进入其他组件),例如将其称为 jPopupMenu
  2. 将菜单项添加到弹出菜单
  3. 转到 JTable 的属性(例如将其称为 jTableDataOrSomething)
  4. 单击绑定选项卡(或右键单击 jTable 绑定 > 元素)
  5. 将 ComponentPopupMenu 值设置为我调用的 jPopupMenu

    下一步,

  6. 而在属性中选择事件并转到 mouseReleased 将其设置为您的 jTableDataOrSomething(或右键单击表格,事件 > 鼠标 > mouseReleased)

    Netbeans 创建一个空函数并设置以下代码

    private void jTableDataOrSomethingMouseReleased(java.awt.event.MouseEvent evt) {
    if (evt.isPopupTrigger())
    {
        JTable source = (JTable)evt.getSource();
        int row = source.rowAtPoint( evt.getPoint() );
        int column = source.columnAtPoint( evt.getPoint() );
    
        if (!source.isRowSelected(row)) {
            source.changeSelection(row, column, false, false);
        }
        jPopupMenu.show(evt.getComponent(), evt.getX(), evt.getY());
    }
    }
    
  7. 创建为每个菜单项执行的菜单项操作

    然后在那里你可以使用:

        int[] rows = jTableDataOrSomething.getSelectedRows();
        for (int row : rows) {
            boolean j = true;
            try {
                modelRow = jTableDataOrSomething.convertRowIndexToModel(row);
                //do something with the selected rows...
    

This takes multirow selection and considers sorting/filtering.

这需要多行选择并考虑排序/过滤。

Finish the function with

完成函数

    jTableDataOrSomething.repaint();

Enjoy

享受