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

发布于 2024-09-10 05:57:55 字数 471 浏览 6 评论 0原文

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

将其部分工作,

  1. 我通过向表单添加弹出菜单
  2. 将菜单项添加到弹出菜单
  3. 转到 JTable
  4. 单击绑定 的属性选项卡
  5. 将 ComponentPopupMenu 值设置为我的 popupmenu

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

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

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

以前有人这样做过吗?

感谢您的帮助!

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

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

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.

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

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

Has anyone ever done this before?

Thanks for your help!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

不…忘初心 2024-09-17 05:57:55

为什么要依赖 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());
        }
    }
});

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:

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());
        }
    }
});
如梦亦如幻 2024-09-17 05:57:55

希望我能为 Netbeans 回答这个问题...我希望这可以帮助有人

  1. 向表单添加弹出菜单(它位于其他组件中)调用它
    jPopupMenu 例如,
  2. 将菜单项添加到弹出菜单中
  3. 转到 JTable 的属性(例如,将其称为 jTableDataOrSomething)
  4. 单击绑定选项卡(或右键单击 jTable Bind > elements)
  5. 将 ComponentPopupMenu 值设置为我调用的 jPopupMenu

    下一步,

  6. 在属性中选择 Events 并转到 mouseReleased 将其设置为您的 jTableDataOrSomething (或右键单击表,Events > Mouse > mouseReleased )

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

    private void jTableDataOrSomethingMouseReleased(java.awt.event.MouseEvent evt) {
    if (evt.isPopupTrigger())
    {
        JTable 源 = (JTable)evt.getSource();
        int row = source.rowAtPoint( evt.getPoint() );
        int 列 = source.columnAtPoint( evt.getPoint() );
    
        if (!source.isRowSelected(row)) {
            source.changeSelection(行、列、假、假);
        }
        jPopupMenu.show(evt.getComponent(), evt.getX(), evt.getY());
    }
    }
    
  7. create对每个菜单项执行的 menuitem 操作

    然后你可以在其中使用:

     int[] rows = jTableDataOrSomething.getSelectedRows();
        for (int row : rows) {
            布尔值 j = true;
            尝试 {
                modelRow = jTableDataOrSomething.convertRowIndexToModel(row);
                //对选定的行做一些事情...
    

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

完成该函数

    jTableDataOrSomething.repaint();

使用Enjoy

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

  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...
    

This takes multirow selection and considers sorting/filtering.

Finish the function with

    jTableDataOrSomething.repaint();

Enjoy

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文