如何自动右键单击 jtable 并选择第一个 JMenu 项目。

huangapple go评论88阅读模式
英文:

How to automatically right click on jtable and select first JMenu item

问题

我能够加载 JSON 并通过以下代码自动选择整个表格:

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

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

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

JMenuItem addToSiteMap = new JMenuItem("添加到站点地图");
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);
}));
英文:

I have a swing application where we pass a json to the textfield and after clicking on load button, a table is getting populated.

I am able to load the json and selected the whole table automatically through following code:

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

Now I want to right click on the selected table and choose first option from the popupmenu. Any suggestions?

I want to automate this particular part of 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);
        }));

答案1

得分: 1

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

一个弹出菜单包含JMenuItemJMenu。无论哪种方式,具有实际操作的是JMenuItem

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

示例:

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);
        });
    }
}
英文:

> Now I want to right click on the selected table and choose first option from the popupmenu.

A popup menu contains JMenuItems or JMenus. Either way, the ones with an actual action are the JMenuItems.

A JMenuItem is a button as well. You already use resourceButton.doClick(). You can use doClick to a JMenuItem too.

An example:

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

答案2

得分: 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);
}));
英文:

You can use button for this purpose instead of the popup menu. You can add the button and write an action listener on it like below

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



</details>



huangapple
  • 本文由 发表于 2020年7月31日 17:29:12
  • 转载请务必保留本文链接:https://go.coder-hub.com/63189213.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定