【问题标题】:How to automatically right click on jtable and select first JMenu item如何自动右键单击jtable并选择第一个JMenu项目
【发布时间】:2020-07-31 09:29:12
【问题描述】:

我有一个摇摆应用程序,我们将一个 json 传递给文本字段,单击加载按钮后,一个表格被填充。

我可以通过以下代码加载 json 并自动选择整个表格:

  resourceButton.doClick();
  this.table.selectAll();

现在我想右键单击选定的表并从弹出菜单中选择第一个选项。有什么建议吗?

我想自动化 UI 的这个特定部分:


    JMenuItem addToSiteMap = new JMenuItem("Add to site map");
    addToSiteMap
        .addActionListener(e -> IntStream.of(tab.getTable().getSelectedRows()).forEach(row -> {
          int index = (int) tab.getTable()
              .getValueAt(row, tab.getTable().getColumn("#").getModelIndex());
          HttpRequestResponse httpRequestResponse = this.httpRequestResponses.get(index);
          callbacks.addToSiteMap(httpRequestResponse);
        }));

【问题讨论】:

  • 使用弹窗的目的是让用户从弹窗中选择一个Action。如果您总是想调用弹出窗口中的第一项,那么用户如何调用第二项或第三项?如果您只有一个项目,那么您不应该使用弹出窗口,因为用户不希望某些事情会自动发生。相反,您应该在框架中添加一个按钮,以便当用户单击该按钮时,将对所有选定项目执行操作

标签: java swing jtable jpopupmenu


【解决方案1】:

您可以为此使用按钮而不是弹出菜单。您可以添加按钮并在其上编写一个动作监听器,如下所示

button.addActionListener(e -> IntStream.of(this.getTable().getSelectedRows()).forEach(row -> {
      int index = (int) this.getTable()
              .getValueAt(row, this.getTable().getColumn("#").getModelIndex());
      HttpRequestResponse httpRequestResponse = this.httpRequestResponses.get(index);
      resourceTextField.setText(String.valueOf(index));
  callbacks.addToSiteMap(httpRequestResponse);
    }));

【讨论】:

  • 是的,这就是我几个小时前的建议。我想我可以发布一个答案,但我假设如果 OP 知道如何将 ActionListener 添加到 JMenuItem,然后知道如何将 ActionListener 添加到 JButton。
  • 我正在自动化这些东西,所以我没有选择更改 UI。虽然你说的是相关的。
【解决方案2】:

现在我想右键单击选定的表并从弹出菜单中选择第一个选项。

弹出菜单包含JMenuItems 或JMenus。不管怎样,有实际行动的是JMenuItems。

JMenuItem 也是一个按钮。您已经使用resourceButton.doClick()。您也可以将doClick 用于JMenuItem

一个例子:

public class TableTest {
    public static void main(String[] args) {
        SwingUtilities.invokeLater(() -> {
            JFrame frame = new JFrame();
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            frame.setLayout(new BorderLayout());

            JTable table = new JTable(new Object[][] { { "something" } }, new String[] { "column" });

            JPopupMenu popupMenu = new JPopupMenu();
            JMenuItem menuItem = new JMenuItem("MenuItem");
            menuItem.addActionListener(e -> {
                System.out.println("Popup item clicked.");
            });
            popupMenu.add(menuItem);

            table.setComponentPopupMenu(popupMenu);

            frame.add(new JScrollPane(table), BorderLayout.CENTER);

            JButton button = new JButton("Click me to fire Popupmenu item");
            button.addActionListener(e -> {
                menuItem.doClick();
            });
            frame.add(button, BorderLayout.PAGE_END);

            frame.pack();
            frame.setLocationByPlatform(true);
            frame.setVisible(true);
        });
    }
}

【讨论】:

  • 它会触发右键单击吗?因为 doclick() 会左键单击
  • 您好,我已经编辑了我的问题。如果对您有帮助,您可以看看吗
  • @YatinKanyal 为什么不直接打电话给addToSiteMap.doClick()
  • 我试过了,但什么也没发生。我是不是错过了什么。这段代码是否必须做任何事情? HttpRequestResponse httpRequestResponse = this.httpRequestResponses.get(index); callbacks.addToSiteMap(httpRequestResponse);
  • @YatinKanyal 如果这是一项繁重的操作 - 需要时间,是的,它会发挥作用。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-01-09
  • 2012-03-02
  • 1970-01-01
  • 2016-03-09
  • 1970-01-01
相关资源
最近更新 更多