英文:
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
现在我想右键单击所选的表格,并从弹出菜单中选择第一个选项。
一个弹出菜单包含JMenuItem或JMenu。无论哪种方式,具有实际操作的是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>
				通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。


评论